《Java语言程序设计(基础篇)》(第10版 梁勇 著)第十一章练习题答案 下载本文

《Java语言程序设计(基础篇)》(第10版 梁勇 著)

第十一章 练习题答案

11.1

public class Exercise11_01 {

public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print(\); double side1 = input.nextDouble(); double side2 = input.nextDouble(); double side3 = input.nextDouble();

Triangle triangle = new Triangle(side1, side2, side3);

System.out.print(\); String color = input.nextLine(); triangle.setColor(color);

System.out.print(\); boolean filled = input.nextBoolean(); triangle.setFilled(filled);

System.out.println(\ + triangle.getArea()); System.out.println(\ + triangle.getPerimeter()); 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 \ + side1 + \ + side2 + \ + 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 \; } }

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 \; } }

class Employee extends Person { protected String office; protected int salary;

protected Calendar dateHired;

@Override

public String toString() { return \; } }

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 \; } }

class Staff extends Employee { protected String title;

@Override

public String toString() {

return \ + title; } }

11.3

class Checkings extends Account { protected int overdraftLimit = 5000;

@Override

public String toString() { return \; } }

class Saving extends Account {

protected int overdraftLimit = 5000;

@Override

public String toString() { return \; } }

11.4

import java.util.ArrayList; import java.util.Scanner;

public class Exercise11_04 {

public static void main(String[] args) {

ArrayList list = new ArrayList();

Scanner input = new Scanner(System.in);

System.out.print(\);

int value; do {

value = input.nextInt(); // 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(\ + max(list)); }

public static Integer max(ArrayList list) { if (list == null || list.size() == 0) return null;

int result = list.get(0);

for (int i = 1; i < list.size(); i++) if (result < list.get(i)) result = list.get(i);

return result; } }

11.5

import java.util.*;

public class Exercise11_05 {

public static void main(String[] args) {

Course course1 = new Course(\); Course course2 = new Course(\);

course1.addStudent(\); course1.addStudent(\); course1.addStudent(\);

course2.addStudent(\); course2.addStudent(\);

System.out.println(\ + course1.getNumberOfStudents());