Question: ***Need help with my code, not sure why it isn't working, also need hlep on converting while loops to do while loops*** Problem Statement: Write

***Need help with my code, not sure why it isn't working, also need hlep on converting while loops to do while loops***

Problem Statement:

Write a class named Car that has the following fields:

yearModel: The yearModel field is an int that holds the car's year model (input is entered by user)

make: The make fiels is a String object that holds the make of the car (input is entered by user)

speed: The speed fields 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 constrictor should also

assign 0 (zero) to the speed field.

Appropriate accessor methods to 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 5 times. After each call to the brake method, get the current speed of the car and display it.

__________________________________

public class Car {

private int yearModel;

private String make;

private double speed;

//constructor

public Car(int yearModel, String make) {

this.yearModel = yearModel;

this.make = make;

this.speed = 0;

}

//Accessors methods

public int getYearModel() {

return yearModel;

}

public String getMake() {

return make;

}

public double getSpeed() {

return speed;

}

//accelerator

public void accelerate(){

speed+=5;

}

//break

public void brake(){

speed-=5;

}

}

________________________________

import java.util.Scanner;

public class CarDemo {

public static void main(String[] args) {

//Declaring variables

Car myCar;

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("\tDemonstration 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 myCar=new Car ( year, make);

System.out.println(myCar);

System.out.println("Speed Up! ");

//Accelerating for 5 times

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

{

myCar.accelerate();

System.out.println("Democar's speed:"+myCar.getSpeed());

}

System.out.println(" Slow down! ");

//Applying brake for 5 times

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

{

myCar.brake();

System.out.println("Democar's speed:"+myCar.getSpeed());

}

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!