87.5k views
2 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 is 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 methods: • Constructor: The constructor should accept the car's year model and make as arguments. These values should be assigned to the object's yearModel and make fields. The constructor should also assign 0 to the speed field. • Accessor: The appropriate accessor methods should be implemented to access the values stored in the object's yearModel, make, and speed fields. • accelerate: The accelerate method should add 5 to the speed field when 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 contains a Car object, and then calls the accelerate method five times. After each call to the accelerate method, get the current speed of the car and print it on a separate line. Then, call the brake method five times, each time printing the current speed of the car on a separate line. Sample Run java Car 5↵ 10↵ 15↵ 20↵ 25↵ 20↵ 15↵ 10↵ 5↵ 0↵

1 Answer

4 votes

Answer:

Here is Car.java

public class Car { //class definition

//define private data members of class Car

private int yearModel; // this field is an int that holds the car's year model

private String make; // this field is a String object that holds the make of the car

private int speed; //an int field that holds the car's current speed.

public Car (int yearModel, String make) { //constructor that accept the car's year model and make as arguments

this.make = make; /*assigns make of Car to make of Constructor argument. Here this is used to refer to Car member variable and to avoid confusion between make variable of class and Constructor argument */

this.yearModel = yearModel; //assigns yearModel of Car to yearModel of Constructor argument

speed = 0; } //assign 0 to the speed field

public int getyearModel () //accessor method to access the value stored in object's yearModel

{ return yearModel; } //return the value of yearModel

public String getMake () //accessor method to access the value stored in object's make

{ return make; } //return the value of make

public int getSpeed () //accessor method to access the value stored in object's speed

{ return speed; } //return the value of speed

public void accelerate () //method that adds 5 to the speed field

{ speed = speed +5; } // adds 5 to the value of speed

public void brake () //subtracts 5 from the speed field

{ speed = speed -5; } //subtracts 5 from the value speed

}

Step-by-step explanation:

Here is the Main.java

public class Main { //class definition

public static void main(String[] args) { //start of main function

Car car1 = new Car(2000, "Civic"); //creates a Car object and assigns values to make and yearModel fields by calling constructor of Car using car1 object

for (int i = 1; i <= 5; i++) //iterates the loop 5 times to call accelerate method 5 times to add 5 to current speed

{ System.out.println("Accelerate!"); //prints Accelerate! on output screen

car1.accelerate(); //calls accelerate method to add 5 to the speed field

System.out.println("Current speed: " + car1.getSpeed()); } //displays the current speed by calling getSpeed() method using object car1

System.out.println("\\");//prints a new line

for (int j = 1; j <= 5; j++) { // iterates the loop 5 times to call brake method 5 times to subtract 5 from current speed

System.out.println("Brake!");// //prints Brake! on output screen

car1.brake(); ////calls brake method to subtract 5 from the speed field

System.out.println("Current speed: " + car1.getSpeed()); } } } //displays the current speed by calling getSpeed() method using object car1

If you want to take input from user then make the following changes to Constructor of Car:

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

this.make = make;

this.yearModel = yearModel;

speed = 0; }

Now use this Main.java instead to take input values from user:

import java.util.Scanner; //class used to take input from user

public class Main {

public static void main(String[] args) {

Scanner keyboard = new Scanner(System.in); //creates Scanner object

//below statement are used to take input for yearModel, make and speed from user

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

int yearModel = keyboard.nextInt();

keyboard.nextLine();

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

String make = keyboard.nextLine();

System.out.println("Enter the speed: ");

int speed = keyboard.nextInt();

//creates Car object named car1 and calls constructor of Car to pass values for yearModel, make and speed using car1 object

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

//this works the same as above Main.java

for (int i = 1; i <= 5; i++)

{ System.out.println("Accelerate!");

car1.accelerate();

System.out.println("Current speed: " + car1.getSpeed()); }

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

for (int j = 1; j <= 5; j++) {

System.out.println("Brake!");

car1.brake();

System.out.println("Current speed: " + car1.getSpeed()); }}}

Write a class named Car that has the following fields: • yearModel: The yearModel-example-1
Write a class named Car that has the following fields: • yearModel: The yearModel-example-2
User Dmitrievanthony
by
5.2k points