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人的轿车。

public class Vehicle {
private String brand;
private String color;
private double speed;
public String getBrand() {
	return brand;
}
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 Vehicle(String brand, String color) {
	
	this.brand = brand;
	this.color = color;
}
void run() {
	System.out.println("品牌为"+brand+"颜色为"+color+"速度为"+speed);
}

}

public class Car extends Vehicle {
private int loader;

public Car(String brand, String color, int loader) {
	super(brand, color);
	this.loader = loader;
}

@Override
	void run() {
		
		System.out.println("品牌为"+getBrand()+"颜色为"+getColor()+"速度为"+getSpeed()+"载人"+loader);
	}


}
public class Vehiclemain {

	public static void main(String[] args) {
		Vehicle vehicle=new Vehicle("benz","black");
		vehicle.setSpeed(10);
		vehicle.run();
		Vehicle car=new Car("honda","red",2);
		car.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 int per;
private String color;
public Shape() {
	
}
public Shape(String color) {
	this.color = color;
}
abstract double getArea();
abstract double getPer();
abstract void showAll();
String getColor() {
	return this.color;
}

}
public class Rectangle extends Shape {
private int width;
private int height;
public Rectangle() {
	
}
public Rectangle(int width,int height,String color) {
	super(color);
	this.height=height;
	this.width=width;
}
@Override
	double getPer() {
	return 2*(width+height);
	}
@Override
	double getArea() {
	return 	width*height;

	}
@Override
	void showAll() {
		System.out.println("宽度"+width+"高度"+height+"颜色"+getColor()+"周长"+getPer()+"面积"+getArea());
	}
}
public class Shapetest {

	public static void main(String[] args) {
		Rectangle shape1=new Rectangle(10,2,"red");
		Circle shape2=new Circle(1,"black");
		shape1.showAll();
        shape2.showAll();
	}

}
public class Circle extends Shape{
private int radius;
public Circle() {
	
}
public Circle(int radius,String color) {
	super(color);
	this.radius=radius;
}
@Override
	double getPer() {
		
		return 2*3.14*radius;
	}
@Override
	double getArea() {
		
		return 3.14*radius*radius;
	}
@Override
	void showAll() {
		System.out.println("半径"+radius+"颜色"+getColor()+"周长"+getPer()+"面积"+getArea());
		
	}
}


//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 数组里,并单元出数组中每个员工当月的工资。

import java.util.Scanner;

public class ColaEmployee {
	private String name;
	private int month;
	
	public ColaEmployee(String name, int month) {
		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;
	}

	double getSalary(int m){
		
		int add;
		if(month==m) {
			add=100;
		}else {
			add=0;
		}
		return add;
	}
}
public class SalariedEmployee extends ColaEmployee{
private int monthsal;

public SalariedEmployee(String name, int month, int monthsal) {
	super(name, month);
	this.monthsal = monthsal;
}

@Override
	double getSalary(int m) {
		
		return monthsal+super.getSalary(m);
	}
}
public class HourlyEmployee extends ColaEmployee {
private int time;
private int hoursal;

public HourlyEmployee(String name, int month, int time, int hoursal) {
	super(name, month);
	this.time = time;
	this.hoursal = hoursal;
}

@Override
	double getSalary(int m) {
		double sal;
		if(time<160) {
		sal=time*hoursal;
		}else {
		sal=(hoursal-160)*1.5*hoursal+160*hoursal;
		}
		return sal+super.getSalary(m);
	}
}
public class SalesEmployee extends ColaEmployee{
private int sale;
private double royrate;

public SalesEmployee(String name, int month, int sale, double royrate) {
	super(name, month);
	this.sale = sale;
	this.royrate = royrate;
}

@Override
	double getSalary(int m) {
		
		return sale*royrate+super.getSalary(m);
	}
}
import java.util.Scanner;

public class Company {
//	System.out.println("输入现在的月份");
	int m=new Scanner(System.in).nextInt();
void show(ColaEmployee empeed) {
	System.out.println(empeed.getName()+"的工资是"+empeed.getSalary(m));
}


}
public class CompanyTest {

	public static void main(String[] args) {
		ColaEmployee[] ColaEmployees=new ColaEmployee[3];
		ColaEmployees[0]=new SalariedEmployee("固定员工",12,3000);
		ColaEmployees[1]=new HourlyEmployee("小时工",3,70,15);
		ColaEmployees[2]=new SalesEmployee("销售员",5,1500,0.1);
		Company company=new Company();
		for(ColaEmployee i:ColaEmployees) {
			company.show(i);
		}
		
	}

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值