2018/12/12

2018/12/12

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=0;
	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(double speed){
		
	}
}
public class Car extends Vehicle{
	private int loader;
	
	public int getLoader() {
		return loader;
	}
	public void setLoader(int loader) {
		this.loader = loader;
	}
	public Car(String brand,String color,int loader){
		super(brand,color);
		this.setLoader(loader);
	}
	public void run(double speed){
		this.setSpeed(speed); 
		System.out.println(this.getColor()+"色的"+this.getBrand()
		+"速度是"+speed+"載人數:"+loader);
	}
}
Car car = new Car("honda","紅",2);
		car.run(100);

2.設計四個類,分別如下: [必做題]
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 int area;
	private int per;
	private String color;
	public String getColor() {
		return color;
	}
	public Shape() {
		
	}
	public Shape(String color) {
		this.color = color;
	}
	public abstract double getArea();
	public abstract double getPer();
	public abstract void showAll();			
}
public class Rectangle extends Shape{
	private int width;
	private int heigth;
	public Rectangle(){
		
	}
	public Rectangle(int width,int heigth,String color){
		super(color);
		this.width = width;
		this.heigth = heigth;
	}
	@Override
	public double getArea() {
		// TODO Auto-generated method stub
		
		return width*heigth;
	}
	@Override
	public double getPer() {
		// TODO Auto-generated method stub
		return 2*(width+heigth);
	}
	@Override
	public void showAll() {
		// TODO Auto-generated method stub
		System.out.println("面積:"+this.getArea()+"周長:"+this.getPer()
		+"顏色:"+this.getColor());
	}
}
public class Circle extends Shape{
	private int radius;
public Circle(){
		
	}
	public Circle(int radius,String color){
		super(color);
		this.radius = radius;
	}
	@Override
	public double getArea() {
		// TODO Auto-generated method stub
		
		return 3.14*radius*radius;
	}
	@Override
	public double getPer() {
		// TODO Auto-generated method stub
		return 2*radius*3.14;
	}
	@Override
	public void showAll() {
		// TODO Auto-generated method stub
		System.out.println("面積:"+this.getArea()+"周長:"+this.getPer()
		+"顏色:"+this.getColor());
	}
}
Rectangle r = new Rectangle(10,10,"紅");
		Circle c = new Circle(10,"黑");
		r.showAll();
		c.showAll();

3.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 數組裏,並單元出數組中每個員工當月的工資。

public  class ColaEmployee {
private String name;
	private int born;
	
	public ColaEmployee(String name, int born) {
		this.name = name;
		this.born = born;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getBorn() {
		return born;
	}

	public void setBorn(int born) {
		this.born = born;
	}

	public  int getSalary(int month){
		return 0;
	}
	public void show(){
	
	}
}
public class SalariedEmployee extends ColaEmployee {
	private int mpay;

	public SalariedEmployee(String name, int born, int mpay) {
		super(name, born);
		this.mpay = mpay;
	}
	public int getSalary(int month){
		if(month==this.getBorn()){
			mpay+=100;
		}
		return mpay;
	}
	public void show(){
		System.out.print("姓名:"+this.getName()+"生日:"+this.getBorn()
			+"月工資:");
	}
}
public class HourlyEmployee extends ColaEmployee {
	private int hpay;
	private int hour;
	public HourlyEmployee(String name, int born, int hpay, int hour) {
		super(name, born);
		this.hpay = hpay;
		this.hour = hour;
	}
	public int getSalary(int month){
		int money = 0;
		if(hour<=160&&hour>=0){
			money =hour*hpay;
		}else if(hour>160){
			money =  (int) ((int)160*hpay+(hour-160)*hpay*1.5);
		}
		if(month == this.getBorn()){
			money+=100;
		}
		return money;
	}
	public void show(){
		System.out.print("姓名:"+this.getName()+"生日:"+this.getBorn()
			+"每小時"+hpay+"元 "+"工時:"+hour+"工資:");
	
	}
}
public class SalesEmployee extends ColaEmployee{
	private int msale;
	private double rate;
	public SalesEmployee(String name, int born, int msale, double rate) {
		super(name, born);
		this.msale = msale;
		this.rate = rate;
	}
	public int getSalary(int month){
		int money = 0;
		money = (int) (msale*rate);
		if(month == this.getBorn()){
			money+=100;
		}
		return money;
	}
	public void show(){
		System.out.print("姓名:"+this.getName()+"生日:"+this.getBorn()+"月銷售額:"+msale+
				"提成:"+rate+"工資:");
	}
}
public class Company {
	public void check(ColaEmployee c,int month){
		
		System.out.println(c.getSalary(month));
	}
}
public static void main(String[] args) {
Company com = new Company();
		ColaEmployee[] co = new ColaEmployee[3];
		co[0] = new SalariedEmployee("張三",5,2000);
		co[1] = new HourlyEmployee("李四",4,15,150);
		co[2] = new SalesEmployee("王五",11,15000,0.1);
		for(ColaEmployee c:co){
			c.show();
			com.check(c, 3);
			System.out.println();
		}
	}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章