Write a class named Car that has the following fields: year Model. The year Mode
ID: 3574532 • Letter: W
Question
Write a class named Car that has the following fields: year Model. The year Model 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: Constructor. The constructor should accept the car's year model and make as arguments. These values should be assigned to the object's year Model and make fields. The constructor should also assign 0 to the speed field. Accessors. Appropriate accessor methods should get the values stored in an object's year Model, 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. Using the Car class Demonstrate the class in a program that creates a Car object, and then calls the accelerate method five times. After each call to the accelerate methods, 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.Explanation / Answer
import java.util.*;
import java.lang.*;
import java.io.*;
class Car
{
private int yearModel;
private String make;
private int speed;
public Car(int yearModel,String make) //parameterized constructor
{
this.yearModel = yearModel;
this.make = make;
this.speed = 0;
}
public int getYearModel() //accessor methods for variables
{
return yearModel;
}
public String getMake()
{
return make;
}
public int getSpeed()
{
return speed;
}
public void accelerate() //increase speed by 5
{
speed = speed +5;
}
public void brake() //decrease speed by 5
{
speed = speed -5;
}
}
class TestCar
{
public static void main (String[] args)
{
Car car = new Car(2009,"Ford IKON");
System.out.println("Car Model Year : "+car.getYearModel()+" Make: "+car.getMake());
System.out.println("Accelerating........"); //calling accelerate() 5 times
car.accelerate();
System.out.println("Speed : "+car.getSpeed());
car.accelerate();
System.out.println("Speed : "+car.getSpeed());
car.accelerate();
System.out.println("Speed : "+car.getSpeed());
car.accelerate();
System.out.println("Speed : "+car.getSpeed());
car.accelerate();
System.out.println("Speed : "+car.getSpeed());
System.out.println("Braking.............."); //calling brake 5 times
car.brake();
System.out.println("Speed : "+car.getSpeed());
car.brake();
System.out.println("Speed : "+car.getSpeed());
car.brake();
System.out.println("Speed : "+car.getSpeed());
car.brake();
System.out.println("Speed : "+car.getSpeed());
car.brake();
System.out.println("Speed : "+car.getSpeed());
}
}
output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.