<Java> Inheritance Usage
- Jeemin Myung

- 2020년 11월 29일
- 3분 분량
public class Jet implements Comparable<Jet> {
protected String manufacturer;
protected String model;
protected int year;
protected String owner;
protected double grossWeightEmpty;
protected int lastOverhaul;
protected int numOverhauls;
protected int maxRecommendedFlightHours;
public Jet(String manufacturer, String model, int year, String owner, double grossWeightEmpty,
int lastOverhaul,int numOverhauls, int maxRecommendedFlightHours){
this.manufacturer=manufacturer;
this.model=model;
this.year=year;
this.owner=owner;
this.grossWeightEmpty=grossWeightEmpty;
this.lastOverhaul=lastOverhaul;
this.numOverhauls=numOverhauls;
this.maxRecommendedFlightHours=maxRecommendedFlightHours;
}
public void sellTo(String owner){
this.owner=owner;
}
public void overhaul(){
lastOverhaul=0;
numOverhauls++;
}
public int timeTillOverhaul(){
return maxRecommendedFlightHours-lastOverhaul;
}
public void fly(int dailyHour){
lastOverhaul+=dailyHour;
}
public boolean needsOverhaul(){
return maxRecommendedFlightHours-lastOverhaul<=100;
}
public boolean isAging(){
return 2020-year>15&&numOverhauls>=20;
}
public int compareTo(Jet o) {
if(this.year<o.year){
return -1;
}
else if(this.year>o.year){
return 1;
}
else{
return 0;
}
}
public String toString (){
return "Manufacturer: "+manufacturer+", Model: "+model+", Year: "+year+", Owner: "+
owner+", Weight in Kg: "+grossWeightEmpty+", Last overhaul: "+lastOverhaul+", Numbers of overhauls: "+
numOverhauls+", Max recommended flight hours: "+maxRecommendedFlightHours;
}
public static void main(String[]args){
Jet j1=new Jet("Boeing","A-747",2018,"Jeemin Myung",1400.7,14,7,250);
Jet j2=new Jet("McDonnell Douglas","F-15K",2000,"Lt.Dan",1224.23,12,21,30);
System.out.println("j1's specification a: "+j1);
j1.sellTo("Professor.Kuhn");
j1.fly(20);
j1.overhaul();
System.out.println("j1's specification b: "+j1);
System.out.println("Jet j1's time till overhaul: "+j1.timeTillOverhaul());
System.out.println("Jet j1 needs overhaul: "+j1.needsOverhaul());
System.out.println("Jet j1 is aging: "+j1.isAging());
System.out.println("\nj2's specification: "+j2);
System.out.println("Jet j2's time till overhaul: "+j2.timeTillOverhaul());
System.out.println("Jet j2 needs overhaul: "+j2.needsOverhaul());
System.out.println("Jet j2 is aging: "+j2.isAging());
if(j1.compareTo(j2)==1){
System.out.println("\nj1 is a better jet because it is newer!");
}
else if(j1.compareTo(j2)==0){
System.out.println("\nIt is nearly impossible to distiguish which jet is better");
}
else{
System.out.println("\nj2 is a better jet because it is newer!");
}
}
}
-------------------------------------------------------------------------------------------------
public class PassengerJet extends Jet {
private int numPassengers;
private int numEngines;
private boolean hasAutopilot;
public PassengerJet(String manufacturer, String model, int year, String owner, double grossWeightEmpty,
int lastOverhaul,int numOverhauls, int maxRecommendedFlightHours, int numPassengers,int numEngines, boolean hasAutopilot){
super(manufacturer,model,year,owner,grossWeightEmpty,lastOverhaul,numOverhauls,maxRecommendedFlightHours);
this.numPassengers=numPassengers;
this.numEngines=numEngines;
this.hasAutopilot=hasAutopilot;
}
public boolean isHardToFly(){
return !hasAutopilot;
}
public boolean needsLongRunway(){
return grossWeightEmpty>230000;
}
public int compareTo(PassengerJet o) {
if(this.hasAutopilot&&!o.hasAutopilot){
return 1;
}
else if(!this.hasAutopilot&&o.hasAutopilot){
return -1;
}
else{
return 0;
}
}
public String toString() {
return super.toString()+"Number of passengers available: "+numPassengers+", Number of engines: "+numEngines+", Autopilot feature: "+hasAutopilot;
}
public static void main(String[] args) {
PassengerJet pj1=new PassengerJet("Boeing","A-737",2010,"Korea Airline",2100.2,10,12,200,100,4,false);
PassengerJet pj2=new PassengerJet("Airbus","A320",2019,"Airbus airlines",2457.2,12,5,150,200,3,true);
System.out.println("pj1's specification: "+pj1);
System.out.println("The jet is hard to fly: "+pj1.isHardToFly());
System.out.println("The jet needs a long runway"+pj1.needsLongRunway());
System.out.println("\npj2's specification"+pj2);
System.out.println("The jet is hard to fly: "+pj2.isHardToFly());
System.out.println("The jet needs a long runway: "+pj2.needsLongRunway());
if(pj1.compareTo(pj2)==1){
System.out.println("\npj1 is a better passenger jet since it has autopilot");
}
else if(pj1.compareTo(pj2)==0){
System.out.println("\nIt is nearly impossible to distinguish which passenger jet is better");
}
else{
System.out.println("\npj2 is a better passenger jet since it has autopilot");
}
}
}
-------------------------------------------------------------------------------------------------
public class BusinessJet extends Jet{
private int numPassengers;
private boolean transOceanCertified;
public BusinessJet(String manufacturer, String model, int year, String owner, double grossWeightEmpty,
int lastOverhaul,int numOverhauls, int maxRecommendedFlightHours, int numPassengers, boolean transOceanCertified) {
super(manufacturer,model,year,owner,grossWeightEmpty,lastOverhaul,numOverhauls,maxRecommendedFlightHours);
this.numPassengers=numPassengers;
this.transOceanCertified=transOceanCertified;
}
public boolean isHighClass(){
return numPassengers>=40&&transOceanCertified;
}
public String toString() {
return super.toString()+"Number of passengers available: "+numPassengers+", The aircraft is certified to fly over ocean: "+transOceanCertified;
}
public int compareTo(BusinessJet o) {
if(this.transOceanCertified&&!o.transOceanCertified){
return 1;
}
else if(!this.transOceanCertified&&o.transOceanCertified){
return -1;
}
else{
return 0;
}
}
public static void main(String[] args) {
BusinessJet bj1=new BusinessJet("Boeing","A-737",2010,"Korea Airline",2100.2,10,12,100,40,false);
BusinessJet bj2=new BusinessJet("Airbus","A320",2019,"Airbus airlines",2457.2,12,5,130,50,true);
System.out.println("bj1's specification: "+bj1);
System.out.println("The jet is high class: "+bj1.isHighClass());
System.out.println("\nbj2's specification: "+bj2);
System.out.println("The jet is high class: "+bj2.isHighClass());
if(bj1.compareTo(bj2)==1){
System.out.println("\nbj1 is a better passenger jet since it has capability of more than 40 passengers");
}
else if(bj1.compareTo(bj2)==0){
System.out.println("\nIt is nearly impossible to distinguish which business jet is better");
}
else{
System.out.println("\nbj2 is a better passenger jet since it has capability of more than 40 passengers");
}
}
}
댓글