继承作业题

3、Engine(引擎),具有power(功率,整数)属性,相对应的setter和getter方法,work()方法:输出"xx功率的发 动机在运转"。Car(Engine轿车),具有Engine属性,相对应的setter和getter方法,run()方法,在方法中判断 Engine对象是否为null,选择输出"发动机发动不了"或者"xx功率的发动机在运转,汽车在跑"。Benz(奔驰), 继承Car类,重写run()方法Driver(驾驶员),具有属性name(姓名),相对应的setter和getter方 法,driveCar(Benz benz)方法,在方法中输出“xxx 在开车”,并调用benz的run()方法。

package jicheng;

public class Engine {
    private int power;

    public void setPower(int power) {
        this.power = power;
    }

    public int getPower() {
        return power;
    }
    public void work(){
        System.out.println(power + "w的发动机在运转");
    }
}
package jicheng;

public class Car extends Engine{
    private Engine engine;

    public Engine getEngine() {
        return engine;
    }

    public void setEngine(Engine engine) {
        this.engine = engine;
    }
    public void run(){
       if(engine==null){
           System.out.println("发动机发动不了");
       }else{
           engine.work();
           System.out.println("汽车在跑");
       }
    }

}
package jicheng;

public class Benz extends Car{
    public void run(){
        if(getEngine()==null){
            System.out.println("发动机故障了,奔驰汽车不能继续开了");
        }else {
            getEngine().work();
            System.out.println("奔驰汽车在继续走");
        }
    }
}
package jicheng;

public class BenzTest {
    public static void main(String[] args) {
        Driver driver = new Driver();
        driver.setName("张三");
        Benz benz = new Benz();
        Engine engine = new Engine();
        engine.setPower(1000);
        benz.setEngine(engine);
        driver.driveCar(benz);
    }
}

 4、写一个名为 Account 的类模拟账户。该类包括的属性:账号 id,余额 balance,年利率 annualInterestRate;包含的方法:访问器方法(getter 和setter 方法),返回月利率的方法 getMonthlyInterest(),取款方法 withdraw(),存款方法deposit()。创建 Account 类的一个子类 CheckAccount 代表可透支的账户,该账户中定义一个属性overdraft 代表可透支限额。在 CheckAccount 类中重写 withdraw 方法,其算法如下:如果(取款金额账户余额),计算需要透支的额度判断可透支额 overdraft 是否足够支付本次透支需要,如 果可以将账户余额修改为 0,冲减可透支金额如果不可以提示用户超过可透支额的限额。

package jicheng.l3;

public class Account {
    private  int id;
    private  double balance;
    private  double annualInterestRate;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    public double getAnnualInterestRate() {
        return annualInterestRate;
    }

    public void setAnnualInterestRate(double annualInterestRate) {
        this.annualInterestRate = annualInterestRate;
    }
    public double getMonthlyInterest(){
        return annualInterestRate/12;
    }
    public void withdraw(int money){
        if(balance>=money&&money>=0){
            balance -=money;
            System.out.println("取钱成功,账户余额为:"+ balance);
        }
    }
    public void deposit(int money){
        if(money>=0){
            balance +=money;
            System.out.println("存钱成功,账户余额为:"+balance);
        }
    }
}
package jicheng.l3;

public class CheckAccount extends Account{
    private int overdraft;

    public CheckAccount(double balance) {
        super.setBalance(balance);
    }

    public int getOverdraft() {
        return overdraft;
    }

    public void setOverdraft(int overdraft) {
        this.overdraft = overdraft;
    }
    public void withdraw(int money){
        if(money>=0){
            if(money<=getBalance()){
            super.withdraw(money);
            }else{
                double min = money-getBalance();
                if(min<=overdraft){
                    setBalance(0);
                    overdraft-=min;
                }else {
                    System.out.println("透支额度已超出,可透支额度为:"+(getBalance()+overdraft));
                }
            }
        }
    }
}
package jicheng.l3;

public class AccountTest {
    public static void main(String[] args) {
        CheckAccount checkAccount = new CheckAccount(50000);
        checkAccount.setOverdraft(20000);
        checkAccount.withdraw(20000);
        checkAccount.withdraw(80000);
        checkAccount.deposit(10000);
    }
}

 8、编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数wheels和车重weight。 小车类Car是Vehicle的子类,其中包含的属性有载人数loader。卡车类Truck是Car类的子类,其中包含 的属性有载重量payload。

package jicheng.l8;

public class Vehicle {
    private  int wheels;
    private  double weight;

    public Vehicle(int wheels, double weight) {
        this.weight = weight;
        this.wheels = wheels;
    }

    public int getWheels() {
        return wheels;
    }

    public void setWheels(int wheels) {
        this.wheels = wheels;
    }

    public double getWeight() {
        return weight;
    }

    public void setWeight(double weight) {
        this.weight = weight;
    }
    public void show(){
        System.out.println("车轮数为:"+wheels+"\t"+"车的自重为:"+weight+"t");
    }
}
package jicheng.l8;

public class Car extends Vehicle{
        private  int loader;

    public int getLoader() {
        return loader;
    }

    public void setLoader(int loader) {
        this.loader = loader;
    }

    public Car(int loader ,int wheels,double weight){
        super(wheels,weight);
        this.loader = loader;
    }
    public void show(){
        System.out.print("该车荷载人数为:"+loader+"\t");
        super.show();
    }
}
package jicheng.l8;

public class Truck extends Car{
    private double payload;

    public Truck(int loader, int wheels, double weight,double payload) {
        super(loader, wheels, weight);
        this.payload = payload;
    }

    public double getPayload() {
        return payload;
    }

    public void setPayload(double payload) {
        this.payload = payload;
    }
    public void show(){
        System.out.print("该车的载重为:"+payload+"t\t");
        super.show();
    }

}
package jicheng.l8;

public class CarTest {
    public static void main(String[] args) {
        Car car = new Car(5,4,2);
        car.show();
        Truck truck = new Truck(3,12,6,40);
        truck.show();
    }
}

 9、定义员工类Employee,包含姓名、工号和工资,包含计算奖金方法bonus,普通员工和经理都 是员工,计算奖金的方法为工资*奖金系数,普通员工的奖金系数为1.5(常量),经理为2(常量),分 别实现bonus方法,创建对象测试。

package jicheng.l9;

public class Employee {
    private String name;
    private String idcard;
    private double salary;
    private String job;
    private double bonus = 0;
    final double LEVEL1 = 1;
    final double LEVEL = 1.5;

    public double getBonus() {
        return bonus;
    }

    public void setBonus(double bonus) {
        this.bonus = bonus;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getIdcard() {
        return idcard;
    }

    public void setIdcard(String idcard) {
        this.idcard = idcard;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    public String getJob() {
        return job;
    }

    public void setJob(String job) {
        this.job = job;
    }

    public Employee(String name,String idcard,double salary,String job){
            this.name = name;
            this.idcard = idcard;
            this.salary = salary;
            this.job = job;
    }
    public void bonus(){
        if("manager".equals(job)){
             bonus = salary *LEVEL;
        }else{
             bonus = salary*LEVEL1;
        }
        System.out.println(name+"的奖金为:"+bonus);
    }
}
package jicheng.l9;

public class EmployeeTest {
    public static void main(String[] args) {
        Employee employee = new Employee("张三","123455",20000,"manager");
        employee.bonus();
        Employee employee1 = new Employee("张yii","123455",20000,"oother");
        employee1.bonus();
    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值