1、实现一个名为Person的类和它的子类Employee,Employee有两个子类Faculty和Staff。具体要求如下:
(1)Person类中的属性有:姓名name(String类型),地址address(String类型),电话号码telphone(String类型)和电子邮件地址email(String类型);
(2)Employee类中的属性有:办公室office(String类型),工资wage(double类型),受雇日期hiredate(String类型);
(3)Faculty类中的属性有:学位degree(String类型),级别level(String类型);
(4)Staff类中的属性有:职务称号duty(String类型)。
public classPerson {privateString name;privateString address;privateString telphone;privateString email;publicString getName() {returnname;
}public voidsetName(String name) {this.name =name;
}publicString getAddress() {returnaddress;
}public voidsetAddress(String address) {this.address =address;
}publicString getTelphone() {returntelphone;
}public voidsetTelphone(String telphone) {this.telphone =telphone;
}publicString getEmail() {returnemail;
}public voidsetEmail(String email) {this.email =email;
}
}
public class Employee extendsPerson {privateString office;private doublewage;privateString hiredate;publicString getOffice() {returnoffice;
}public voidsetOffice(String office) {this.office =office;
}public doublegetWage() {returnwage;
}public void setWage(doublewage) {this.wage =wage;
}publicString getHiredate() {returnhiredate;
}public voidsetHiredate(String hiredate) {this.hiredate =hiredate;
}
}
public class Faculty extendsEmployee{privateString degree;privateString level;publicString getDegree() {returndegree;
}public voidsetDegree(String degree) {this.degree =degree;
}publicString getLevel() {returnlevel;
}public voidsetLevel(String level) {this.level =level;
}
}
public class Staff extendsEmployee {privateString duty;publicString getDuty() {returnduty;
}public voidsetDuty(String duty) {this.duty =duty;
}
}
测试:
public classF {public static voidmain(String[] args) {
Faculty a=newFaculty();
a.setName("张三");
a.setTelphone("123456788");
a.setDegree("本科");
a.setLevel("一级");
a.setEmail("12345877@qq.com");
System.out.println("员工姓名:"+a.getName()+" 电话"+a.getTelphone()+
" 学位"+a.getDegree()+" 级别"+a.getLevel()+" 邮箱"+a.getEmail());
Employee b=newEmployee();
b.setName("张三");
b.setTelphone("123456788");
b.setEmail("12345877@qq.com");
b.setOffice("A座407");
b.setWage(20000);
System.out.println("员工姓名:"+b.getName()+" 电话"+b.getTelphone()+
" 邮箱"+a.getEmail()+" 办公室"+b.getOffice()+" 工资"+b.getWage());
}
}
2、编写一个Car类,具有String类型的属性品牌,具有功能drive;定义其子类Aodi和Benchi,具有属性:价格、型号;具有功能:变速;定义主类E,在其main方法中分别创建Aodi和Benchi的对象并测试对象的特性。
public classCar {private String pinpa; //品牌属性
private doublesudu;publicString getPinpa() {returnpinpa;
}public voidsetPinpa(String pinpa) {this.pinpa =pinpa;
}public doublegetSudu() {returnsudu;
}public void setSudu(doublesudu) {this.sudu =sudu;
}publicString drive(){return "汽车已启动!";
}
}
public class Aodi extendsCar{private doublejiage;privateString xinghao;public doublegetJiage() {returnjiage;
}public void setJiage(doublejiage) {this.jiage =jiage;
}publicString getXinghao() {returnxinghao;
}public voidsetXinghao(String xinghao) {this.xinghao =xinghao;
}public doublebiansu(){double sudu=super.getSudu()+10;returnsudu;
}
}
public class Benchi extendsAodi{public doublebiansu(){double sudu=super.getSudu()+20;returnsudu;
}
}
测试:
public classE {public static voidmain(String[] args){
Aodi aodi=newAodi();
aodi.setJiage(500000);
aodi.setXinghao("奥迪A6");
System.out.println("奥迪车的型号是:"+aodi.getXinghao()+" 价格是:"+aodi.getJiage());
System.out.println(aodi.drive());
System.out.println("启动后的速度为:"+aodi.biansu());
Benchi benchi=newBenchi();
benchi.setJiage(800000);
benchi.setXinghao("奔驰2000");
System.out.println("奔驰车的型号是:"+benchi.getXinghao()+" 价格是:"+benchi.getJiage());
System.out.println(benchi.drive());
System.out.println("启动后的速度为:"+benchi.biansu());
}
}
3、按要求编写一个Java应用程序:
(1)编写一个矩形类Rect,包含:
两个属性:矩形的宽width;矩形的高height。
两个构造方法:
1.一个带有两个参数的构造方法,用于将width和height属性初化;
2.一个不带参数的构造方法,将矩形初始化为宽和高都为10。
两个方法:
求矩形面积的方法area()
求矩形周长的方法perimeter()
(2)通过继承Rect类编写一个具有确定位置的矩形类PlainRect,其确定位置用
矩形的左上角坐标来标识,包含:
添加两个属性:矩形左上角坐标startX和startY。
两个构造方法:
带4个参数的构造方法,用于对startX、startY、width和height属性初始化;
不带参数的构造方法,将矩形初始化为左上角坐标、长和宽都为0的矩形;
添加一个方法:
判断某个点是否在矩形内部的方法isInside(double x,double y)。如在矩形内,返回true, 否则,返回false。
提示:点在矩形类是指满足条件:x>=startX&&x<=(startX+width)&&y=(startY-height)
(3)编写PlainRect类的测试程序创建一个左上角坐标为(10,10),长为20,宽为10的矩形对象;计算并打印输出矩形的面积和周长;判断点(25.5,13)是否在矩形内,并打印输出相关信息。
public classRect {//声明属性
private doublewidth;private doubleheight;//带参数构造方法
public Rect(double width,doubleheight){this.width=width;this.height=height;
}//无参的构造方法
publicRect(){this.width=10;this.height=10;
}//成员方法 周长
public doubleperimeter(){doubleperimeter;
perimeter=(width+height)*2;returnperimeter;
}//成员方法 面积
public doublearea(){doublearea;
area=width*height;returnarea;
}//getter/setter方法
public doublegetWidth() {returnwidth;
}public void setWidth(doublewidth) {this.width =width;
}public doublegetHeight() {returnheight;
}public void setHeight(doubleheight) {this.height =height;
}
}public class PlainRect extendsRect {//声明属性
private intstartX;private intstartY;//含参构造方法
public PlainRect(int startX,int startY,double width,doubleheight){super(width,height); //调用父类构造方法
this.startX=startX;this.startY=//不含参构造方法
publicPlainRect(){
super(0,0);this.startX=0;this.startY=0;
}//成员方法 判断点与矩形位置
public boolean isInside(double x,doubley){returnx>=startX&&x<=(startX+getWidth())&&y=(startY-getHeight());}
}
public classTestPlainRect {public static voidmain(String[] args) {//创建矩形
PlainRect rect =new PlainRect(10,10,20,10);//打印输出
System.out.println("长为20,宽为10的矩形的面积是:"+rect.area());
System.out.println("长为20,宽为10的矩形的周长是:"+rect.perimeter());if(rect.isInside(25.5, 13)==true){
System.out.println("点(25.5,13)在矩形内");
}else{
System.out.println("点(25.5,13)不在矩形内");
}
}
}