java22 作業 2018-12-12
#作業
##1/定義一個汽車類Vehicle,要求如下:[選做題]
2.1屬性包括:汽車品牌brand(String型別)、顏色color(String型別)
和速度speed(double型別),並且所有屬性為私有。
2.2至少提供一個有參的構造方法(要求品牌和顏色可以初始化為任意值,
但速度的初始值必須為0)。
2.3為私有屬性提供訪問器方法。注意:汽車品牌一旦初始化之後不能修改。
2.4定義一個一般方法run(),用列印語句描述汽車奔跑的功能
2.5定義測試類VehicleTest,在其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 abstract class Vehicle {
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 abstract void run();
}
//子類
public class Car extends Vehicle {
private int loader;
public Car(String brand, String color, int loader) {
super(brand, color);
this.loader = loader;
}
public void run() {
System.out.println(getColor() + "色" + getBrand() + "可載" + this.loader + "時速為" + getSpeed() + "米/小時");
}
}
//測試類
public class VehicleTest {
public static void main(String[] args) {
Vehicle car = new Car("Honda", "black", 2);
car.setColor("red");
car.setSpeed(300);
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 int area;
private int per;
private String color;
public Shape() {
super();
}
public Shape(String color) {
super();
this.color = color;
}
public abstract void getArea();// 計算面積
public abstract void getPer();// 計算周長
public abstract void showAll();// 輸出所有資訊
public void getColor() {
System.out.println("顏色為:"+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.Width = width;
this.height = height;
}
@Override
public void getArea() {
System.out.println("矩形的面積為:"+this.Width*this.height);
}
@Override
public void getPer() {
System.out.println("矩形的周長為:"+2*(this.Width+this.height));
}
@Override
public void showAll() {
System.out.println("長為:"+this.height+"寬為:"+this.Width);
this.getArea();
this.getPer();
getColor();
}
}
public class Circle extends Shape {
private double radius;
public Circle(double radius, String color) {
super(color);
this.radius = radius;
}
@Override
public void getArea() {
System.out.println("圓的面積為:" + this.radius * this.radius * 3.14);
}
@Override
public void getPer() {
System.out.println("圓的周長為:" + 2 * this.radius * 3.14);
}
@Override
public void showAll() {
System.out.println("半徑為:" + this.radius);
this.getArea();
this.getPer();
getColor();
}
}
//測試類
public class Delineation {
public static void main(String[] args) {
Shape f = new Rectangle(2, 4, "黃色");
Shape y = new Circle(2, "藍色");
f.showAll();
y.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 數組裡,並單元出陣列中每個員工當月的工資
//父類