《华南理工大学大一JAVA复习题.doc》由会员分享,可在线阅读,更多相关《华南理工大学大一JAVA复习题.doc(13页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、精品文档,仅供学习与交流,如有侵权请联系网站删除2.1从输入对话框读入double型的华氏度,将其转换为摄氏度,并在消息对话框中显示结果。Celsius = (5/9)*(Fahrenheit-32)import javax.swing.JOptionPane;public class Exercise2_1 public static void main(String args) String fahrenheitString = JOptionPane.showInputDialog(null, Enter a temperature in fahrenheit:, Exercise2_1
2、 Input, JOptionPane.QUESTION_MESSAGE); double fahrenheit = Double.parseDouble(fahrenheitString); double celsius = (5.0 / 9.0) * (fahrenheit - 32); System.out.println(The temperature is + celsius + in Celsius); System.exit(0);2.7编写程序将大写字母转换为小写字母,该字符在源代码ASCII中指定为直接量。public class Exercise2_7 public sta
3、tic void main(String args) char uppercase = F; int offset = (int)a - (int)A; char lowercase = (char)(int)uppercase + offset); System.out.print(The lowercase letter is + lowercase);3.1 (三角形有效性验证)读入三角形的三条边并确定输入是否有效。如果任意两条边和大于第三边则输入有效。import javax.swing.JOptionPane;public class Exercise3_1 public stati
4、c void main(String args) String numberString = JOptionPane.showInputDialog(null, Enter the first edge length (double), Exercise3_1 Input, JOptionPane.QUESTION_MESSAGE); double edge1 = Double.parseDouble(numberString); numberString = JOptionPane.showInputDialog(null, Enter the second edge length (dou
5、ble), Exercise3_1 Input, JOptionPane.QUESTION_MESSAGE); / Convert string to double double edge2 = Double.parseDouble(numberString); numberString = JOptionPane.showInputDialog(null, Enter the third edge length (double), Exercise3_1 Input, JOptionPane.QUESTION_MESSAGE); double edge3 = Double.parseDoub
6、le(numberString); System.out.println(Can edges + edge1 + , + edge2 + , and + edge3 + form a triangle? + ( (edge1 + edge2 edge3) & (edge1 + edge3 edge2) & (edge2 + edge3 edge1); System.exit(0);4.1 (千克转换成磅)编一个显示下列表格的程序(1千克为2.2磅)(1kilogram = 2.2 pounds)KilogramsPounds12.236.6197433.4199437.8public clas
7、s Exercise3_8 public static void main(String args) System.out.println(kilogramsttpounds); System.out.println(-); int kilograms = 1; for (int i = 1; i = 100; kilograms += 2, i+) System.out.println(kilograms + tt + kilograms * 2.2);4.18 用嵌套的循环语句,分别编写程序打印下列图案。Pattern 111 21 2 31 2 3 41 2 3 4 51 2 3 4 5
8、 6Pattern 21 2 3 4 5 61 2 3 4 51 2 3 41 2 31 2 /* Print Pattern I */public class Exercise3_23 public static void main(String args) for (int i = 1; i = 6; i+) for (int j = 1; j = i; j+) System.out.print(j + ); System.out.println();/pattern 2public class test public static void main(String args) for (
9、int i = 1; i = 6; i+) for (int j = 1; j = A & c num2) double temp = num1; num1 = num2; num2 = temp; if (num2 num3) double temp = num2; num2 = num3; num3 = temp; if (num1 num2) double temp = num1; num1 = num2; num2 = temp; System.out.println(The sorted numbers are + num1 + + num2 + + num3);6.1读入10个数字
10、,计算它们的平均值并且找出有多少个数字在平均值之上。import javax.swing.*;public class Exercise5_1 public static void main(String args) double numbers = new double10; double sum = 0; for (int i = 0; i numbers.length; i+) String s = JOptionPane.showInputDialog(Enter a number: ); numbersi = Double.parseDouble(s); sum += numbers
11、i; double average = sum / 10; int countGreater = 0; for (int i = 0; i average) countGreater+; System.out.println(The number of values greater than the average is + countGreater); System.exit(0);6.5读入10个数并且显示其中相互不同的数。提示:读入一个数,如果它是新数,则把它存储在数组中;如果数组中已有该数,则把它丢弃。输入结束后,数组中的数都是不同的数。public class Exercise6_5
12、 public static void main(String args) int numbers = new int10; int size = 0; for (int i = 0; i numbers.length; i+) String s = JOptionPane.showInputDialog(null, Enter an integer); int value = Integer.parseInt(s); boolean isInArray = false; for (int j = 0; j size; j+) if (numbersj = value) isInArray =
13、 true; break; if (!isInArray) numberssize = value; size+;7.1 编写名为Rectangle的类表示矩形,这个类包括:l 两个double型的数据域 width and height表示矩形的宽和高,它们的默认值都是1l String类型的数据域 color 表示矩形的颜色,进一步假设所有矩形的颜色都是相同的,默认颜色为白色.l 无参构造方法创建默认矩形.l 一个构造方法创建指定width and height的矩形.l 所有三个数据域的访问器方法和修改器方法;l getArea()方法返回该矩形的面积.l getPerimetet()方
14、法返回它的周长.创建两个Rectangle 对象。设计width 5 and height 15 为第一个对象and width 4.3 and height 40.5为第二个对象。所有 Rectangle对象的颜色为红色。显示两个对象的属性并求出它们的面积和周长。/ Exercise7_1.java: Create the Rectangle classpublic class test public static void main (String args) Rectangle myRectangle = new Rectangle(5, 40); System.out.println(
15、The area of a rectangle with width + myRectangle.getWidth() + and height + myRectangle.getHeight() + is + myRectangle.getArea(); System.out.println(The color is + myRectangle.getColor(); Rectangle yourRectangle = new Rectangle(1.0, 3.5); System.out.println(The area of a rectangle with width + yourRe
16、ctangle.getWidth() + and height + yourRectangle.getHeight() + is + yourRectangle.getArea(); System.out.println(The color is + yourRectangle.getColor();class Rectangle private double width, height; private static String color=red; public Rectangle(double w, double h) width = w; height = h; public dou
17、ble getWidth() return width; public double getHeight() return height; public static String getColor() return color; public static void setColor(String color) Rectangle.color = color; public double getArea() return width*height; public double getPerimeter() return (width*2+height*2);8.21假设文本文件名为8-21.
18、tXt,包含未指定个数的分数。编写程序,从文件中读入分数并显示它们的和与平均值。分数被空格分开。import java.util.*;import java.io.*;public class Exercise8_21 public static void main(String args) throws Exception Scanner input = new Scanner(new File(Exercise8_21.txt); double sum = 0; while (input.hasNext() sum += input.nextDouble(); System.out.pri
19、ntln(Total is + sum);9.1设计名为Triangle的类, 扩展GeometricObject.该类包括:l 三个名为side1,side2, 和side3 的double数据域,三角形三边默认值为1.0 ;l A一个无参构造方法创建默认的三角形.l 指定side1, side2,and side3的值,创建三角形的构造方法.l 三个数据域的访问器方法。l 名为getArea()的方法返回三角形的面积.l 名为getPerimetet()的方法返回三角形的周长.l 名为toString()的方法返回描述三角形的字符串.计算三角形面积的公式执行如下:Return “Trian
20、gle: side1= ”+ side1 +” side2 = ”+ sdie2 +” side3 = ”+side3;画出包括Triangle和GeometricObject类的UML图,创建边长为 10, 15, 10的Triangle对象,设置颜色为 yellow ,是否填充为true, 显示它的面积,周长,颜色以及是否填充。public abstract class GeometricObject private String color = white; private boolean filled; private java.util.Date dateCreated; prote
21、cted GeometricObject() dateCreated = new java.util.Date(); public String getColor() return color; public void setColor(String color) this.color = color; public boolean isFilled() return filled; public void setFilled(boolean filled) this.filled = filled; public java.util.Date getDateCreated() return
22、dateCreated; public String toString() return created on + dateCreated + ncolor: + color + and filled: + filled; public abstract double getArea(); public abstract double getPerimeter();/ Exercise9_1.java: Create a class Triangle that extends/ GeometricObjectclass Triangle extends GeometricObject doub
23、le side1, side2, side3; public Triangle(double side1, double side2, double side3) public double getArea() double s = (side1 + side2 + side3) / 2; return Math.sqrt(s * (s - side1) * (s - side2) * (s - side3); public double getPerimeter() return side1 + side2 + side3; public String toString() return T
24、riangle: side1 = + side1 + side2 = + side2 + side3 = + side3;public class Exercise9_1 public static void main(String args) Triangle triangle = new Triangle(1, 1.5, 1); triangle.setColor(yellow); triangle.setFilled(true);System.out.println(triangle.toString();System.out.println(“The color is ”+triang
25、le.getColor(); System.out.println(The area is + triangle.getArea(); System.out.println(The perimeter is + triangle.getPerimeter();9.2 The Person, Student, Employee, Faculty, and Staff classes/ Exercise8_2.java: Create three classes Person,/ Student, Employee, Faculty, and Staff/ To avoid name confli
26、ct with another Student class, Student class/ is named Student1class Person protected String name; protected String address; protected String phoneNumber; protected String email; public String toString() return Person;class Student1 extends Person public static int FRESHMAN = 1; public static int SO
27、PHOMORE = 2; public static int JUNIOR = 3; public static int SENIOR = 4; protected int status; public String toString() return Student;class Employee extends Person protected String office; protected int salary; protected MyDate dateHired; public String toString() return Employee;class MyDate int ye
28、ar; int month; int day;class Faculty extends Employee public static int LECTURER = 1; public static int ASSISTANT_PROFESSOR = 2; public static int ASSOCIATE_PROFESSOR = 3; public static int PROFESSOR = 4; protected String officeHours; protected int rank; public String toString() return Faculty;class
29、 Staff extends Employee protected String title; public String toString() return Staffs title is + title;9.3创建两个子类,用于对账账户和储蓄账户。对账账户有一个透支限定额,但储蓄账户不能透支。画出UML图并实现这些类。编写测试程序,创建Account和Savingaccount,checkingaccount的对象并调用tostring方法。/ Exercise9_3.java: Create two subclasses of Account,/ Checking and Savings
30、. Account is defined in Exercise 5.3class Checkings extends Account protected int overdraftLimit = 5000; public String toString() return Checkings;class Saving extends Account protected int overdraftLimit = 5000; public String toString() return Saving;10.1修改GeometricObject类,以实现Comparable接口,并在该类中定义ma
31、x方法,求两个GeometricObject对象中较大者。画出UML图并实现新的GeometricObject类。编写程序,使用max方法求出两个圆中的较大者和两个矩形中较大者。/ Exercise10_1.java: Create a new GeometricObject class, a new Circle/ class and a new Cylinder classpublic class Exercise10_1 / Main method public static void main(String args) / Create two comarable circles Ci
32、rcle1 circle1 = new Circle1(5); Circle1 circle2 = new Circle1(4); / Display the max circle Circle1 circle = (Circle1)Max.max(circle1, circle2); System.out.println(The max circles radius is + circle.getRadius(); System.out.println(circle); / Create two comarable cylinders Cylinder1 cylinder1 = new Cy
33、linder1(5, 2); Cylinder1 cylinder2 = new Cylinder1(4, 5); / Display the max cylinder Cylinder1 cylinder = (Cylinder1)Max.max(cylinder1, cylinder2); System.out.println(); System.out.println(cylinder1s volume is + cylinder1.findVolume(); System.out.println(cylinder2s volume is + cylinder2.findVolume()
34、; System.out.println(The max cylinders tradius is + (Cylinder1)cylinder).getRadius() + ntttlength is + (Cylinder1)cylinder).getLength() + ntttvolume is + (Cylinder1)cylinder).findVolume(); System.out.println(cylinder);abstract class GeometricObject1 implements Comparable protected String color; prot
35、ected double weight; / Default construct protected GeometricObject1() color = white; weight = 1.0; / Construct a geometric object protected GeometricObject1(String color, double weight) this.color = color; this.weight = weight; / Getter method for color public String getColor() return color; / Sette
36、r method for color public void setColor(String color) this.color = color; / Getter method for weight public double getWeight() return weight; / Setter method for weight public void setWeight(double weight) this.weight = weight; / Abstract method public abstract double findArea(); / Abstract method p
37、ublic abstract double findPerimeter();/ Circle.java: The circle class that extends GeometricObjectclass Circle1 extends GeometricObject1 protected double radius; / Default constructor public Circle1() this(1.0, white, 1.0); / Construct circle with specified radius public Circle1(double radius) super
38、(white, 1.0); this.radius = radius; / Construct a circle with specified radius, weight, and color public Circle1(double radius, String color, double weight) super(color, weight); this.radius = radius; / Getter method for radius public double getRadius() return radius; / Setter method for radius publ
39、ic void setRadius(double radius) this.radius = radius; / Implement the findArea method defined in GeometricObject public double findArea() return radius*radius*Math.PI; / Implement the findPerimeter method defined in GeometricObject public double findPerimeter() return 2*radius*Math.PI; / Override the equals() method defined in the Object class public boolean equals(Circle1 circle) return this.radius = circle.getRadius(); / Override the toString() method defined in the Object class public String toString() return Circle
限制150内