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;
	
	//构造方法
	public Faculty(long officeHours, String rank, String office, double salary, MyDate DateOfAppointment,
			String name, String adress, String phone, String email) {
		
		super(office, salary, DateOfAppoint
  • 34
    点赞
  • 146
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值