《Java语言程序设计(基础篇)》(第10版梁勇著)
第十一章练习题答案
11.1
public class Exercise11_01 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter three sides: ");
double side1 = Double();
double side2 = Double();
double side3 = Double();
Triangle triangle = new Triangle(side1, side2, side3);
System.out.print("Enter the color: ");
String color = Line();
triangle.setColor(color);
System.out.print("Enter a boolean value for filled: ");
boolean filled = Boolean();
triangle.setFilled(filled);
System.out.println("The area is " + Area());
System.out.println("The perimeter is "
+ Perimeter());
System.out.println(triangle);
}
}
class Triangle extends GeometricObject {
private double side1 = 1.0, side2 = 1.0, side3 = 1.0;
/** Constructor */
public Triangle() {
}
/** Constructor */
public Triangle(double side1, double side2, double side3) {
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
public double getSide1() {
return side1;
}
public double getSide2() {
return side2;
}
public double getSide3() {
return side3;
}
@Override /** Override method findArea in GeometricObject */ public double getArea() {
double s = (side1 + side2 + side3) / 2;
return Math.sqrt(s * (s - side1) * (s - side2) * (s - side3)); }
@Override /** Override method findPerimeter in GeometricObject */ public double getPerimeter() {
return side1 + side2 + side3;
}
@Override
public String toString() {
// Implement it to return the three sides
return"Triangle: side1 = " + side1 + " side2 = " + side2 +
" side3 = " + side3;
}
}
11.2
public class Exercise11_02 {
public static void main(String[] args) {
}
}
class Person {
protected String name;
protected String address;
protected String phoneNumber;
protected String email;
@Override
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;
@Override
public String toString() {
return"Student";
}
}
class Employee extends Person {
protected String office;
protected int salary;
protected Calendar dateHired;
@Override
public String toString() {
return"Employee";
}
}
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;
@Override
public String toString() {
return"Faculty";
}
}
class Staff extends Employee {
protected String title;
@Override
public String toString() {
return"Staff's title is " + title;
}
}
11.3
class Checkings extends Account {
protected int overdraftLimit = 5000;
@Override
public String toString() {
return"Checkings";
}
}
class Saving extends Account {
java程序设计基础视频protected int overdraftLimit = 5000;
@Override
public String toString() {
return"Saving";
}
}
11.4
import java.util.ArrayList;
import java.util.Scanner;
public class Exercise11_04 {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<Integer>();
Scanner input = new Scanner(System.in);
System.out.print("Enter integers (input ends with 0): ");
int value;
do {
value = Int(); // Read a value from the input
if (value != 0)
list.add(value); // Add the value if it is not in the list } while (value != 0);
// Display the maximum number
System.out.print("The maximum number is " + max(list));
}
public static Integer max(ArrayList<Integer> list) {
if (list == null || list.size() == 0)
return null;
int result = (0);
for (int i = 1; i < list.size(); i++)
if (result < (i))
result = (i);
return result;
}
}
11.5
import java.util.*;
public class Exercise11_05 {
public static void main(String[] args) {
Course course1 = new Course("Data Structures");
Course course2 = new Course("Database Systems");
course1.addStudent("Peter Jones");
course1.addStudent("Brian Smith");
course1.addStudent("Anne Kennedy");
course2.addStudent("Peter Jones");
course2.addStudent("Steve Smith");
System.out.println("Number of students in course1: "
+ NumberOfStudents());
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论