华南理工大学大一JAVA复习题(共17页).doc
精选优质文档-倾情为你奉上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 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 static 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 static 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 (double)", "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.parseDouble(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 class 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 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 (int i = 1; i <= 6; i+) for (int j = 1; j <= 7-i; j+) System.out.print(j + " "); System.out.println(); 5.1使用下述方法头,编写一个将大写字母转换成小写字母的方法。public static char upperCaseToLowerCase (char ch)例如, upperCaseToLowerCase (B) returns b.public class Exercise5_1 public static void main(String args) System.out.println(upperCaseToLowerCase('A'); System.out.println(upperCaseToLowerCase('e'); System.out.println(upperCaseToLowerCase('4'); System.out.println(upperCaseToLowerCase('R'); public static char upperCaseToLowerCase(char c) if (c >= 'A' && c <= 'Z') return (char)(c + ('a' - 'A'); return c; 5.5 (对三个数排序)编写以下方法,按升序显示三个数。public static void sort(double d1, double d2, double d3)public class Exercise5_5 public static void main(String args) sort(3.4, 5.4, 2.2); public static void sort(double num1, double num2, double num3) if (num1 > 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个数字,计算它们的平均值并且找出有多少个数字在平均值之上。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 += numbersi; double average = sum / 10; int countGreater = 0; for (int i = 0; i < numbers.length; i+) if (numbersi > 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 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 = 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()方法返回它的周长.创建两个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("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 " + yourRectangle.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 double 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.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.println("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 “Triangle: 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; protected 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 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 double 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 "Triangle: 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 ”+triangle.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 conflict 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 SOPHOMORE = 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 year; 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 Staff extends Employee protected String title; public String toString() return "Staff's title is " + title; 9.3创建两个子类,用于对账账户和储蓄账户。对账账户有一个透支限定额,但储蓄账户不能透支。画出UML图并实现这些类。编写测试程序,创建Account和Savingaccount,checkingaccount的对象并调用tostring方法。/ Exercise9_3.java: Create two subclasses of Account,/ Checking and Savings. 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接口,并在该类中定义max方法,求两个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 Circle1 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 circle's radius is " + circle.getRadius(); System.out.println(circle); / Create two comarable cylinders Cylinder1 cylinder1 = new Cylinder1(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("cylinder1's volume is " + cylinder1.findVolume(); System.out.println("cylinder2's volume is " + cylinder2.findVolume(); System.out.println("The max cylinder's 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; protected 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; / Setter 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 public 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("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 public void setRadius(double radius)