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; //底薪
}