Home > Software design >  Creating a basic class in Java and the toString method
Creating a basic class in Java and the toString method

Time:09-27

I am new to Java and working through some coding assignments to better my understanding. In this particular one, I am making a Car class so that it conforms to the following UML:

-make: String
-year: int
-speed: double

 Car(m:String,y:int)
 toString(): String
 getMake(): String
 getSpeed(): double
 getYear(): int
 accelerate(): void
 brake(): void

First, I am creating three instance variables within the class: an int that holds the car's model year, a String that holds the car's make, a double that holds the car's speed. These should be listed as private.

Next, I am to create a constructor that accepts the model year and make and sets the speed to 0. Note that either the constructor's parameters should be a different name than the instance variables or use the " this " qualifier when placing the parameter's value into the instance variable.

So far, this is my code per the given instructions:

    public class Car
{
private int year;
private String make;
private double speed;

public Car(String make, int year) {
   this.make = make;
   this.year = year;
   speed = 0;
public String toString(){

So my first question is-- how do I set the speed equal to zero using the "this" keyword? Should I just do speed=0 under the constructor?

My next question is in regards to the following instruction: "Code the toString method. This method should take no parameters and should return a string generated using the String.format method:

String.format("A %d %s that is going %.1f mph", year, make, speed)

Now, compile your Car class and correct any errors

So I have done research on the toString format and the String.format but am unsure of where to place it in the code. Would it just be one line after the public static void main(String[] args)?

CodePudding user response:

So my first question is-- how do I set the speed equal to zero using the "this" keyword? Should I just do this.year=0?

Yes.

In java every Object has toString() method. If you don't override it, it will give you class name and hash code. To override method you need to add @override before your method. The final result should be like this:

public class Car {

    private int year;
    private String make;
    private double speed;

    public Car(int year, String make) {
        this.year = year;
        this.make = make;
        this.speed = 0;
    }

    @Override
    public String toString() {
        return String.format("A %d %s that is going %.1f mph", year, make, speed);
    }
}

CodePudding user response:

how do I set the speed equal to zero using the "this" keyword? Should I just do this.year=0?

Yes since the constructor takes in no parameters to set the speed, it would make sense to do:

this.speed = 0 // or speed = 0 since there is no conflict in name with the incoming parameters

so your constructor for the Car class would look like: (I prefer using the this keyword because it helps me stay consistent and know that I'm dealing with an instance variable when I see it.)

public Car(int year, String make) {
   this.year = year;
   this.make = make;
   this.speed = 0;
}

Every class in java has a default implementation for the to toString method.

Default behavior of toString() is to print class name, then @, then unsigned hexadecimal representation of the hash code of the object.

If you want a different behaviour, you can override the method:

@Override
public String toString() {
    return String.format("A %d %s that is going %.1f mph", year, make, speed);
}

This will be included within the Car class.

  • Related