目录
1.机器人及其子类
设计一个机器人的抽象类,具有质量(double)、名字(String)等属性;具有设置属性值方法和获得并打印属性值的方法,还有一个运动的抽象方法move()。
设计电动汽车类和飞行器类,这两个类继承机器人抽象类,实现抽象方法,并在抽象方法中打印输出“我是电动汽车,我在地上跑!”(电动汽车类),以及“我是飞行器,我在天上飞!”(飞行器类)。
设计测试类,分别生成电动汽车和飞行器的实例,分别按顺序输入名字和质量,并调用获得名字的方法输出名字,调用抽象方法打印对应信息。
输入描述:分两行输入电动汽车信息和飞行器信息
输出描述:分别输出电动汽车名字、运动信息。
序号 | 输入样例 | 输出样例 |
1 | Tesla 2200 ShenzhouV 7790 | name:Tesla weight:2200.0KG I am automobile,I am running on the ground! name:ShenzhouV weight:7790.0KG I am aircraft,I am flying in the sky! |
代码实现:
//Robot.java
package practice_5_1;
public abstract class Robot {
protected double mass;
protected String name;
public abstract void move();
public double getMass() {
return mass;
}
public void setMass(double mass) {
this.mass = mass;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Robot(String name,double mass){
setName(name);
setMass(mass);
}
}
//ElectricalCar.java
package practice_5_1;
public class ElectricalCar extends Robot{
public ElectricalCar(String name, double mass) {
super(name, mass);
}
@Override
public void move() {
System.out.println("I am automobile,I am running on the ground!");
}
}
//Aircraft.java
package practice_5_1;
public class Aircraft extends Robot{
public Aircraft(String name, double mass) {
super(name, mass);
}
@Override
public void move() {
System.out.println("I am aircraft,I am flying in the sky!");
}
}
//Test.java
package practice_5_1;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Robot robot1 = new ElectricalCar(sc.next(),sc.nextDouble());
Robot robot2 = new Aircraft(sc.next(),sc.nextDouble());
System.out.println("name:"+robot1.getName()+" "+"weight:"+robot1.getMass()+"KG");
robot1.move();
System.out.println("name:"+robot2.getName()+" "+"weight:"+robot2.getMass()+"KG");
robot2.move();
}
}
输出显示:
Tesla 2200
ShenzhouV 7790
name:Tesla weight:2200.0KG
I am automobile,I am running on the ground!
name:ShenzhouV weight:7790.0KG
I am aircraft,I am flying in the sky!进程已结束,退出代码0
2. 一个电源接口
有一个输入电压的整型常量,值为220;设计两个抽象方法:1)充电方法,addPower();2)放电方法,discharge()。
设计电动汽车类,设置名字(String)、充电电压(double)和放电量(int)属性,实现构造方法、基本的输入输出方法和电源接口,实现充电方法,输出:
name:XXX vehicle, I am charging...
input voltage:XXX charging voltage:XXX”。
实现放电方法,输出:
name:XXX vehicle, I will discharge XXX degrees, I am discharging...
设计电脑类,设置名字(String)、充电功率(int)和放电电压(double)属性,实现构造方法、基本的输入输出方法和电源接口。
实现充电方法,输出:
name:XXX notebook, I am charging...
input voltage:XXXV charging power:XXW
实现放电方法,输出:
name:XXX notebook, will discharge.
Discharge voltage:XXXV,I am discharging...
设计主测试类,输入名字、充电电压和放电量),生成电动汽车的实例,依次调用充电方法和放电方法。
输入名字、充电功率和放电电压,生成电脑的实例,依次调用充电方法和放电方法。
输入描述
第一行输入电动汽车信息(名字 充电电压 放电量)
第二行输入电脑信息(名字 充电功率 放电电压)
输出描述
分别输出电动汽车、电脑充电、放电信息
序号 | 输入样例 | 输出样例 |
1 | Dongfeng 36 100 HP 65 9.5 | name:Dongfeng vehicle,I am charging... input voltage:220V charging voltage:36.0V name:Dongfeng vehicle,I will discharge 100 degrees,I am discharging... name:HP notebook,I am charging... input voltage:220V charging power:65W name:HP notebook,I will discharge. Discharge voltage:9.5V,I am discharging... |
代码实现:
//Power.java
package practice_5_2;
interface Power {
int voltage=220;
void addPower();
void disCharge();
}
//Electromobile.java
package practice_5_2;
public class Electromobile implements Power{
private String name;
private double chargingVoltage;
private int dischargeCapacity;
public Electromobile(String name, double chargingVoltage, int dischargeCapacity) {
this.name = name;
this.chargingVoltage = chargingVoltage;
this.dischargeCapacity = dischargeCapacity;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getChargingVoltage() {
return chargingVoltage;
}
public void setChargingVoltage(double chargingVoltage) {
this.chargingVoltage = chargingVoltage;
}
public double getDischargeCapacity() {
return dischargeCapacity;
}
public void setDischargeCapacity(int dischargeCapacity) {
this.dischargeCapacity = dischargeCapacity;
}
@Override
public void addPower() {
System.out.println("name:"+name+" vehicle,I am charging...");
System.out.println("input voltage:"+voltage+"V "+"charging voltage:"+getChargingVoltage()+"V");
}
@Override
public void disCharge() {
System.out.println("name:"+name+" vehicle,I will discharge "+dischargeCapacity+" degrees,I am discharging...");
}
}
//Computer.java
package practice_5_2;
public class Computer implements Power{
private String name;
private int chargingVoltage;
private double disChargingVoltage;
public Computer(String name, int chargingVoltage, double disChargingVoltage) {
this.name = name;
this.chargingVoltage = chargingVoltage;
this.disChargingVoltage = disChargingVoltage;
}
@Override
public void addPower() {
System.out.println("name:"+name+" notebook,I am charging...");
System.out.println("input voltage:"+voltage+"V charging power:"+getChargingVoltage()+"W");
}
@Override
public void disCharge() {
System.out.println("name:"+name+" notebook,I will discharge.");
System.out.println("Discharge voltage:"+getDisChargingVoltage()+"V,I am discharging...");
}
public String getName() {
return name;
}
public int getChargingVoltage() {
return chargingVoltage;
}
public double getDisChargingVoltage() {
return disChargingVoltage;
}
}
//Test.java
package practice_5_2;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Electromobile electromobile = new Electromobile(sc.next(),sc.nextDouble(),sc.nextInt());
Computer computer = new Computer(sc.next(),sc.nextInt(),sc.nextDouble());
electromobile.addPower();
electromobile.disCharge();
computer.addPower();
computer.disCharge();
}
}
输出显示:
Dongfeng 36 100
HP 65 9.5
name:Dongfeng vehicle,I am charging...
input voltage:220V charging voltage:36.0V
name:Dongfeng vehicle,I will discharge 100 degrees,I am discharging...
name:HP notebook,I am charging...
input voltage:220V charging power:65W
name:HP notebook,I will discharge.
Discharge voltage:9.5V,I am discharging...进程已结束,退出代码0