-1

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;
}
}
halfer
  • 19,824
  • 17
  • 99
  • 186
  • In the sports car class change `super(0.0, 0.0, 0.0);` to `super(hp,w,ad);` in `public SportCar(double hp, double w, double ts)` constuctor. – StackFlowed Jan 30 '18 at 22:21
  • Possible duplicate of [super() in Java](https://stackoverflow.com/questions/3767365/super-in-java) – Dmitrii Z. Jan 31 '18 at 00:45

3 Answers3

1

You aren't passing your parameters to the parent constructor, you simply ignore them and put 0.0.

public SportCar(double hp, double w, double ts)
{
    super(0.0, 0.0, 0.0);
    ...
}

to

public SportCar(double hp, double w, double ts)
{
    super(hp, w, ts);
    ...
}
RAZ_Muh_Taz
  • 4,059
  • 1
  • 13
  • 26
0

Pass your arguments into the parents constructor then get their values using the getters

Paul Brittain
  • 319
  • 2
  • 17
0

I believe it's because your SuperCar constructor automatically sets the horsepower, aerodynamics, and the weight to 0. Though you do set the aerodynamics to 0.5, when you calculate the fuel consumption you multiply by the horsepower, which you set to be 0, and so you'll always receive 0 as an answer. The correct constructor should be

`public SuperCar(double hp, double w, double ts){
super(hp, w, 0.5);
topspeed = ts;
}`

As a sidenote, you all your class data really should be either private or sometimes protected, but almost never public.