Hello. I have created a superclass, a subclass, and a test class
Hello. I have created a superclass, a subclass, and a test class.
I am trying to print the toString() method from the subclass in the test class, but I’m having trouble. What am I doing wrong and what do I need to change? Thanks in advance.
import java.lang.String; public class Car { private int speed; private boolean power; private boolean brake; public Car (int speed, boolean power, boolean brake){ super(); this.speed = speed; this.power = power; this.brake = brake; } public Car(){ this.speed = 50; this.power = true; this.brake = false; } public void setSpeed() {this.speed = speed;} public void setPower() {this.power = power;} public void setBrake() {this.brake = brake;} public int getSpeed() {return this.speed;} public boolean getPower() {return this.power;} public boolean getBrake() {return this.brake;} public String toString() { return “Car variables: ” + speed + “, “+ power + “, ” + brake + “. “; } }
public class SportsCar extends Car { private String engine; public SportsCar(int speed , boolean power , boolean brake , String engine) { super(speed, power, brake); this.engine = engine; } public SportsCar() { this.engine = “V8”; } public void setEngine() { this.engine = engine; } public String getEngine() { return this.engine; } @Override public String toString() { return super.toString() + getEngine(); } }
public class CarTest { public static void main(String[] args){ SportsCar carOne = new SportsCar(50, false, true, “V6”); System.out.println(carOne); System.out.println(“Original Sports Car: ” + SportsCar.toString()); } }