第一題
建立一個汽車Auto類,包括輪胎個數,汽車顏色,車身重量、速度等成員變量。並通過不同的構造方法創建實例。至少要求: 汽車能夠加速,減速,停車。 再定義一個小汽車類Car,繼承Auto,並添加空調、CD等成員變量,覆蓋加速,減速的方法
第二題
寫一個Person類,有編號id,姓名name,職位job。
構造方法帶三個參數。
方法:
登陸login
注冊register
自我介紹talk
寫一個學生Student類繼承Person類,方法有:考試test
屬性有:學費money
寫一個老師Teacher類繼承Person類,
屬性有 工資salary
方法有:工作work
寫一個測試類TestPerson,測試學生和老師
學生:姓名-張三 職位-學生 學費-18000
老師:姓名-李四 職位-老師 工資-8000
注冊、登陸輸出就可以,自我介紹輸出屬性
第三題
定義一個父類:形狀類Shapes,里面有兩個方法,分別是求面積和周長。
定義一個子類:矩形Rectangle
定義一個子類:三角形 Triagle
定義一個子類:圓 Circle
定義一個測試類:傳入圓的半徑4 輸出周長和面積
傳入矩形的長和寬4,5 輸出周長和面積
傳入三角形三邊:3,4,5 輸出周長和面積
第一題: package homework; public class Auto { protected int gears; protected String color; protected double weight; protected double speed; public Auto() { this.gears=4; this.color="黃色"; this.weight=200; this.speed=50; } public Auto(int gears, String color, double weight, double speed) { super(); this.gears = gears; this.color = color; this.weight = weight; this.speed = speed; } public void speedUp() { speed+=5; System.out.println("加速至"+speed); } public void speedDown() { speed-=5; System.out.println("減速至"+speed); } public void stop() { speed=0; System.out.println("汽車停止,速度是"+speed); } } package homework; public class Car extends Auto{ protected String kongtiao; protected String CD; public Car() { super(); kongtiao="品牌1空調"; CD="CD1"; } public Car(int gears, String color, double weight, double speed,String kongtiao, String cD) { super(gears, color, weight, speed); this.kongtiao = kongtiao; CD = cD; } @Override public void speedUp(){ super.speed+=10; System.out.println("加速至"+speed); } @Override public void speedDown() { speed-=10; System.out.println("減速至"+speed); } public void information() { System.out.println("當前是"+color+"的"+gears+"輪"+color+"的重"+weight+"的配有"+kongtiao+"和"+CD+"的車子,當前時速為"+speed); } } package homework; public class TestAuto { public static void main(String[] args) { Car car=new Car(); car.information(); car.speedDown(); car.speedUp(); car.stop(); System.out.println(); Car car2=new Car(3, "白色", 300, 70,"品牌2空調", "CD2"); car2.information(); car2.speedDown(); car2.speedUp(); car.stop(); } } 第二題: package homework; public class Person { protected int id; protected String name; protected String job; public Person(int id, String name, String job) { this.id = id; this.name = name; this.job = job; } public void login() { System.out.println("登陸成功"); } public void register() { System.out.println("注冊成功"); } public void talk() { System.out.println("大家好!我是"+name+",編號是"+id+",職位是"+job); } } package homework; public class Student extends Person { protected double money; public Student(int id, String name, String job, double money) { super(id, name, job); this.money = money; } public void test() { System.out.println("我在考試"); } public void information() { System.out.println("姓名-"+name+"\t職位-"+job+"\t學費"+money); } } package homework; public class Teacher extends Person{ protected double salary; public Teacher(int id, String name, String job, double salary) { super(id, name, job); this.salary = salary; } public void work() { System.out.println("我在工作"); } public void information() { System.out.println("姓名-"+name+"\t職位-"+job+"\t工資"+salary); } } package homework; public class TestPerson { public static void main(String[] args) { Student stu=new Student(1, "張三", "學生", 18000); stu.login(); stu.register(); stu.talk(); stu.test(); stu.information(); System.out.println(); Teacher tea=new Teacher(2, "張老師", "老師", 8000); tea.login(); tea.register(); tea.talk(); tea.information(); } } 第三題: package homework; public class Shapes { public void area() { System.out.println("求面積"); } public void zhouChang() { System.out.println("求周長"); } } package homework; public class Rectangle extends Shapes { private int chang; private int kuan; public Rectangle(int chang, int kuan) { super(); this.chang = chang; this.kuan = kuan; } @Override public void area() { System.out.println("長方形面積是"+chang*kuan); } @Override public void zhouChang() { System.out.println("長方形周長是"+(chang+kuan)*2); } } package homework; public class Circle extends Shapes{ private int a; public Circle(int a) { super(); this.a = a; } @Override public void area() { System.out.println("圓形面積是"+a*a*Math.PI); } @Override public void zhouChang() { System.out.println("圓形周長是"+a*Math.PI*2); } } package homework; public class Triagle extends Shapes { private int a; private int b; private int c; public Triagle(int a, int b, int c) { super(); this.a = a; this.b = b; this.c = c; } public void area() { System.out.println("三角形面積是"+Math.sqrt((a+b+c)*(a+b-c)*(a+c-b)*(b+c-a))/4); } @Override public void zhouChang() { System.out.println("三角形周長是"+(a+b+c)); } } package homework; public class TestShapes { public static void main(String[] args) { Circle circle=new Circle(1); circle.area(); circle.zhouChang(); Rectangle rectangle=new Rectangle(2, 3); rectangle.area(); rectangle.zhouChang(); Triagle triagle=new Triagle(3, 4, 5); triagle.area(); triagle.zhouChang(); } }