今天是学习Java的第十九天
4道练习题
第1题
题目:
1、多态练习1
笔记本支持用户使用电池 (Battery) 和交流电(AcPower)两种方式进行供电。使用多态性输出目前笔记本的电源供电情况“XX供电中…”
运行结果:
代码如下:
package work1;
public abstract class PoweredBy {
public abstract void getPower();
}
package work1;
public class AcPower extends PoweredBy {
@Override
public void getPower() {
System.out.println("交流电供电中。。。。");
}
}
package work1;
public class Battery extends PoweredBy {
@Override
public void getPower() {
System.out.println("电池供电中。。。。");
}
}
package work1;
public class Laptop {
public void charge(PoweredBy pb) {
pb.getPower();
}
}
package work1;
public class Test {
public static void main(String[] args) {
Laptop lt = new Laptop();
System.out.println("IBM笔记本:");
lt.charge(new Battery());
System.out.println("hp笔记本:");
lt.charge(new AcPower());
}
}
第2题
题目:
2、多态练习2
使用多态模拟计算器加减乘除 父类:计算类 方法:计算 子类:加法类 减法类 乘法类 除法类 子类重写父类计算方法 创建各对象,进行测试
运行结果:
代码如下:
package work2;
public abstract class Calculator {
protected double num1;
protected double num2;
protected String opt;
public Calculator() {
}
public Calculator(double num1, double num2, String opt) {
super();
this.num1 = num1;
this.num2 = num2;
this.opt = opt;
}
public double getNum1() {
return num1;
}
public void setNum1(double num1) {
this.num1 = num1;
}
public double getNum2() {
return num2;
}
public void setNum2(double num2) {
this.num2 = num2;
}
public String getOpt() {
return opt;
}
public void setOpt(String opt) {
this.opt = opt;
}
public abstract void cal();
}
package work2;
public class Addition extends Calculator {
public Addition() {
}
public Addition(double num1, double num2, String opt) {
super(num1, num2, opt);
}
@Override
public void cal() {
double result = num1 + num2;
System.out.println(num1 + "+" + num2 + "=" + result);
}
}
package work2;
public class Subtraction extends Calculator {
public Subtraction() {
}
public Subtraction(double num1, double num2, String opt) {
super(num1, num2, opt);
}
@Override
public void cal() {
double result = num1 - num2;
System.out.println(num1 + "-" + num2 + "=" + result);
}
}
package work2;
public class Multiplication extends Calculator {
public Multiplication() {
}
public Multiplication(double num1, double num2, String opt) {
super(num1, num2, opt);
}
@Override
public void cal() {
double result = num1 * num2;
System.out.println(num1 + "*" + num2 + "=" + result);
}
}
package work2;
public class Division extends Calculator {
public Division() {
}
public Division(double num1, double num2, String opt) {
super(num1, num2, opt);
}
@Override
public void cal() {
double result = num1 / num2;
System.out.println(num1 + "/" + num2 + "=" + result);
}
}
package work2;
public class Test {
public static void main(String[] args) {
System.out.println("计算器");
Calculator c1 = new Addition(3, 5, "+");
c1.cal();
Calculator c2 = new Subtraction(12, 7, "-");
c2.cal();
Calculator c3 = new Multiplication(8, 9, "*");
c3.cal();
Calculator c4 = new Division(9, 3, "/");
c4.cal();
}
}
第3题
题目:
3、多态练习3 Instrument
小明喜欢乐器, 如果他有3万块钱,他就买一架钢琴 如果他有8000块钱,他就买一部吉他, 如果他还不到1000,就先买个口琴。 元旦晚会,他将使用自己的乐器演奏。 试用多态模拟此过程。 public 乐器 buy(int money)
运行结果:
代码如下:
package work3;
public abstract class Instrument {
public abstract void play();
}
package work3;
public class Piano extends Instrument {
@Override
public void play() {
System.out.println("弹钢琴。。。");
}
}
package work3;
public class Guitar extends Instrument {
@Override
public void play() {
System.out.println("弹吉他。。。");
}
}
package work3;
public class Harmonica extends Instrument {
@Override
public void play() {
System.out.println("吹口琴。。。");
}
}
package work3;
public class BuyInstrument {
private int money;
public BuyInstrument() {
}
public BuyInstrument(int money) {
super();
this.money = money;
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
public Instrument buy() {
Instrument ist = null;
if (money >= 30000) {
ist = new Piano();
} else if (money >= 8000 && money < 30000) {
ist = new Guitar();
} else if (money < 1000) {
ist = new Harmonica();
}
return ist;
}
}
package work3;
public class Test {
public static void main(String[] args) {
int money1 = 9000;// 大于8000元
System.out.println("若有" + money1 + "元");
BuyInstrument bi = new BuyInstrument(money1);
bi.buy().play();
System.out.println("---------------------");
int money2 = 40000;// 大于30000元
System.out.println("若有" + money2 + "元");
BuyInstrument bi2 = new BuyInstrument(money2);
bi2.buy().play();
System.out.println("---------------------");
int money3 = 500;// 不到1000元
System.out.println("若有" + money3 + "元");
BuyInstrument bi3 = new BuyInstrument(money3);
bi3.buy().play();
}
}
第4题
题目:
运行结果:
代码如下:
/**
* 车辆类:是一个父类
* */
package work4;
public class Vechile {
protected String no;// 车牌号
protected String model;// 车型
protected int usageTime;// 使用时间
protected double dailyRent;// 日租金
protected int state;// 状态:0表示可租,1表示待还
public Vechile() {
}
public Vechile(String no, String model, int usageTime, double dailyRent) {
super();
this.no = no;
this.model = model;
this.usageTime = usageTime;
this.dailyRent = dailyRent;
}
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public int getUsageTime() {
return usageTime;
}
public void setUsageTime(int usageTime) {
this.usageTime = usageTime;
}
public double getDailyRent() {
return dailyRent;
}
public void setDailyRent(double dailyRent) {
this.dailyRent = dailyRent;
}
public int getState() {
return state;
}
public void setState(int state) {
this.state = state;
}
public void showInfo() {
System.out.println("车牌号:" + this.no);
System.out.println("车型:" + this.model);
System.out.println("使用时间:" + this.usageTime);
System.out.println("日租金:" + this.dailyRent);
}
}
/**
* 小汽车类:是一个子类
* */
package work4;
public class Car extends Vechile {
public Car() {
}
public Car(String no, String model, int usageTime, double dailyRent) {
super(no, model, usageTime, dailyRent);
}
@Override
public void showInfo() {
super.showInfo();
}
}
/**
* 卡车类:是一个子类
* */
package work4;
public class Truck extends Vechile {
private double load;// 载重
public Truck() {
}
public Truck(String no, String model, int usageTime, double dailyRent, double load) {
super(no, model, usageTime, dailyRent);
this.load = load;
}
public double getLoads() {
return load;
}
public void setLoads(double load) {
this.load = load;
}
@Override
public void showInfo() {
super.showInfo();
System.out.println("载重:" + this.load);
}
}
/**
* 数据操作类
* */
package work4;
import java.util.Scanner;
public class VechileDao {
private Vechile[] vs;
public VechileDao() {// 只有无参构造
vs = new Vechile[20];// 使用无参构造来初始化
}
public Vechile[] getVs() {
return vs;
}
public void setVs(Vechile[] vs) {
this.vs = vs;
}
public Vechile getVechile() {// 创建一辆车
Scanner input = new Scanner(System.in);
Vechile v = null;
System.out.println("请输入要创建的车(小汽车/卡车):");
String select = input.next();
if ("小汽车".equals(select)) {
System.out.println("请输入车牌号:");
String select2 = input.next();
System.out.println("请输入使用时间(年):");
int select3 = input.nextInt();
System.out.println("请输入日租金(元):");
double select4 = input.nextDouble();
v = new Car(select2, "小汽车", select3, select4);
}
if ("卡车".equals(select)) {
System.out.println("请输入车牌号:");
String select2 = input.next();
System.out.println("请输入使用时间(年):");
int select3 = input.nextInt();
System.out.println("请输入日租金(元):");
double select4 = input.nextDouble();
System.out.println("请输入载重(吨):");
double select5 = input.nextDouble();
v = new Truck(select2, "卡车", select3, select4, select5);
}
return v;
}
public boolean putInStorage() {// 入库
for (int i = 0; i < vs.length; i++) {
if (vs[i] == null) {// 找到空位置
Vechile v = getVechile();// 创建车辆
if (v != null) {// 创建车辆成功
vs[i] = v;// 放入
vs[i].setState(0);// 状态改为可租
return true;
} else {// 创建车辆失败
return false;// 放回false,入库失败
}
}
}
return false;
}
public boolean rentVechile(String no) {// 租车
Scanner input = new Scanner(System.in);
for (int i = 0; i < vs.length; i++) {// 遍历一遍车库
if (vs[i] != null && vs[i].getNo().equals(no) && vs[i].getState() == 0) {// 下标不空 并且车牌号一致 并且状态为可租
System.out.println("请输入要租的天数:");
int days = input.nextInt();// 存放输入的天数
Vechile v = null;// 存放通过车牌号找到的车
for (int j = 0; j < vs.length; j++) {// 通过车牌找到车
if (vs[j] != null && vs[j].getNo().equals(no)) {
v = vs[j];
}
}
System.out.println("车牌号为" + no + "的" + v.getModel() + days + "天的租金为:" + calRent(v, days));
vs[i].setState(1);// 状态改为待还
return true;// 租车成功
}
}
return false;// 租车失败
}
public boolean returnVechile(String no) {// 还车
for (int i = 0; i < vs.length; i++) {
if (vs[i] != null && vs[i].getNo().equals(no) && vs[i].getState() == 1) {// 下标不空 并且车牌号一致 并且状态为待还
vs[i].setState(0);
return true;// 还车成功
}
}
return false;// 还车失败
}
public double calRent(Vechile v, int days) {// 计算租金
if (days >= 1 && days <= 30) {// 若租期小于等于30天,小汽车和卡车的租金计算方式一样
return v.getDailyRent() * days;
} else if (days > 30) {// 若租期大于30天
if (v.getModel().equals("小汽车")) {// 若要租的是小汽车
return v.getDailyRent() * 30 + v.getDailyRent() * 1.1 * (days - 30);// 返回计算的租金
}
if (v.getModel().equals("卡车")) {// 若要租的是卡车
int[] loads = TruckLoad(days);// 创建一个数组,存放输入的卡车30天后的载重数据,例如loads[0]存放的是第31天的租金
int sum = 0;// 记录30后的租金和
for (int i = 1; i <= days - 30; i++) {// 计算卡车30天后的租金
sum += v.getDailyRent() + loads[i - 1] / 10 * v.getDailyRent();// 30天后卡车的租金分为基础日租金+载重量租金
}
return v.getDailyRent() * 30 + sum;// 返回总租金
}
}
return -1;// 异常情况,表示天数输入错误
}
public int[] TruckLoad(int days) {// 输入卡车30天后的载重数据
Scanner input = new Scanner(System.in);
int[] loads = new int[days - 30];// 这个数组存放卡车30后的载重数据
for (int i = 1; i <= days - 30; i++) {// 遍历30后的每一天
System.out.println("请输入卡车第" + (30 + i) + "天的载重量");
int select = input.nextInt();
loads[i - 1] = select;// 输入数据
}
return loads;// 将数组返回
}
public void showInfo() {// 显示待租车辆信息
boolean flag = false;
for (int i = 0; i < vs.length; i++) {
if (vs[i] != null && vs[i].getState() == 0) {// 下标不空 并且状态为0
vs[i].showInfo();// 显示信息
flag = true;// true表示显示过待租车辆
System.out.println();
}
}
if (!flag) {// 若没有显示过待租车辆,说明没有车辆可租
System.out.println("车库里没有车了,请尽快将新车入库");
}
}
public void showPending() {// 显示待还车辆信息
boolean flag = false;
for (int i = 0; i < vs.length; i++) {
if (vs[i] != null && vs[i].getState() == 1) {// 下标不空 并且状态为0
vs[i].showInfo();
flag = true;
System.out.println();
}
}
if (!flag) {
System.out.println("所有车都在车库,没有在外的车");
}
}
}
/**
* 显示类
* */
package work4;
import java.util.Scanner;
public class VechileView {
private VechileDao ve;
public VechileView() {
ve = new VechileDao();// 使用无参构造来初始化
}
public void showInfo() {// 打印可租车辆
ve.showInfo();
}
public void showPending() {// 打印待还车辆
ve.showPending();
}
public void putInStorage() {// 入库
boolean flag = ve.putInStorage();
if (flag) {
System.out.println("入库成功");
} else {
System.out.println("入库失败");
}
}
public void rentVechile() {// 租车
Scanner input = new Scanner(System.in);
System.out.println("请输入要租的车辆的车牌号:");
String no = input.next();
boolean flag = ve.rentVechile(no);
if (flag) {
System.out.println("租车成功");
} else {
System.out.println("租车失败");
}
}
public void returnVechile() {// 还车
Scanner input = new Scanner(System.in);
System.out.println("请输入要还的车辆的车牌号:");
String no = input.next();
boolean flag = ve.returnVechile(no);
if (flag) {
System.out.println("还车成功");
} else {
System.out.println("还车失败");
}
}
public void menu() {
Scanner input = new Scanner(System.in);
int select;
do {
System.out.println("\n-----欢迎来到汽车租赁系统-----");
System.out.println("\t1、查询可租车辆");
System.out.println("\t2、查询待还车辆");
System.out.println("\t3、新车入库");
System.out.println("\t4、租车");
System.out.println("\t5、还车");
System.out.println("\t6、退出系统");
System.out.println("------------------------------");
System.out.println("请输入您的选择:");
select = input.nextInt();
switch (select) {
case 1:
showInfo();
break;
case 2:
showPending();
break;
case 3:
putInStorage();
break;
case 4:
rentVechile();
break;
case 5:
returnVechile();
break;
case 6:
System.out.println("退出成功");
System.exit(0);
}
} while (select != 6);
}
}
/**
* 测试类
* */
package work4;
public class Test {
public static void main(String[] args) {
VechileView vv = new VechileView();
vv.menu();
}
}