So when the program runs it prints:
0.0
0.0
0.0
However it should print numbers that are calculated in the subclass. The subclass inherits all variables from the parent class. Types of SportCar are declared in the main method along with numbers that are assigned to the variables hp, w, and ts. Here is the code.
public class TestConsumption
{
public static void main (String[] args)
{
SportCar car1 = new SportCar(200, 1500, 220);
SportCar car2 = new SportCar(100, 1000, 170);
SportCar car3 = new SportCar(135, 1100.2, 173);
System.out.println(car1.computeConsumption());
System.out.println(car2.computeConsumption());
System.out.println(car3.computeConsumption());
}
}
THE SUBLASS
public class SportCar extends Vehicle
{
public double topspeed;
public SportCar(double hp, double w, double ts)
{
super(0.0, 0.0, 0.0);
topspeed = ts;
aerodynamics = 0.5;
}
public double getTopspeed()
{
return topspeed;
}
public double computeConsumption()
{
double fuelConsumption = (1000+(weight/5))*(topspeed/100)*(aerodynamics*horsepower)/10000;
return fuelConsumption;
}
}
THE PARENT CLASS
public class Vehicle
{
public double horsepower;
public double aerodynamics;
public double weight;
public Vehicle(double hp, double w, double ad)
{
horsepower = hp;
weight = w;
aerodynamics = ad;
}
public double getHorsepower()
{
return horsepower;
}
public double getAerodynamics()
{
return aerodynamics;
}
public double getWeight()
{
return weight;
}
}