98.7k views
4 votes
Write a class named Car that has the following fields:

yearModel: The yearModel field is an int that holds the car’s year model.

make: The make field references a String object that holds the make of the car.

speed: The speed field is an int that holds the car’s current speed.

In addition, the class should have the following constructor and other methods:

Constructors: One constructor should accept the car's year model, make, and speed as arguments. These values should be assigned to the object's yearModel, make, and speed fields. Another constructor will have no arguments and will assign 0 as the car's year model and speed and an empty string ("") as the make.

Accessors: Appropriate accessor methods should get the values stored in an object's yearModel, make, and speed fields.

Mutators: Appropriate mutator methods should store values in an object's yearModel, make, and speed fields.

accelerate: The accelerate method should add 5 to the speed field each time it is called.

brake: The brake method should subtract 5 from the speed field each time it is called.

Demonstrate the class in a program that asks the user to input data and then creates a Car object. It then calls the accelerate method five times. After each call to the accelerate method, get the current speed of the car and display it. Then call the brake method five times. After each call to the brake method, get the current speed of the car and display it.

The output from running this program will appear similar to:

Enter the car's year model: 1965
Enter the car's make: Mustang
Enter the car's speed: 30

Current status of the car:
Year model: 1965
Make: Mustang
Speed: 30

Accelerating...
Now the speed is 35

Accelerating...
Now the speed is 40

Accelerating...
Now the speed is 45

Accelerating...
Now the speed is 50

Accelerating...
Now the speed is 55

Braking...
Now the speed is 50

Braking...
Now the speed is 45

Braking...
Now the speed is 40

Braking...
Now the speed is 35

Braking...
Now the spee

User Sentenza
by
3.5k points

1 Answer

4 votes

Answer:

The Solution in Java programming language is provided in the explanation section.

Please observe comments that further explains each step

Step-by-step explanation:

import java.util.Scanner;

//This is the CarTest Class

public class num1 {

//The main Method in the Test Class

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

//Receiving Users inputs

System.out.print("Enter the car's year model: ");

int yearModel = in.nextInt();

System.out.print("Enter the car's make: ");

String make = in.next();

System.out.print("Enter the car's speed: ");

int speed = in.nextInt();

Car newCar = new Car(yearModel,make, speed);

//Printing Cars Current Status

System.out.println("Current status of the car:");

System.out.println("Year Model: "+newCar.getYearModel());

System.out.println("Make: "+newCar.getMake());

System.out.println("Speed: "+newCar.getSpeed());

//Calling the accelerate Method

for(int i=1; i<6; i++){

int newSpeed = newCar.getSpeed();

newCar.setSpeed(newSpeed);

newCar.accelerate();

System.out.println("Accelerating...");

System.out.println("Now the Speed is "+newCar.getSpeed());

}

//Calling the Breaking Method

for(int i=1; i<6; i++){

int newSpeed = newCar.getSpeed();

newCar.setSpeed(newSpeed);

newCar.breaking();

System.out.println("Breaking...");

System.out.println("Now the Speed is "+newCar.getSpeed());

}

}

}

//The Car CLass begins here

class Car{

private int yearModel;

private String make;

private int speed;

// Cnstructors

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

this.speed = speed;

this.make = make;

this.yearModel = yearModel;

}

public Car(){

this.yearModel =0;

this.speed = 0;

this.make = "";

}

//Accessor Methods

public int getYearModel() {

return yearModel;

}

public String getMake() {

return make;

}

public int getSpeed() {

return speed;

}

//Mutators

public void setYearModel(int yearModel) {

this.yearModel = yearModel;

}

public void setMake(String make) {

this.make = make;

}

public void setSpeed(int speed) {

this.speed = speed;

}

//Method Accelerate

public void accelerate(){

this.speed = this.speed + 5;

}

//Method Break

public int breaking(){

return this.speed = this.speed - 5;

}

}

User Ronald Conco
by
3.9k points