Avaka
Avaka

Reputation: 3

How to use toString in this example?

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();

    }

}

Upvotes: 0

Views: 1147

Answers (3)

Ana
Ana

Reputation: 831

There are a couple of issues here. One, Your question says wheelnumber and mpg should be private. Second, The datatype should be Integer. Hopefully below solves you question.

public class Vehicle {

  //should be private as per question
  private Integer wheelnumber;
  private Integer mpg;

  Vehicle (int wheelnum, int aMpg) {
    wheelnumber = wheelnum;
    mpg = aMpg;
  }

  public void display() {
    System.out.println("Wheels: " + wheelnumber.toString + " Mpg: " + mpg.toString);
  }

}

Now, to elaborate, The method toString() is defined in Java Object class, and any class that you define by default extends it.

The datatype you used - "int" is a primitive data type (taken directly from C - simple but not object oriented approach) which does not extend Java Object class. Thus, you cannot use toString() method with any instance of a primitive datatype.

Whereas "Integer" datatype is a class defined in Java API, it extends Object class and has toString() method defined in it. Thus, you can use toString() method with any instance of this class.

Having said that, if you absolutely have to convert an "int" variable to String, I suggest you do the below:

int i = 10;
System.out.println("Value of i: " + Integer.toString(i));

Note that, int or Integer can easily be converted to string without calling toString() method, as Java kind of automatically does that for you when you concatenate it with a String - something like this:

int p = 10;
Integer q = new Integer(20); 
System.out.println("value of p:" + p + ", q:" + q);

But, that is not the conventional way, and I guess not what your teacher wants in this assignment.

Upvotes: 3

emeraldjava
emeraldjava

Reputation: 11212

I would implement the toString() on Vehical

public class Vehicle {

    protected int wheelnumber;
    protected int mpg;

    Vehicle (int wheelnum, int aMpg) {
        this.wheelnumber = wheelnum;
        this.mpg = aMpg;
    }

    public void display() {
        System.out.println(this.toString());
    }

    @Override
    public String toString() {
        return "Wheels: " + this.wheelnumber + " Mpg: " + this.mpg;
    }
}

Upvotes: 0

Thomas Weller
Thomas Weller

Reputation: 59523

prints the required output using the toString() method to convert the integer values to String.

In this sentence, the teacher refers to Integer.toString(), see the Integer class. Likely, the overload Integer.toString(int i).

There are alternatives of converting an integer to a string, some of which appear to be simpler. But there might be a reason for the teacher to demand that particular approach.

Upvotes: 3

Related Questions