In: Computer Science
Write in Java. Separate code for each question. The answer to the first question should NOT be comparable.
1. Write a class to represent a AlternativeEnergyCar. Select the
fields and methods that fit the modeling of an alternative energy
car. Make sure to include code for the constructors, set/get
methods, a toString() method.
2. Inheritance – Create two abstract subclasses of AECar, one for Electric cars and one for Altfuel cars. Next create four additional subclasses., two for types of Electric cars and two for Altfuel cars( for example, Altfuel cars might be Hydrogen powered or Biofuel powered.) Decide which properties should be pushed up into a super abstract class and which belong in the individual subclasses. You do not need to code every method – just place a stub placeholders. (This answer should be a UML).
>>Answer
>>Given
AlternativeEnergyCar.java
public class AlternativeEnergyCar implements
Comparable<AlternativeEnergyCar>
{
protected double miles;
protected int yearmade;
protected double price;
public AlternativeEnergyCar()
{
this.miles = 0;
this.yearmade = 2019;
this.price = 50000;
}
public AlternativeEnergyCar(double miles, int yearmade, double
price)
{
this.miles = miles;
this.yearmade = yearmade;
this.price = price;
}
public void setMiles(double miles)
{
this.miles = miles;
}
public void setYearMade(int yearmade)
{
this.yearmade = yearmade;
}
public void setPrice(double price)
{
this.price = price;
}
public double getMiles()
{
return this.miles;
}
public int getYearMade(int yearmade)
{
return this.yearmade;
}
public double getPrice()
{
return this.price;
}
public String toString()
{
return "\nmiles: "+miles+"\nyear made: "+yearmade+"\nprice:
"+price;
}
public int compareTo(AlternativeEnergyCar otherAECar)
{
return
-1*(Double.valueOf(otherAECar.getPrice()).compareTo(this.price));
}
}
HydrogenCar.java
public class HydrogenCar extends NoEmissionsCar
{
private boolean infastructure;
public HydrogenCar()
{
this.infastructure = false;
}
public HydrogenCar(double miles, int yearmade, double price, double
fuelcost, boolean infastructure)
{
super(miles, yearmade, price, fuelcost);
this.infastructure = infastructure;
}
@Override
public void setFuelCost(double fuelcost)
{
this.fuelcost = fuelcost;
}
@Override
public double getFuelCost()
{
return this.fuelcost;
}
public void setInfastructure(boolean infastructure)
{
this.infastructure = infastructure;
}
public boolean getInfastructure(boolean infastructure)
{
return this.infastructure;
}
@Override
public String toString()
{
return "\nmiles: "+miles+"\nyear made: "+yearmade+"\nprice:
"+price+"\nfuel cost: "+fuelcost+"\ninfastructure:
"+infastructure;
}
}