Write a class named Car that has the following fields: -yearModel. The yearModel
ID: 3713982 • Letter: W
Question
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.
- 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.
- Accessors. Appropriate accessor methods should get the values stored 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 creates 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 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 answer should be the puesdocode NO other language just puesdocode. Will be running through IDE
Thanks for the help!
Explanation / Answer
Car.java
public class Car {
//Declaring instance variables
private int year_model;
private String make;
private int speed;
//Parameterized constructor
public Car(int year_model, String make) {
super();
this.year_model = year_model;
this.make = make;
this.speed = 0;
}
//This method will increase the speed of the car by 5 mph
public void accelerate()
{
speed+=5;
}
//This method will decrease the speed of the car by 5 mph
public void brake()
{
speed-=5;
}
//Getting the speed of the car
public int getSpeed() {
return speed;
}
//Display the contents of the Car class object
@Override
public String toString() {
System.out.println("demoCar");
System.out.println(" Model Year:"+year_model);
System.out.println(" Make:"+make);
System.out.println(" Speed:"+speed);
return "";
}
}
_________________
CarDemo.java
import java.util.Scanner;
public class CarDemo {
public static void main(String[] args) {
//Declaring variables
String make;
int year,speed;
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
//Gettinng the inputs entered by the user
System.out.println(" Demonstration of car class");
System.out.print("Enter the make of the car:");
make=sc.nextLine();
while(make.isEmpty())
{
System.out.println(" Input error - enter a car make");
System.out.print("Enter the make of the car:");
make=sc.nextLine();
}
/* This while loop continues to execute
* until the user enters a valid year number
*/
while(true)
{
System.out.print("Enter the year of the car:");
year=sc.nextInt();
if(year<1940 || year>2016)
{
System.out.println("Input error - enter a year between 1940 and 2016");
continue;
}
else
break;
}
/* Creating an instance of Car class object by passing
* user entered inputs as arguments
*/
Car c=new Car(year, make);
System.out.println(c);
System.out.println("Speed Up! ");
//Accelerating for 5 times
for(int i=0;i<5;i++)
{
c.accelerate();
System.out.println("Democar's speed:"+c.getSpeed());
}
System.out.println(" Slow down! ");
//Applying brake for 5 times
for(int i=0;i<5;i++)
{
c.brake();
System.out.println("Democar's speed:"+c.getSpeed());
}
}
}
__________________
Output:
Demonstration of car class
Enter the make of the car:
Input error - enter a car make
Enter the make of the car:chevy
Enter the year of the car:1000
Input error - enter a year between 1940 and 2016
Enter the year of the car:2015
demoCar
Model Year:2015
Make:chevy
Speed:0
Speed Up!
Democar's speed:5
Democar's speed:10
Democar's speed:15
Democar's speed:20
Democar's speed:25
Slow down!
Democar's speed:20
Democar's speed:15
Democar's speed:10
Democar's speed:5
Democar's speed:0
_____________Could you rate me well.Plz .Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.