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

编程练习题

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

11.1(三角形Triangle)

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

如下图: 



package p11;

public class GeometricObject {
	private String color = "white";
	private boolean filled;
	private java.util.Date dateCreated;
	
	public GeometricObject() {
		dateCreated=new java.util.Date();
	}
	
	public GeometricObject(String color, boolean filled) {
		dateCreated=new java.util.Date();
		this.color=color;
		this.filled=filled;
	}
	
	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;
	
	}
}

 

package p11;

public class Triangle extends GeometricObject {
	private double side1 = 1.0;
	private double side2 = 1.0;
	private double side3 = 1.0;
	
	public Triangle() {
		
	}
	
	//这是一个 重载 的构造方法
	public Triangle(double side1, double side2,
			double side3, String color, boolean filled) {   
		
		super(color,filled);//或者 调用父类中已有方法 ,如下
							//setColor(color);
		                   //setFilled(filled);
		this.side1=side1;
		this.side2=side2;
		this.side3=side3;
		
	}
	
	public double getSide1() {
		return side1;
	
	}
	
	public double getSide2() {
		return side2;
	
	}
	
	public double getSide3() {
		return side3;
	
	}
	
	public double getArea() {
		double s=(side1+side2+side3)/2;
		double area=Math.pow( s*(s-side1)*(s-side2)*(s-side3) , 0.5); 
		return area;
	}
	
	public double getPerimeter() {
		return side1+side2+side3;
	
	}
	
	@Override
	public String toString() {
	
		return super.toString() + "\nArea is " + getArea();
	
	}
}
package p11;

import java.util.Scanner;

public class Test1 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.print("请输入三角形的三条边:");
		double s1=input.nextInt();
		double s2=input.nextInt();
		double s3=input.nextInt();
		
		System.out.print("请输入三角形的颜色: ");
		String c1=input.next();
		
		System.out.print("请输入一个boolean值(false:不填充;true:填充):");   
		boolean f1=input.nextBoolean();
		
		input.close();
		
		//创建对象
		GeometricObject object=new Triangle(s1, s2, s3, c1, f1);
		
		System.out.println(object.toString());
		
		
		
	}

}

 

(11.5~11.14节)

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

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

总体的继承关系:


 

package p11;

public class Person {
	private String name;
	private String adress;
	private String phone;
	private String email;
	
	public Person(String name, String adress, String phone, String email) {
		this.name=name;
		this.adress=adress;
		this.phone=phone;
		this.email=email;
	}
	
	public Person(String name) {
		this.name=name;
	}
	
	public String getAdress() {
		return adress;
	
	}
	
	public void setAdress(String adress) {
		this.adress=adress;
	}
	
	public String getPhone() {
		return phone;
	
	}
	
	public void setPhone(String phone) {
		this.phone=phone;
	}
	
	public String getEmail() {
		return email;
	
	}
	
	public void setEmail(String email) {
		this.email=email;
		
	}
	
	public String toString() {
		return "The Person's name is "+name+"\nAdress is "+adress+
				"\nPhone is "+phone+"\nEmail is "+email;
	
	}
}
package p11;

public class Student extends Person {
	
	private int classState;
	
	public final static int FRESHMAN=1;//大一新生
	public final static int SOPHOMORE=2;//大二学生
	public final static int JUNIOR=3;//大三学生
	public final static int SENIOR=4;//大四学生
	
	
	public Student(int classState, String name, String adress, String phone, String email) {
		super(name, adress, phone, email);
		this.classState=classState;
		
	}
	
	public int getClassState() {
		return classState;
	
	}
	
	public void setClassState(int classState) {
		this.classState=classState;
	}
	
	@Override
	public String toString() {
		return "The Student's classState is "+classState+"\n"+super.toString();
	
	}
}
package p11;

import java.util.Date;

import p10.MyDate;

public class Employee extends Person {
	
	private String office;
	private double salary;
	private MyDate DateOfAppointment;//受聘日期
	
	//构造方法
	public Employee(String office, double salary, MyDate DateOfAppointment, 
			String name, String adress, String phone, String email) {
		
		super(name, adress, phone, email);
		
		this.DateOfAppointment=DateOfAppointment;
		this.salary=salary;
		this.office=office;
		
	}
	
	public String getOffice() {
		return office;
	
	}
	
	public void setOffice(String office) {
		this.office=office;
	}
	
	public double getSalary() {
		return salary;
	
	}
	
	public void setSalary(double salary) {
		this.salary=salary;
	}
	
	public String getDateOfAppointment() {
		
		return DateOfAppointment.getYear()+"年"+DateOfAppointment.getMonth()
			+"月"+DateOfAppointment.getDay()+"日";
	
	}
	
	@Override
	public String toString() {
		return "The Employee's dateOfAppointment is "+  getDateOfAppointment()
								+"\nOffice is "+office+"\nSalary is "+salary+"\n"+super.toString();
	
	}
}
package p11;

import p10.MyDate;

public class Faculty extends Employee {
	private long officeHours;
	private String rank
  • 34
    点赞
  • 146
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
Java语言程序设计第10版》(基础 进阶) 是由Y. Daniel Liang编写的一本经典的Java编程教材。本书内容涵盖了Java编程基础知识与进阶应用,并且通过大量的示例代码和练习题帮助读者提升编程技能。 基础主要介绍了Java编程的基本概念和语法。首先,它介绍了如何安装和配置Java开发环境,并且讲解了Java程序的基本结构和运行机制。随后,它详细介绍了Java的数据类型、运算符、控制语句和数组等基本知识。同时,它还介绍了面向对象程序设计的基本概念,如类、对象、方法和构造函数等,并且通过示例代码演示了如何使用这些概念进行编程。 进阶则进一步介绍了Java编程的高级技术和应用。其中,它详细介绍了面向对象程序设计的更深入的内容,如继承多态和抽象类等。此外,它还讲解了异常处理、文件输入输出、集合框架和泛型等高级主题。进阶还涵盖了GUI编程、多线程编程和网络编程等更加复杂的应用领域,使读者能够更全面地理解和运用Java编程。 《Java语言程序设计第10版》(基础 进阶) 答案是一本非常实用的学习教材,通过系统的学习和实践,读者可以获得扎实的Java编程基础,掌握常用的编程技巧和方法。无论是编程初学者还是有一定经验的开发人员,都可以从这本书中获得丰富的知识和实践经验,提升自己的编程水平。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值