226k views
3 votes
Design a class named Car that has the following fields:

year Model: an int instance variable that holds the car’s year model.
make: a string instance variable that holds the make of the car.
speed: an int instance variable that holds the car’s current speed.
In addition, the class should have the following methods.
Constructor 1: no-arg constructor set the yearModel, make and speed to 2000, Nissan and 4, respectively.
Constructor 2: this is an overloaded constructor. This constructor should accept the car’s year model, speed, and make as arguments. These values should be assigned to the object’s yearModel, speed and make fields.
Accessor (getters): appropriate accessor methods to get the values stored in an object’s yearModel, make, and speed fields.
Mutator (setters): appropriate mutator methods to store values in an object’s yearModel, make, and speed fields.
toString: returns a string describing the object’s yearModel, make and speed.

User Rinzwind
by
4.9k points

1 Answer

3 votes

Answer:

public class Car {

private int yearModel;

private String make;

private int speed;

public Car(){

yearModel = 2000;

make = "Nissan";

speed = 4;

}

public Car(int yearModel, String make, int speed) {

this.yearModel = yearModel;

this.make = make;

this.speed =speed;

}

public void setYearModel(int yearModel){

this.yearModel = yearModel;

}

public void setMake(String make){

this.make = make;

}

public void setSpeed(int speed){

this.speed = speed;

}

public int getYearModel(){ return yearModel; }

public String getMake(){ return make; }

public int getSpeed(){ return speed; }

public String toString(){

return "Car's year model: " + getYearModel() + ", make: " + getMake() + ", speed: " + getSpeed();

}

}

Step-by-step explanation:

Variables are declared.

No-arg constructor is created with default values.

A constructor with parameters is created.

The required set methods and get methods are created.

toString method is created to return car's specifications.

User Steven Borg
by
4.7k points