2018-12-12作业

2018-12-12作业

2、设计2个类,要求如下: [必做题] 2.1 定义一个汽车类Vehicle, 2.1.1 属性包括:汽车品牌brand(String类型)、颜色color(String类型)和速度speed(double类型)。 2.1.2 至少提供一个有参的构造方法(要求品牌和颜色可以初始化为任意值,但速度的初始值必须为0)。 2.1.3 为属性提供访问器方法。注意:汽车品牌一旦初始化之后不能修改。 2.1.4 定义一个一般方法run(),用打印语句描述汽车奔跑的功能 2.1.5 在main方法中创建一个品牌为“benz”、颜色为“black”的汽车。 2.2 定义一个Vehicle类的子类轿车类Car,要求如下: 2.2.1 轿车有自己的属性载人数loader(int 类型)。 2.2.2 提供该类初始化属性的构造方法。 2.2.3 重新定义run(),用打印语句描述轿车奔跑的功能。 2.2.4 在main方法中创建一个品牌为“Honda”、颜色为“red”,载人数为2人的轿车。

private String brand;
       private String color;
       private double speed;
		public Vehicle(String brand, String color) {
			super();
			this.brand = brand;
			this.color = color;
		}
		public String getColor() {
			return color;
		}
		public void setColor(String color) {
			this.color = color;
		}
		public double getSpeed() {
			return speed;
		}
		public void setSpeed(double speed) {
			this.speed = speed;
		}
		public String getBrand() {
			return brand;
		}
		public void run(){
			System.out.println(color+"色"+brand+"汽车的速度为:"+speed);
		}
public class Car extends Vehicle{
	int loader;

	public Car(String brand, String color) {
		super(brand, color);
		
	}

	public Car(String brand, String color, int loader) {
		super(brand, color);
		this.loader = loader;
	}
	public void run(){
		System.out.println(getColor()+"色"+getBrand()+"轿车的速度为:"+getSpeed());
	}
public static void main(String[] args) {
	  Vehicle vehicle=new Vehicle("benz","black");
	  vehicle.setSpeed(100.0);
	  Car car=new Car("Honda","red",2);
	  car.setSpeed(120.0);
	  car.run();
	  vehicle.run();
	}

3、设计四个类,分别如下: [必做题] 3.1 设计Shape表示图形类,有面积属性area、周长属性per,颜色属性color,有两个构造方法(一个是默认的、一个是为颜色赋值的) ,还有3个抽象方法,分别是:getArea计算面积、getPer计算周长、showAll输出所有信息,还有一个求颜色的方法getColor。 3.2 设计 2个子类: 3.2.1 Rectangle表示矩形类,增加两个属性,Width表示长度、height表示宽度,重写getPer、getArea和showAll 三个方法,另外又增加一个构造方法(一个是默认的、一个是为高度、宽度、颜色赋值的)。 3.2.2 Circle表示圆类,增加1个属性,radius表示半径,重写getPer、getArea和showAll三个方法, 另外又增加两个构造方法(为半径、颜色赋值的)。 3.3 测试类中,在main方法中,声明创建每个子类的对象,并调用2个子类的showAll方法。

public abstract class Shape {
	private double area;
	private double per;
	private String color;
	public Shape() {
		
	}
	public Shape(double area, double per, String color) {
		super();
		this.area = area;
		this.per = per;
		this.color = color;
	}
	public abstract double getArea();
	public abstract double getPer();
	public abstract void showAll();
	public String getColor() {
		return color;
	}
public  class Rectangle extends Shape{
	private double width;
	private double height;
	private double area;
	private double per;
	private String color;
	public Rectangle(double width, double height) {
		super();
		this.width = width;
		this.height = height;
	}
	public Rectangle() {
		super();
	}
	public Rectangle(double width, double height, String color) {
		super();
		this.width = width;
		this.height = height;
		this.color = color;
	}
	@Override
	public double getPer() {
		per=2*(width+height);
		return per;
	}
	@Override
	public double getArea() {
		area=width*height;
		return area;
	}
	@Override
	public void showAll() {
		System.out.println("该矩形为"+color+"色"+"长度为:"+height+"宽度为:"+width+"周长为:"+getPer()+"面积为:"+ getArea());
	}
public  class Circle extends Shape{
	private double area;
	private double per;
	private String color;
	private double radius;
	@Override
	public double getPer() {
		per=3.14*2*radius;
		return per;
	}
	@Override
	public double getArea() {
		area=3.14*radius*radius;
		return area;
	}
	@Override
	public void showAll() {
		System.out.println("该圆为"+color+"色"+"半径为:"+radius+"周长为:"+getPer()+"面积为:"+getArea());
	}
	public Circle() {
		super();
	}
	public Circle(String color, double radius) {
		super();
		this.color = color;
		this.radius = radius;
	}
public static void main(String[] args) {
		Rectangle rectangle=new Rectangle(4,5,"红");
		Circle circle=new Circle("黄",2);
		rectangle.showAll();
		circle.showAll();
	}

4、 Cola公司的雇员分为以下若干类:(知识点:多态) [必做题] 4.1 ColaEmployee :这是所有员工总的父类,属性:员工的姓名,员工的生日月份。方法:getSalary(int month) 根据参数月份来确定工资,如果该月员工过生日,则公司会额外奖励100 元。 4.2 SalariedEmployee : ColaEmployee 的子类,拿固定工资的员工。属性:月薪 4.3 HourlyEmployee :ColaEmployee 的子类,按小时拿工资的员工,每月工作超出160 小时的部分按照1.5 倍工资发放。属性:每小时的工资、每月工作的小时数 4.4 SalesEmployee :ColaEmployee 的子类,销售人员,工资由月销售额和提成率决定。属性:月销售额、提成率 4.5 定义一个类Company,在该类中写一个方法,调用该方法可以打印出某月某个员工的工资数额,写一个测试类TestCompany 在main方法,把若干各种类型的员工放在一个ColaEmployee 数组里,并单元出数组中每个员工当月的工资。

 private String name;
 private int month;
public double getSalary(int month){
	 if(month==this.month){
		 return 100;
	 }else{
		 return 0;
	 }
}
public ColaEmployee(String name, int month) {
	super();
	this.name = name;
	this.month = month;
}
public String getName() {
	return name;
}
public void setName(String name) {
	this.name = name;
}
public int getMonth() {
	return month;
}
public void setMonth(int month) {
	this.month = month;
}
public class SalariedEmployee extends ColaEmployee{
private double salary;
public SalariedEmployee(String name, int month, int salary) {
	super(name, month);
	this.salary = salary;
}
@Override
public double getSalary(int month) {
		return salary+super.getSalary(month);
	}
public double getSalary() {
	return salary;
}
public void setSalary(double salary) {
	this.salary = salary;
}
}
public class HourlyEmployee extends ColaEmployee {
private double salaryh;
   private int hour;
public HourlyEmployee(String name, int month, double salaryh, int hour) {
	super(name, month);
	this.salaryh = salaryh;
	this.hour = hour;
}
public double getSalaryh() {
	return salaryh;
}
public void setSalaryh(double salaryh) {
	this.salaryh = salaryh;
}
public int getHour() {
	return hour;
}
public void setHour(int hour) {
	this.hour = hour;
}
@Override
	public double getSalary(int month) {
	if(hour>160){
		return salaryh*160+salaryh*(1.5*(hour-160))+super.getSalary(month);
	}else{
		return salaryh*160+super.getSalary(month);
	}		
	}
}
public class SalesEmployee extends ColaEmployee {
	private double sale;
	private double rate;
	
	public SalesEmployee(String name, int month, double sale, double rate) {
		super(name, month);
		this.sale = sale;
		this.rate = rate;
	}
	public double getSale() {
		return sale;
	}
	public void setSale(double sale) {
		this.sale = sale;
	}
	public double getRate() {
		return rate;
	}
	public void setRate(double rate) {
		this.rate = rate;
	}
	@Override
 public double getSalary(int month) {	
		return sale*rate+ super.getSalary(month);
	}
}
public class Company {
	private int month;
	public Company(int month, String name, double salary) {
		super();
		this.month = month;
		this.name = name;
		this.salary = salary;
	}
	private String name;
	private double salary;
	public int getMonth() {
		return month;
	}
	public void setMonth(int month) {
		this.month = month;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getSalary() {
		return salary;
	}
	public void setSalary(double salary) {
		this.salary = salary;
	}
	
	public static  void show(ColaEmployee emp) {
		System.out.println(emp.getName()+emp.getMonth()+"月份"+"的工资数额为:"+emp.getSalary(2));
	}
}
public class TestCompany {
	public static void main(String[] args) {
		
        ColaEmployee a[] = new ColaEmployee[3];
        ColaEmployee as;
		a[0] = new SalariedEmployee("小红", 2, 5000);
		a[1] = new HourlyEmployee("小黄", 3, 150, 200);
		a[2] = new SalesEmployee("小白", 4, 68800, (double) 0.1);
		
		 for ( ColaEmployee emp:a){
			 Company.show(emp);
		 }
	}
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
《Java程序设计》课程实验指导书程序代码(答案)(实验四:java继承与多态),个人原创,仅供参考与交流。 希望多多交流,共同进步! 实验四 java继承与多态 一、实验目的: 掌握继承、多态的概念与实现方法; 掌握包和接口的定义和使用方法; 了解JAVA语言实现多继承的途径; 二、实验内容: 1.分别编写两个Point2D,Point3D来表示二维空间和三维空间的点,使之满足下列要求: (1) Point2D有两个整型成员变量x, y (分别为二维空间的X,Y方向坐标),Point2D的构造方法要实现对其成员变量x, y的初始化。 (2)Point2D有一个void型成员方法offset(int a, int b),它可以实现Point2D的平移。 (3)Point3D是Point2D的直接子,它有有三个整型成员变量x,y,z (分别为三维空间的X,Y,Z方向坐标),Point3D有两个构造方法:Point3D(int x,int y,int z)和Point3D(Point2D p,int z),两者均可实现对Point3D的成员变量x, y,z的初始化。 (4)Point3D有一个void型成员方法offset(int a, int b,int c),该方法可以实现Point3D的平移。 (5)在Point3D中的主函数main()中实例化两个Point2D的对象p2d1,p2d2,打印出它们之间的距离,再实例化两个Point2D的对象p3d1,p3d2,打印出他们之间的距离。 2.定义抽象Shape,抽象方法为showArea(),求出面积并显示,定义矩形Rectangle,正方形Square,圆 Circle,根据各自的属性,用showArea方法求出各自的面积,在main方法中构造3个对象,调用showArea方法。 定义接口DiagArea,其中包含方法double getDiagonal()求对角线长, double getArea()求面积,定义一个矩形,实现此接口,并自行扩充成员变量和方法,定义一个正方形继承矩形(如矩形有长w和宽h,正方形有边x,并有相应的构造函数,有一个方法中一次直接显示边长、面积和对角线长),在另一类中的主方法里使用测试该。 三、实验要求: 1. 能实现的继承关系; 2. 用多种方法创建各个的对象; 3. 程序应包括各个被调用方法的执行结果的显示。 4. 写出实验报告。要求记录编译和执行Java程序当中的系统错误信息提示,并给出解决办法。(附运行界面、源代码)。 四、实验步骤: 1.(第1题)定义Point2D,及定义它的属性和方法; 定义子Point3D,及定义它的属性和方法;在Point3D中的主函数main()中实例化两个Point2D的对象,并通过这两个对象调用它们的属性和方法,输出方法执行结果。 2.(第2题)定义抽象Shape,抽象方法为showArea(),再定义矩形Rectangle,正方形Square,圆 Circle,和各自的属性。定义主、主方法,在main方法中构造3个对象,调用showArea方法;定义接口DiagArea,其中包含方法double getDiagonal(),在主main方法中输出方法执行结果。 五、自做作实验 1.定义一个描述人的基本,该包括人的性别和出生日期两个数据成员,以及设置和获取这些属性值的方法成员;再定义一个大学生,使大学生具有人的所有属性外,还具有姓名、学号,大学入学成绩,籍贯属性以及设置和获取这些属性值的方法成员;编写完整的程序,完成一个具有班级学生信息管理功能的程序。 2创建一个接口Shape,其中有抽象方法area,Circle 、Rectangle实现area方法计算其面积并返回。又有Star实现Shape的area方法,其返回值是0,Star另有一返回值boolean型方法isStar;在main方法里创建一个Vector,根据随机数的不同向其中加入Shape的不同子对象(如是1,生成Circle对象;如是2,生成Rectangle对象;如是3,生成Star对象)。然后将Vector中元素依次取出,判断其是否为Star。如是返回其是个Star。否则返回其面积。 3..学校中有老师和学生两人,而在职研究生既是老师又是学生,对学生的管理和对教师的管理在他们身上都有体现。 1)设计两个信息管理接口StudentManageInterface和TeacherManageInterface。其中,StudentInterface接口包括setFee()方法和getFee()方法,分别用于设置和获取学生的学费;TeacherInterface接口包括setPay()方法和getPay()方法,分别用于设置和获取教师的工资 2) 定义一个研究生Graduate,实现StudentInterface接口和TeacherInterface接口,它定义的成员变量有name(姓名)、sex(性别)、age(年龄)、fee(每学期学费)、pay(月工资)。 3) 创建一个姓名为“zhangsan”的研究生,统计他的年收入和学费,如果收入减去学费不足2000元,则输出“provide a loan”(需要贷款)信息。 提示: 1)定义两个接口,分别在其中申明两个方法。 2)定义主Graduate,实现这两个接口。 3)定义主的成员变量,和构造方法。 4)给出四个接口方法的实现。 5)给出一个计算是否需要贷款的方法,在里面统计年收入和学费,并输出是否需要贷款的信息。 6)写main方法。在其中创建一个姓名为“zhangsan”的研究生,调用计算是否需要贷款的方法。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值