1.(面向對象基礎)寫一個Worker 類,并創建多個Worker 對象。
為Worker 類添加四個屬性, <1>int 類型的id,表示工人的編號; <2>String 類型的name,表示工人的姓名; <3>int 類型的age,表示工人的年齡; <4>double 類型的salary,表示工人的工資。 為Worker 類添加兩個構造方法, <1>公開無參構造方法; <2>接受四個參數的構造方法,四個參數分別為int、字符串、int 和double類型。 為Worker 類添加兩個work 方法,一個無參,打印工人姓名工作8小時 另一個帶整數參數,打印工人姓名以及工作的時間(為多少小時)。
package com.fxm.test;
public class Test1{public static void main(String[] args){Worker w = new Worker(10086,"xiaobai",18,1800);w.work();w.work(6);}
}
class Worker{int id;String name;int age;double salary;public Worker(){}public Worker(int id,String name,int age,double salary){this.id = id;this.name = name;this.age = age;this.salary = salary;}public void work(){System.out.println(name+" 工作8小時");}public void work(int hours){System.out.println(name+" 工作 "+hours+" 小時");}
}
2 ** 、完成如下代碼:
public class WorkerArrayTest{ public static void main(String[] args){ //1 、創建一個元素為Worker類型的數組長度為 3 //2 、創建 3 個Worker對象并保存到數組中 //3 、遍歷數組,并調用每一個Worker對象的無參work方法 //4 、調用oldWorker函數 } //寫一個函數oldWorker遍歷數組返回其中年紀最大的Worker對象 / / 形參為Worker[]類型 返回值為Worker類型 }
package com.fxm.test;
public class Test2{public static void main(String[] args){Worker[] ws = new Worker[3];ws[0] = new Worker("xiaobai",18);ws[1] = new Worker("xiaohei",20);ws[2] = new Worker("xiaohong",19);for(int i = 0; i < ws.length; i++){ws[i].work();}Worker max = oldWorker(ws);System.out.println(max.name+"-->"+max.age);}public static Worker oldWorker(Worker[] ws){Worker max = ws[0];for(int i = 0; i < ws.length; i++){if(max.age < ws[i].age){max = ws[i];} }return max;}
}
class Worker{String name;int age;public void work(){}public Worker(String name,int age){this.name = name;this.age = age;}
}
3 *** 、定義一個Company類,該類中有一個Worker[]類型屬性,保存多個Worker對 象。完成如下代碼: public class Company{ Worker[] ws = new Worker[16]; int count = 0; / / 記錄ws中有效的元素個數 //完成添加Worker方法 public void addWorker(Worker w){ //如果ws已滿,擴容 //添加 w //將count遞增 1 } //完成方法 返回每月公司需要支付的薪資 public double getAllSalaries(){ / / 遍歷數組,并計算總額 } //完成方法 判斷一個給定的 W o r k e r 對象是否屬于當前公司(通過 i d 判斷) public boolean contains(Worker w){ //遍歷數組進行判斷 } }
package com.fxm.test;
public class Test3{public static void main(String[] args){Company c = new Company();//添加員工Worker w1 = new Worker(1,"zhang3",18,18000.0);Worker w2 = new Worker(2,"li4",20,20000.0);Worker w3 = new Worker(3,"wang5",16,16000.0);c.addWorker(w1);c.addWorker(w2);double sum = c.getAllSalaries();System.out.println(sum);System.out.println(c.contains(w1));System.out.println(c.contains(w3));}
}
class Company{Worker[] ws = new Worker[16];int count = 0;public void addWorker(Worker w){if(count == ws.length){ws = java.util.Arrays.copyOf(ws,ws.length << 1);}ws[count] = w;count++;}public double getAllSalaries(){double sum = 0.0;for(int i = 0; i < count; i++){sum += ws[i].salary;}return sum;}public boolean contains(Worker w){for(int i = 0; i < count; i++){if(ws[i].id == w.id){return true;}}return false;}
}
4.(封裝)已知一個類Student 代碼如下:
class Student{String name;int age;String address;String zipCode;String mobile; }
把Student 的屬性都作為私有,并提供get/set 方法以及適當的構造方法。 為Student 類添加一個getPostAddress 方法,要求返回Student 對象的 地址和郵編。
package com.fxm.test;
public class Test4{public static void main(String[] args){Student s = new Student();s.setName("xiaobai");s.setAddress("nanyang");s.setZipCode("474250");s.study(6);System.out.println(s.getPostAddress());}
}
class Student{private String name;private int age;private String address;private String zipCode;private String mobile;public String getName(){return name;}public void setName(String newName){name = newName;}public int getAge(){return age;}public void setAge(int newAge){age = newAge;}public String getAddress(){return address;}public void setAddress(String newAddress){address = newAddress;}public String getZipCode(){return zipCode;}public void setZipCode(String newZipCode){zipCode = newZipCode;}public String getMobile(){return mobile;}public void setMobile(String newMobile){mobile = newMobile;}public void study(int hours){System.out.println(name+"??"+hours+"С?");}public String getPostAddress(){return address+zipCode;}
}
5、 *** .使用day07的Teacher類和Student類 類 Clazz //該類對象表示一個班級 屬性 Teacher t Student[] ss = new Student[10]?//保存多個Student對象 int count?//表示有效元素個數 方法: addStudent(Student s)?//添加Student對象 removeStudent(int id)?//根據id移除Student對象 updateStudent(Student s)?//更新Student對象 queryStudent(int id)?//根據id查詢Student對象
package com.fxm.test;
public class Test5{public static void main(String[] args){Clazz c = new Clazz();Student s1 = new Student(1,"xiao1hei",16,'男');Student s2 = new Student(2,"xiao2hei",16,'男');Student s3 = new Student(3,"xiao3hei",16,'男');Student s4 = new Student(4,"xiao4hei",16,'男');c.addStudent(s1);c.addStudent(s2);c.addStudent(s3);c.addStudent(s4);s4.age = 18;c.updateStudent(s4);Student s = c.queryStudent(4);System.out.println(s.name+"-->"+s.age);c.removeStudent(4);System.out.println(c.queryStudent(4));}
}
class Student{int id;String name;int age;char sex;public Student(int id,String name,int age,char sex){this.id = id;this.name = name;this.age = age;this.sex = sex;}public void study(){System.out.println(name+"學習8小時"); }public void study(int hours){System.out.println(name+"學習"+hours+"小時");}
}class Teacher{int id;String name;String course;public void teach(){System.out.println(name+"教"+course);}
}class Clazz{Student[] ss = new Student[10];Teacher t;int count;public void addStudent(Student s){if(ss.length == count){ss = java.util.Arrays.copyOf(ss,ss.length << 1);}ss[count] = s;count++;}public void removeStudent(int id){int index = -1;for(int i = 0; i < count; i++){if(ss[i].id == id){index = i;break;}}if(index == -1){return;}System.arraycopy(ss,index+1,ss,index,count-index-1);count--;}public boolean updateStudent(Student s){for(int i = 0; i < count; i++){if(ss[i].id == s.id){ss[i] = s;return true;}}return false;}public Student queryStudent(int id){for(int i = 0; i < count; i++){if(ss[i].id == id){return ss[i];}}return null;}
}
6、按類圖要求寫出所有的類 Instrument表示樂器類,play方法打印輸出”彈奏樂器” Wind繼承Instrument,重寫play方法,打印輸出”彈奏Wind” 并提供另外一個playWind方法,打印輸出”調用wind的play2方法” Brass繼承Instrument,重寫play方法,打印輸出”彈奏Brass” 并提供另外一個playBrass方法,打印輸出”調用Brass的play2方法”
package com.fxm.test;
public class Test6{public static void main(String args[]){Wind w = new Wind();w.play();w.playWind();Brass b = new Brass();b.play();b.playBrass();}
}
class Instrument{public void play(){System.out.println(" 彈奏樂器");}
}
class Wind extends Instrument{public void play(){System.out.println(" 彈奏Wind");}public void playWind(){System.out.println(" 調用Wind的play2方法");}
}
class Brass extends Instrument{public void play(){System.out.println(" 彈奏Brass");}public void playBrass(){System.out.println(" 調用Brass的play2方法");}
}
7.(封裝、繼承)有以下幾個類,根據下面的繼承關系,用 Java 代碼實現。 a) Circle 類(圓形) ,屬性:半徑;方法:求周長、求面積 b) Rect 類(矩形) ,屬性:長、寬;方法:求周長、求面積 c) Square 類(正方形) ,屬性:邊長;方法:求周長、求面積 提示: 1) 三個子類各自有其屬性,圓:radius半徑 矩形:length長 width寬 正方形: length邊長; 2) 父類中的周長(girth)和面積(area)的方法統一返回0.0,在三個子類中重寫兩個方法 圓 周長公式:3.142r 面積公式:3.14rr 矩形 周長公式:(length+width)2 面積公式 lengthwidth 正方形:周長公式 length4 面積公式 lengthlength
package com.fxm.test;
public class Test7{public static void main(String args[]){Circle c = new Circle();c.area();c.girth();}
}
class Shape{public double area(){ return 0.0;}public double girth(){return 0.0;}
}
class Circle extends Shape{double radius;public Circle(){}public Circle(double radius){this.radius = radius;}public void setRadius(double radius){this.radius = radius;}public double getRadius(){return radius;}public double area(){return (3.14*radius*radius);}public double girth(){return (6.28*radius);}
}
class Rect extends Shape{private double length;private double width;public void Rect(){}public void Rect(double length,double width){this.length = length;this.width = width;}public void setLength(double length){this.length = length;}public void setWidth(double width){this.width = width;}public double getLength(){return length;}public double getWidth(){return width;}public double area(){return (length*width);}public double girth(){return ((length+width)*2);}
}
class Square extends Shape{private double length;public void setLength(double length){this.length = length;}public double getLength(){return length;}public double area(){return (length*length);}public double girth(){return (length*4);}
}
8.(封裝、繼承、super)某公司的雇員分為以下若干類:
Employee:這是所有員工總的父類,屬性:員工的姓名,員工的生日月份。方法:getSalary(intmonth) 根據參數月份來確定工資,如果該月員工過生日,則公司會額外獎勵100 元。
SalariedEmployee:Employee 的子類,拿固定工資的員工。屬性:月薪
HourlyEmployee: Employee 的子類, 按小時拿工資的員工, 每月工作超出160 小時的部分按照1.5 倍工資發放。屬性:每小時的工資、每月工作的小時數
SalesEmployee:Employee 的子類,銷售人員,工資由月銷售額和提成率決定。 屬性:月銷售額、提成率
BasePlusSalesEmployee:SalesEmployee 的子類,有固定底薪的銷售人員,工資 由底薪加上銷售提成部分。屬性:底薪。
根據要求創建 SalariedEmployee 、 HourlyEmployees 、SaleEmployee 和 BasePlusSalesEmployee四個類的對象各一個,并計算某個月這四個對象的工資。 注意:要求把每個類都做成完全封裝,不允許非私有化屬性。
package com.fxm.test;public class Employee {public Employee(String name, int month){this.name = name;this.month = month;}public float get_Salary(int month){//如果月份為員工生日月份,則增加100if(month == this.month){return 100;}else{return 0;}}private String name;private int month;public static void main(String[] args) {Employee a[] = new Employee[4];a[0] = new SalariedEmployee("A", 2, 1000);a[1] = new HourlyEmployee("B", 3, 2000, 200);a[2] = new SalesEmployee("C", 4, 50000, (float) 0.1);a[3] = new BasedPlusSalesEmployee("D", 5, 50000, (float) 0.1, 1000);System.out.println("A的工資為" + a[0].get_Salary(2));System.out.println("B的工資為" + a[1].get_Salary(2));System.out.println("C的工資為" + a[2].get_Salary(2));System.out.println("D的工資為" + a[3].get_Salary(2));}
}class SalariedEmployee extends Employee{public SalariedEmployee(String name, int month, float salary){super(name, month);this.salary = salary; }@Overridepublic float get_Salary(int month){return salary + super.get_Salary(month); }private float salary;
}
class HourlyEmployee extends Employee
{public HourlyEmployee(String name, int month, float salary, int hour){super(name, month);this.salary = salary;this.hour = hour;}@Overridepublic float get_Salary(int month){//大于160小時的情況if (hour > 160){return (float) (salary * 160 + (hour - 160) * salary * 1.5 + super.get_Salary(month));}else{return salary * hour + super.get_Salary(month);}}private float salary; //每小時工資private int hour; //每月工作的小時數
}//銷售人員
class SalesEmployee extends Employee
{public SalesEmployee(String name, int month, float sale, float bonus){super(name, month);this.sale = sale;this.bonus = bonus;}@Overridepublic float get_Salary(int month){return sale * bonus + super.get_Salary(month);}private float sale; //銷售額private float bonus; //提成率
}//有固定底薪的銷售人員
class BasedPlusSalesEmployee extends SalesEmployee
{public BasedPlusSalesEmployee(String name, int month, float sale,float bonus, float baseSalary){super(name, month, sale, bonus);this.baseSalary = baseSalary;}@Overridepublic float get_Salary(int month){return baseSalary + super.get_Salary(month);}private float baseSalary; //底薪
}
總結
以上是生活随笔 為你收集整理的Java 习题(面向对象) 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。