当前位置:   article > 正文

JAVA语言程序设计(基础篇) 第十版——第11章 继承和多态 (参考答案)_java设计基础第十版第七章编程答案

java设计基础第十版第七章编程答案

编程练习题

(11.2~11.4节)+(11.5~11.14节)

11.1(三角形Triangle)

父类(左边) 和   子类(右边)继承关系,

如下图: 



  1. package p11;
  2. public class GeometricObject {
  3. private String color = "white";
  4. private boolean filled;
  5. private java.util.Date dateCreated;
  6. public GeometricObject() {
  7. dateCreated=new java.util.Date();
  8. }
  9. public GeometricObject(String color, boolean filled) {
  10. dateCreated=new java.util.Date();
  11. this.color=color;
  12. this.filled=filled;
  13. }
  14. public String getColor() {
  15. return color;
  16. }
  17. public void setColor(String color) {
  18. this.color=color;
  19. }
  20. public boolean isFilled() {
  21. return filled;
  22. }
  23. public void setFilled(boolean filled) {
  24. this.filled=filled;
  25. }
  26. public java.util.Date getDateCreated() {
  27. return dateCreated;
  28. }
  29. public String toString() {
  30. return "created on " + dateCreated + "\ncolor: " + color+
  31. " and filled: " + filled;
  32. }
  33. }

 

  1. package p11;
  2. public class Triangle extends GeometricObject {
  3. private double side1 = 1.0;
  4. private double side2 = 1.0;
  5. private double side3 = 1.0;
  6. public Triangle() {
  7. }
  8. //这是一个 重载 的构造方法
  9. public Triangle(double side1, double side2,
  10. double side3, String color, boolean filled) {
  11. super(color,filled);//或者 调用父类中已有方法 ,如下
  12. //setColor(color);
  13. //setFilled(filled);
  14. this.side1=side1;
  15. this.side2=side2;
  16. this.side3=side3;
  17. }
  18. public double getSide1() {
  19. return side1;
  20. }
  21. public double getSide2() {
  22. return side2;
  23. }
  24. public double getSide3() {
  25. return side3;
  26. }
  27. public double getArea() {
  28. double s=(side1+side2+side3)/2;
  29. double area=Math.pow( s*(s-side1)*(s-side2)*(s-side3) , 0.5);
  30. return area;
  31. }
  32. public double getPerimeter() {
  33. return side1+side2+side3;
  34. }
  35. @Override
  36. public String toString() {
  37. return super.toString() + "\nArea is " + getArea();
  38. }
  39. }
  1. package p11;
  2. import java.util.Scanner;
  3. public class Test1 {
  4. public static void main(String[] args) {
  5. Scanner input=new Scanner(System.in);
  6. System.out.print("请输入三角形的三条边:");
  7. double s1=input.nextInt();
  8. double s2=input.nextInt();
  9. double s3=input.nextInt();
  10. System.out.print("请输入三角形的颜色: ");
  11. String c1=input.next();
  12. System.out.print("请输入一个boolean值(false:不填充;true:填充):");
  13. boolean f1=input.nextBoolean();
  14. input.close();
  15. //创建对象
  16. GeometricObject object=new Triangle(s1, s2, s3, c1, f1);
  17. System.out.println(object.toString());
  18. }
  19. }

 

(11.5~11.14节)

11.2(Person、Student、Employee、Faculty 和 Staff类)

这个不难,我也就是在 受聘日期 那里“卡壳”了一下

总体的继承关系:


 

  1. package p11;
  2. public class Person {
  3. private String name;
  4. private String adress;
  5. private String phone;
  6. private String email;
  7. public Person(String name, String adress, String phone, String email) {
  8. this.name=name;
  9. this.adress=adress;
  10. this.phone=phone;
  11. this.email=email;
  12. }
  13. public Person(String name) {
  14. this.name=name;
  15. }
  16. public String getAdress() {
  17. return adress;
  18. }
  19. public void setAdress(String adress) {
  20. this.adress=adress;
  21. }
  22. public String getPhone() {
  23. return phone;
  24. }
  25. public void setPhone(String phone) {
  26. this.phone=phone;
  27. }
  28. public String getEmail() {
  29. return email;
  30. }
  31. public void setEmail(String email) {
  32. this.email=email;
  33. }
  34. public String toString() {
  35. return "The Person's name is "+name+"\nAdress is "+adress+
  36. "\nPhone is "+phone+"\nEmail is "+email;
  37. }
  38. }
  1. package p11;
  2. public class Student extends Person {
  3. private int classState;
  4. public final static int FRESHMAN=1;//大一新生
  5. public final static int SOPHOMORE=2;//大二学生
  6. public final static int JUNIOR=3;//大三学生
  7. public final static int SENIOR=4;//大四学生
  8. public Student(int classState, String name, String adress, String phone, String email) {
  9. super(name, adress, phone, email);
  10. this.classState=classState;
  11. }
  12. public int getClassState() {
  13. return classState;
  14. }
  15. public void setClassState(int classState) {
  16. this.classState=classState;
  17. }
  18. @Override
  19. public String toString() {
  20. return "The Student's classState is "+classState+"\n"+super.toString();
  21. }
  22. }
  1. package p11;
  2. import java.util.Date;
  3. import p10.MyDate;
  4. public class Employee extends Person {
  5. private String office;
  6. private double salary;
  7. private MyDate DateOfAppointment;//受聘日期
  8. //构造方法
  9. public Employee(String office, double salary, MyDate DateOfAppointment,
  10. String name, String adress, String phone, String email) {
  11. super(name, adress, phone, email);
  12. this.DateOfAppointment=DateOfAppointment;
  13. this.salary=salary;
  14. this.office=office;
  15. }
  16. public String getOffice() {
  17. return office;
  18. }
  19. public void setOffice(String office) {
  20. this.office=office;
  21. }
  22. public double getSalary() {
  23. return salary;
  24. }
  25. public void setSalary(double salary) {
  26. this.salary=salary;
  27. }
  28. public String getDateOfAppointment() {
  29. return DateOfAppointment.getYear()+"年"+DateOfAppointment.getMonth()
  30. +"月"+DateOfAppointment.getDay()+"日";
  31. }
  32. @Override
  33. public String toString() {
  34. return "The Employee's dateOfAppointment is "+ getDateOfAppointment()
  35. +"\nOffice is "+office+"\nSalary is "+salary+"\n"+super.toString();
  36. }
  37. }
  1. package p11;
  2. import p10.MyDate;
  3. public class Faculty extends Employee {
  4. private long officeHours;
  5. private String rank;
  6. //构造方法
  7. public Faculty(long officeHours, String rank, String office, double salary, MyDate DateOfAppointment,
  8. String name, String adress, String phone, String email) {
  9. super(office, salary, DateOfAppoint
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/代码探险家/article/detail/998215
推荐阅读
相关标签
  

闽ICP备14008679号