Java Exercise 05 (02) Power Interface

  1. (50 points)
    Power supply interface topic description
    Design a power supply interface with an integer constant of input voltage with a value of 220; design two abstract methods: 1) charging method, AddPower(); 2) discharging method, GetPower() . Design electric vehicle class and computer class, set charging voltage (double), discharge capacity (int) and name (String) attributes, implement construction methods, basic input and output methods and power interface, requirements:
    1) In the power interface of electric vehicles In the charging method in the printout: "I am an XX electric car, I am charging, the input voltage is XX volts, and the charging voltage is XX volts". In the discharge method, output: "I am an XX electric car, I want to discharge XX degrees, I am discharging...".
    2) Print out in the charging method in the power interface of the computer: "I am a XX computer, I am charging, the input voltage is XX volts, and the charging voltage is XX volts". In the discharge method, output: "I am a computer, I want to discharge XX degrees, I am discharging...".
    Design the main test class, generate examples of electric vehicles and computers, enter the name, charging voltage and discharge capacity in sequence, and call the charging abstract method and the point abstract method in turn.
    Input description
    Input the electric vehicle information (name voltage discharge capacity) and computer information in two lines, separated by spaces.
    Output description
    Output charging and discharging information separately.
    Input example
    Dongfeng 36 100
    HP 3.3 20
    Output example
    I am Dongfeng electric car, I During charging, the input voltage is 220 volts, and the charging voltage is 36.0 volts.
    I am a Dongfeng electric car. I want to discharge 100 degrees. I am discharging...
    I am a HP computer and I am charging. The input voltage is 220 volts and the charging voltage is 3.3 volts.
    I’m a HP computer, I’m going to discharge 20 degrees, I’m discharging...
import java.util.Scanner;
public class Main {
    
    
    public static void main(String[] args) {
    
    
        Scanner sc = new Scanner(System.in);
        String name1 = sc.next();
        double chargingVoltage1 = sc.nextDouble();
        int dischargeCapacity1= sc.nextInt();

        String name2 = sc.next();
        double chargingVoltage2 = sc.nextDouble();
        int dischargeCapacity2= sc.nextInt();
        Electric_Vehicle ev = new Electric_Vehicle(name1,chargingVoltage1,dischargeCapacity1);
        Computer cp = new Computer(name2,chargingVoltage2,dischargeCapacity2);
        ev.AddPower();
        ev.GetPower();
        cp.AddPower();
        cp.GetPower();
    }
}

interface Power_Interface{
    
    
    static final int inputVoltage = 220;
//    充电
    abstract void AddPower();
//    放电
    abstract void GetPower();
}

class Electric_Vehicle implements Power_Interface{
    
    
//    充电电压
    double chargingVoltage;
//    放电量
    int dischargeCapacity ;
    String name;
    Electric_Vehicle(String name,double chargingVoltage,int dischargeCapacity){
    
    
        this.name = name;
        this.chargingVoltage = chargingVoltage;
        this.dischargeCapacity = dischargeCapacity;
    }
    public void AddPower(){
    
    
        System.out.println("我是"+this.name+"电动车,我在充电,输入电压为"
                +inputVoltage+"伏,充电电压为"+this.chargingVoltage+"伏");
    }
    public void GetPower(){
    
    
        System.out.println("我是"+this.name+"电动车,要放电"+this.dischargeCapacity+"度,我正在放电……");
    }
}


class Computer implements Power_Interface{
    
    
    //    充电电压
    double chargingVoltage;
    //    放电量
    int dischargeCapacity ;
    String name;
    Computer(String name,double chargingVoltage,int dischargeCapacity){
    
    
        this.name = name;
        this.chargingVoltage = chargingVoltage;
        this.dischargeCapacity = dischargeCapacity;
    }
    public void AddPower(){
    
    
        System.out.println("我是"+this.name+"电脑,我在充电,输入电压为"
                +inputVoltage+"伏,充电电压为"+this.chargingVoltage+"伏");
    }
    public void GetPower(){
    
    
        System.out.println("我是"+this.name+"电脑,要放电"+this.dischargeCapacity+"度,我正在放电……");
    }
}

Guess you like

Origin blog.csdn.net/weixin_44179485/article/details/105452310