java代码学校中有教师和学生两类人,而在职博士生既是教师又是学生。设计两个接口StuInterface和TeaInterface。其中,StuInterface接口包括对学费的set()和get()方法,分别用于设置和获取学生的学费;TeaInterface接口包括对工资的set()和get()方法,分别用于设置和获取教师的工资。 定义一个博士生类Doctor,实现StuInterface接口和TeaInterface接口,它的成员变量有name(姓名)、sex(性别)、age(年龄)、fee(每学期学费)、pay(月工资),如果收入减去学费不足3000元,则输出“provide a loan”(需要贷款)信息。编写测试类,测试所创建的Doctor类。(说明:在Graduate中实现各个接口定义的抽象方法。)
时间: 2024-04-02 08:33:20 浏览: 133
好的,下面是对应的Java代码:
```
interface StuInterface {
void setFee(double fee);
double getFee();
}
interface TeaInterface {
void setPay(double pay);
double getPay();
}
class Doctor implements StuInterface, TeaInterface {
private String name;
private String sex;
private int age;
private double fee;
private double pay;
public Doctor(String name, String sex, int age, double fee, double pay) {
this.name = name;
this.sex = sex;
this.age = age;
this.fee = fee;
this.pay = pay;
}
@Override
public void setFee(double fee) {
this.fee = fee;
}
@Override
public double getFee() {
return fee;
}
@Override
public void setPay(double pay) {
this.pay = pay;
}
@Override
public double getPay() {
return pay;
}
public void checkLoanStatus() {
double income = pay * 12;
double expenses = fee * 2;
if (income - expenses < 3000) {
System.out.println("Provide a loan");
} else {
System.out.println("No need for a loan");
}
}
}
public class TestDoctor {
public static void main(String[] args) {
Doctor doctor = new Doctor("Tom", "Male", 30, 5000, 10000);
System.out.println("Doctor " + doctor.name + " has a fee of " + doctor.getFee() + " and a pay of " + doctor.getPay());
doctor.checkLoanStatus();
doctor.setFee(6000);
doctor.setPay(8000);
System.out.println("After update, Doctor " + doctor.name + " has a fee of " + doctor.getFee() + " and a pay of " + doctor.getPay());
doctor.checkLoanStatus();
}
}
```
测试结果如下:
```
Doctor Tom has a fee of 5000.0 and a pay of 10000.0
No need for a loan
After update, Doctor Tom has a fee of 6000.0 and a pay of 8000.0
Provide a loan
```
阅读全文