Currently doing some homework and having trouble - I'm not sure how to use toString correctly. Here's the homework question:
Create a class named Vehicle that acts as a superclass for vehicle types. The Vehicle class contains private variables for the number of wheels and the average number of miles per gallon. The Vehicle class also contains a constructor with integer arguments for the number of wheels and average miles per gallons, and a display() method that prints the required output using the toString() method to convert the integer values to String.
Create two subclasses, Car and MotorCycle, that extend the Vehicle class. Each subclass contains a constructor that accepts the miles-per-gallon value as an argument and forces the number of wheels to the appropriate value—2 for a MotorCycle and 4 for a Car. Use the superclass constructor to set the wheels and mpg data fields (use the super keyword).
Write a UseVehicle class to instantiate one object of each subclass and display the object’s values. Save the files as Vehicle.java, Car.java, MotorCycle.java, and UseVehicle.java
public class Vehicle {
protected int wheelnumber;
protected int mpg;
Vehicle (int wheelnum, int aMpg) {
wheelnumber = wheelnum;
mpg = aMpg;
}
public void display() {
System.out.println("Wheels: " + toString(wheelnumber) + " Mpg: " + toString(mpg));
}
}
public class Car extends Vehicle{
Car (int CarMPG) {
super(4, CarMPG);
}
}
public class Motorcycle extends Vehicle{
Motorcycle (int MotorcycleMPG) {
super(2, MotorcycleMPG);
}
}
public class UseVehicle {
public static void main(String[] args) {
Car Car1 = new Car(30);
Motorcycle Motorcycle2 = new Motorcycle(60);
System.out.print("Car--> ");
Car1.display();
System.out.print("Motorcycle—> ");
Motorcycle2.display();
}
}