Java综合练习题
要求:
1)定义一个父类,并定义多个不同子类,子类分别以不同方式重写父类的方法;
2)定义多个类型的对象,编写方法实现多态效果;
3)将多个对象存放在一个适当的集合中;
4)使用合适的方法对集合中的数据按一定的方式排序;
5)使用迭代器遍历集合并输出集合的元素,将排序后输出的结果写入out.txt文件中;
集合中要使用泛型,IO操作需要有异常处理。
Main包
Main.java
package Main;
import Motor_Vehicle.*;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;
public class Main {
public static void main(String[] args) {
TreeSet<Motor_Vehicle> Cars= new TreeSet<>(new MyComparator());
Motor_Vehicle AMG=new Benz(); //定义一个车类为Benz,款式为AMG的对象。
Petrol_Engine pe1=new Petrol_Engine(); //定义一个汽油发动机对象。
pe1.setCooling_system("水冷系统"); //发动机的冷却系统采用水冷
pe1.setPrice(200000); //发动机价格为200000
Spoiler s1=new Spoiler(); //定义一个扰流板对象
s1.HasSpoiler=false; //没有扰流板因此HasSpoiler=false
Pipe pipe1=new Pipe(2,"铁素体不锈钢"); //定义一个排气筒对象
AMG.setEngine(pe1);
AMG.setSpoiler(s1);
AMG.setPipe(pipe1);
AMG.setTop_speed(200); //让最高时速设置为200km/h
AMG.setName("AMG");
Motor_Vehicle Range_Rover=new Rover(); //定义一个车类为Rover,款式为Range_Rover的对象。
Diesel_Engine de1=new Diesel_Engine(); //定义一个柴油发动机对象。
de1.setCooling_system("风冷系统"); //发动机的冷却系统采用风冷
de1.setPrice(400000); //发动机价格为400000
Spoiler s2=new Spoiler();
s2.HasSpoiler=true; //有扰流板因此HasSpoiler=true
Range_Rover.setEngine(de1);
Range_Rover.setSpoiler(s2);
Range_Rover.setPipe(pipe1);
Range_Rover.setTop_speed(220);
Range_Rover.setName("Range Rover");
Motor_Vehicle MayBach=new Benz(); //定义一个车类为Benz,款式为MayBach的对象。
Petrol_Engine pe2=new Petrol_Engine(); //定义一个汽油发动机对象。
pe2.setCooling_system("水冷系统"); //发动机的冷却系统采用风冷
pe2.setPrice(400000); //发动机价格为400000
Pipe pipe2=new Pipe(4,"铁素体不锈钢"); //定义一个排气筒对象
MayBach.setEngine(pe2);
MayBach.setSpoiler(s2);
MayBach.setPipe(pipe2);
MayBach.setTop_speed(300); //让最高时速设置为300km/h
MayBach.setName("MayBach");
Motor_Vehicle X7=new BMW(); //定义一个车类为BMW,款式为X8的对象
Petrol_Engine pe3=new Petrol_Engine(); //定义一个汽油发动机对象。
pe3.setCooling_system("水冷系统"); //发动机的冷却系统采用风冷
pe3.setPrice(300000); //发动机价格为300000
X7.setEngine(pe3);
X7.setSpoiler(s1);
X7.setPipe(pipe1);
X7.setTop_speed(230);
X7.setName("X7");
Motor_Vehicle Model3=new Tesla();
Electric_Engine ee1=new Electric_Engine(); //定义一个电力发动机对象
ee1.setCooling_system("风冷系统"); //发动机的冷却系统采用风冷
ee1.setPrice(500000); //发动机价格为500000
Pipe pipe3=new Pipe(0,""); //定义一个排气筒对象
Model3.setEngine(ee1);
Model3.setSpoiler(s2);
Model3.setPipe(pipe3);
Model3.setTop_speed(220);
Model3.setName("Model3");
Motor_Vehicle M4=new BMW(); //定义一个车类为BMW,款式为M4的对象
Petrol_Engine pe4=new Petrol_Engine();
pe4.setCooling_system("水冷系统");
pe4.setPrice(400000);
Pipe pipe4=new Pipe(4,"普通钢铁");
M4.setEngine(pe4);
M4.setSpoiler(s2);
M4.setPipe(pipe4);
M4.setTop_speed(250);
M4.setName("M4");
Motor_Vehicle Davidson=new Harley(); //定义一个车类为Harley,款式为Davidson的对象
Diesel_Engine de2=new Diesel_Engine();
de2.setCooling_system("风冷系统");
de2.setPrice(400000);
Pipe pipe5=new Pipe(1,"铁素体不锈钢");
Davidson.setEngine(de2);
Davidson.setSpoiler(s1);
Davidson.setPipe(pipe5);
Davidson.setTop_speed(200);
Davidson.setName("Davidson");
Motor_Vehicle Iron=new Harley(); //定义一个车类为Harley,款式为Iron的对象
Diesel_Engine de3=new Diesel_Engine();
de3.setCooling_system("水冷系统");
de3.setPrice(500000);
Pipe pipe6=new Pipe(4,"铁素体不锈钢");
Iron.setEngine(de3);
Iron.setSpoiler(s1);
Iron.setPipe(pipe6);
Iron.setTop_speed(200);
Iron.setName("Iron");
Motor_Vehicle GLC=new Benz(); //定义一个车类为Benz,款式为GLC的对象
Diesel_Engine de4=new Diesel_Engine();
de4.setCooling_system("水冷系统");
de4.setPrice(200000);
Pipe pipe7=new Pipe(2,"普通钢铁");
GLC.setEngine(de4);
GLC.setSpoiler(s1);
GLC.setPipe(pipe7);
GLC.setTop_speed(180);
GLC.setName("GLC");
Cars.add(Iron);
Cars.add(AMG);
Cars.add(MayBach);
Cars.add(X7);
Cars.add(Range_Rover);
Cars.add(GLC);
Cars.add(Model3);
Cars.add(Davidson);
Cars.add(M4);
Iterator it=Cars.iterator();
StringBuilder sb=new StringBuilder();
while(it.hasNext())
{
Motor_Vehicle m=(Motor_Vehicle)it.next();
sb.append(m.toString());
sb.append("\r\n");
System.out.print(m.toString());
m.controlairconditioner();
System.out.println();
}
try {
FileOutputStream fos=new FileOutputStream("D:\\out.txt",false);
String str=sb.toString();
byte []b=str.getBytes();
for(int i=0;i<b.length;i++)
{
fos.write(b[i]);
}
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Motor_Vehicle包
1)Air_Conditioner接口
package Motor_Vehicle;
public interface Air_Conditioner {
//空调接口
void controlairconditioner();
}
2)Benz类
package Motor_Vehicle;
import java.util.Comparator;
public class Benz extends Motor_Vehicle {
@Override
public double Total_Price() {
double engineprice=this.engine.Price;
double pipeprice=this.pipe.Price;
double spoilerprice=this.spoiler.price;
return 600000+engineprice+pipeprice+spoilerprice;
}
@Override
public double Top_Speed() {
Petrol_Engine p=new Petrol_Engine(); //定义一个汽油机对象
Diesel_Engine d=new Diesel_Engine(); //定义一个柴油机对象
if(this.engine.getClass().equals(p.getClass()))
{
return 300; //如果是汽油发动机,则最高时速不能超过300km/h.
}
if(this.engine.getClass().equals(d.getClass()))
{
return 250; //如果是柴油发动机,则最高时速不能超过250km/h.
}
else return 0; //其他情况说明输入错误,返回一个0.
}
@Override
public void controlairconditioner() {
System.out.println(this.Name+"装有空调。");
}
}
3)BMW类
package Motor_Vehicle;
import java.util.Comparator;
public class BMW extends Motor_Vehicle {
@Override
public double Total_Price() {
double engineprice=this.engine.Price;
double pipeprice=this.pipe.Price;
double spoilerprice=this.spoiler.price;
return 500000+engineprice+pipeprice+spoilerprice;
}
@Override
public double Top_Speed() {
Petrol_Engine p=new Petrol_Engine(); //定义一个汽油机对象
Diesel_Engine d=new Diesel_Engine(); //定义一个柴油机对象
if(this.engine.getClass().equals(p.getClass()))
{
return 260; //如果是汽油发动机,则最高时速不能超过260km/h.
}
if(this.engine.getClass().equals(d.getClass()))
{
return 220; //如果是柴油发动机,则最高时速不能超过220km/h.
}
else return 0; //其他情况说明输入错误,返回一个0.
}
@Override
public void controlairconditioner() {
System.out.println(this.Name+"装有空调。");
}
}
4)Harley类
package Motor_Vehicle;
import java.util.Comparator;
public class Harley extends Motor_Vehicle {
@Override
public double Total_Price() {
double engineprice=this.engine.Price;
double pipeprice=this.pipe.Price;
double spoilerprice=this.spoiler.price;
return 300000+engineprice+pipeprice+spoilerprice;
}
@Override
public double Top_Speed() {
Petrol_Engine p=new Petrol_Engine(); //定义一个汽油机对象
Diesel_Engine d=new Diesel_Engine(); //定义一个柴油机对象
if(this.engine.getClass().equals(p.getClass()))
{
return 200; //如果是汽油发动机,则最高时速不能超过200km/h.
}
if(this.engine.getClass().equals(d.getClass()))
{
return 200; //如果是柴油发动机,则最高时速不能超过200km/h.
}
else return 0; //其他情况说明输入错误,返回一个0.
}
@Override
public void controlairconditioner() {
System.out.println(this.Name+"没有空调。");
}
}
5)Rover类
package Motor_Vehicle;
import java.util.Comparator;
public class Rover extends Motor_Vehicle {
@Override
public double Total_Price() {
double engineprice=this.engine.Price;
double pipeprice=this.pipe.Price;
double spoilerprice=this.spoiler.price;
return 700000+engineprice+pipeprice+spoilerprice;
}
@Override
public double Top_Speed() {
Petrol_Engine p=new Petrol_Engine(); //定义一个汽油机对象
Diesel_Engine d=new Diesel_Engine(); //定义一个柴油机对象
Electric_Engine e=new Electric_Engine(); //定义一个电发动机对象
if(this.engine.getClass().equals(p.getClass()))
{
return 300; //如果是汽油发动机,则最高时速不能超过300km/h.
}
if(this.engine.getClass().equals(d.getClass()))
{
return 270; //如果是柴油发动机,则最高时速不能超过270km/h.
}
if(this.engine.getClass().equals(e.getClass()))
{
return 250; //如果是电发动机,则最高时速不能超过250km/h.
}
else return 0; //其他情况说明输入错误,返回一个0.
}
@Override
public void controlairconditioner() {
System.out.println(this.Name+"装有空调。");
}
}
6)Tesla类
package Motor_Vehicle;
import java.util.Comparator;
public class Tesla extends Motor_Vehicle {
@Override
public double Total_Price() {
double engineprice=this.engine.Price;
double pipeprice=this.pipe.Price;
double spoilerprice=this.spoiler.price;
return 500000+engineprice+pipeprice+spoilerprice;
}
@Override
public double Top_Speed() {
Electric_Engine e=new Electric_Engine(); //定义一个电发动机对象
if(this.engine.getClass().equals(e.getClass()))
{
return 250; //如果是电发动机,则最高时速不能超过250km/h.
}
else return 0; //其他情况说明输入错误,返回一个0.
}
@Override
public void controlairconditioner() {
System.out.println(this.Name+"装有空调。");
}
}
7)Engine类
package Motor_Vehicle;
abstract public class Engine implements Run {
//汽车引擎类
double Price; //引擎价格
String cooling_system; //冷却系统
public abstract void Runby(); //引擎燃料,分为汽油机,柴油机和电机,继承Run接口的Runby方法
public double getPrice() {
return Price;
}
public void setPrice(double price) {
this.Price = price;
}
public String getCooling_system() {
return cooling_system;
}
public void setCooling_system(String cooling_system) {
this.cooling_system = cooling_system;
}
}
8)Diesel_Engine类
package Motor_Vehicle;
public class Diesel_Engine extends Engine {
@Override
public void Runby() {
System.out.println("该汽车使用的是柴油机。");
}
}
9)Electric_Engine类
package Motor_Vehicle;
public class Electric_Engine extends Engine {
@Override
public void Runby() {
System.out.println("该汽车使用的是电发动机。");
}
}
10)Petrol_Engine类
package Motor_Vehicle;
public class Petrol_Engine extends Engine {
@Override
public void Runby() {
System.out.println("该汽车使用的是汽油机。");
}
}
11)Motor_Vehicle类
package Motor_Vehicle;
import java.util.Comparator;
abstract public class Motor_Vehicle implements Air_Conditioner {
//汽车类,汽车一般都有空调,继承接口Air_Conditioner
Engine engine; //汽车的引擎
Spoiler spoiler; //汽车的扰流板
Pipe pipe; //汽车的排气管
String Name; //汽车的型号
double top_speed; //汽车的最高时速
public double getTop_speed() {
return top_speed;
}
public void setTop_speed(double top_speed) {
if(top_speed<=Top_Speed())
this.top_speed = top_speed;
//如果输入的最高时速大于该发动机最大限度,则最高时速设置失败。
else this.top_speed=0;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public Engine getEngine() {
return engine;
}
public void setEngine(Engine engine) {
this.engine = engine;
}
public Spoiler getSpoiler() {
return spoiler;
}
public void setSpoiler(Spoiler spoiler) {
this.spoiler = spoiler;
}
public Pipe getPipe() {
return pipe;
}
public void setPipe(Pipe pipe) {
this.pipe = pipe;
}
public abstract double Total_Price(); //汽车的总价
public abstract double Top_Speed(); //汽车的最高时速不能超过的值,受发动机种类限定
public abstract void controlairconditioner(); //继承接口Air_Conditioner的方法
@Override
public String toString()
{
if (this.top_speed == 0)
return "输入错误";
return "车的型号:" + this.Name
+ "\r\n" +
"车的最高时速:" + this.top_speed +"km/h"
+ "\r\n" +
"车的总价:" + this.Total_Price()
+ "\r\n";
}
}
12)Pipe类
package Motor_Vehicle;
public class Pipe {
//排气管类
int Count; //排气管个数
String Material; //排气管材质
double Price; //排气管价格
public Pipe(int count,String material) //构造函数,输入排气管个数和材质,为排气管个数、材质、价格赋值
{
this.Count=count;
this.Material=material;
if(this.Material=="铁素体不锈钢")
{
this.Price=Count*8000;
}
else if(this.Material=="普通钢铁")
{
this.Price=Count*3000;
}
else this.Price=0; //没有排气管的情况,比如电车
}
}
13)Spoiler类
package Motor_Vehicle;
public class Spoiler {
//汽车扰流板,只有部分汽车才有。且价格固定,种类只有一种
public boolean HasSpoiler;
double price;
public Spoiler()
{
if(HasSpoiler)
{
this.price=6000;
}
else this.price=0;
}
public double getPrice() {
return price;
}
}
14)Run接口
package Motor_Vehicle;
public interface Run {
//接口"运行",表示引擎的燃料类型
void Runby(); //抽象方法Runby
}
15)MyComparator方法
package Motor_Vehicle;
import java.util.Comparator;
public class MyComparator implements Comparator {
@Override
public int compare(Object o1, Object o2) {
Benz b1=new Benz();
BMW b2=new BMW();
Harley h=new Harley();
Rover r=new Rover();
Tesla t=new Tesla();
Motor_Vehicle m1=(Motor_Vehicle)o1;
Motor_Vehicle m2=(Motor_Vehicle)o2;
if(m1.getClass().equals(m2.getClass()))
{
double temp=m1.Total_Price()-m2.Total_Price();
return (int)temp;
}
if(m1.getClass()==b1.getClass())
{
return -1;
}if(m2.getClass()==b1.getClass())
{
return 1;
}
if(m1.getClass()==b2.getClass())
{
return -1;
}if(m2.getClass()==b2.getClass())
{
return 1;
}
if(m1.getClass()==h.getClass())
{
return -1;
}if(m2.getClass()==h.getClass())
{
return 1;
}
if(m1.getClass()==r.getClass())
{
return -1;
}if(m2.getClass()==r.getClass())
{
return 1;
}
else return 1;
}
}
运行结果:
车的型号:GLC
车的最高时速:180.0km/h
车的总价:806000.0
GLC装有空调。
车的型号:AMG
车的最高时速:200.0km/h
车的总价:816000.0
AMG装有空调。
车的型号:MayBach
车的最高时速:300.0km/h
车的总价:1032000.0
MayBach装有空调。
车的型号:X7
车的最高时速:230.0km/h
车的总价:816000.0
X7装有空调。
车的型号:M4
车的最高时速:250.0km/h
车的总价:912000.0
M4装有空调。
车的型号:Davidson
车的最高时速:200.0km/h
车的总价:708000.0
Davidson没有空调。
车的型号:Iron
车的最高时速:200.0km/h
车的总价:832000.0
Iron没有空调。
车的型号:Range Rover
车的最高时速:220.0km/h
车的总价:1116000.0
Range Rover装有空调。
车的型号:Model3
车的最高时速:220.0km/h
车的总价:1000000.0
Model3装有空调。
Process finished with exit code 0