1.机器人类及其子类
设计一个机器人的抽象类,具有质量(double)、名字(String)等属性;具有设置属性值方法和获得并打印属性值的方法,还有一个运动的抽象方法move()。设计电动汽车类和飞行器类,这两个类继承机器人抽象类,实现抽象方法,并在抽象方法中打印输出“我是电动汽车,我在地上跑!”(电动汽车类),以及“我是飞行器,我在天上飞!”(飞行器类)。设计测试类,分别生成电动汽车和飞行器的实例,分别按顺序输入名字和质量,并调用获得名字的方法输出名字,调用抽象方法打印对应信息。
输入描述:分两行输入电动汽车信息和飞行器信息
输出描述:分别输出电动汽车名字、运动信息。
运行结果:
源码:
package rotdome;
import java.util.Scanner;
public class RotDome {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
String name = in.next();
double weight = in.nextDouble();
String name1 = in.next();
double weight1 = in.nextDouble();
Rot car1 = new Car();
car1.setName(name);
car1.setQuality(weight);
System.out.println(car1.toSting());
car1.move();
Rot plan1 = new Plan();
plan1.setName(name1);
plan1.setQuality(weight1);
System.out.println(plan1.toSting());
plan1.move();
in.close();
}
}
abstract class Rot {
protected double weight;
protected String name;
public double getQuality() {
return weight;
}
public void setQuality(double quality) {
this.weight = quality;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
abstract public void move();
public String toSting() {
return "name:" + name + " weight:" + weight + "KG";
}
}
class Car extends Rot {
public void move() {
System.out.println("I am automobile,I am running on the ground!");
}
}
class Plan extends Rot {
public void move() {
System.out.println("I am aircraft,I am flying in the sky!");
}
}
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…
设计主测试类,输入名字、充电电压和放电量),生成电动汽车的实例,依次调用充电方法和放电方法。输入名字、充电功率和放电电压,生成电脑的实例,依次调用充电方法和放电方法。
输入描述
第一行输入电动汽车信息(名字 充电电压 放电量)
第二行输入电脑信息(名字 充电功率 放电电压)
输出描述
分别输出电动汽车、电脑充电、放电信息
运行结果:
源码:
package plugDome;
import java.util.Scanner;
public class PlugDome {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
//电动汽车类输入
String name = in.next();
double avoltage = in.nextDouble();
int dvoltage = in.nextInt();
//电脑类输入
String name1 = in.next();
int chargingpower = in.nextInt();
double dvoltage1 = in.nextDouble();
Plug E1 = new ElectricVehicle(name,avoltage,dvoltage);//new一个ElectricVehicle对象E1
Plug C1 = new Computer(name1,chargingpower,dvoltage1);//new一个Computer对象
E1.addPower();
E1.discharge();
C1.addPower();
C1.discharge();
in.close();
}
}
interface Plug{
int Voltage = 220;
public void addPower();//充电
public void discharge();//放电
}
class ElectricVehicle implements Plug{
private String name;
private double avoltage;
private int dvoltage;
public ElectricVehicle(String name,double avoltage,int dvoltage) {
this.name = name;
this.avoltage = avoltage;
this.dvoltage = dvoltage;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getAvoltage() {
return avoltage;
}
public void setAvoltage(double avoltage) {
this.avoltage = avoltage;
}
public int getDvoltage() {
return dvoltage;
}
public void setDvoltage(int dvoltage) {
this.dvoltage = dvoltage;
}
public void addPower() {
System.out.println("name:"+name+"vehicle, I am charging..."
+"\n"+"input voltage:"+Voltage+"V charging voltage:"+avoltage+"V");
}
public void discharge() {
System.out.println("name:"+name+"vehicle, I will discharge "+dvoltage
+"degrees,I am discharging...");
}
}
class Computer implements Plug{
private String name;
private int chargingpower;
private double dvoltage;
//带参构造
public Computer(String name,int chargingpower,double dvoltage) {
this.name = name;
this.chargingpower = chargingpower;
this.dvoltage = dvoltage;
}
//标准方法
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getChargingpower() {
return chargingpower;
}
public void setChargingpower(int chargingpower) {
this.chargingpower = chargingpower;
}
public double getDvoltage() {
return dvoltage;
}
public void setDvoltage(double dvoltage) {
this.dvoltage = dvoltage;
}
//实现接口中的抽象方法
public void addPower() {
System.out.println("name:"+name+" notebook, I am charging..."
+"\n"+"input voltage:"+Voltage+"V charging power:"+chargingpower+"W");
}
public void discharge() {
System.out.println("name:"+name+" notebook, will discharge."+"\n"+"Discharge voltage:"+dvoltage
+"V,I am discharging...");
}
}
END:我选择了去做,就被试图打断我。