In: Computer Science
Car class Question
Design a class named car that has the following fields: YearModel: The yearModel field is an integer that holds the car's year model. Make: The make field references a string that holds the make of the car. Speed: The speed field is an integer 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. The values should be assigned to the object's yearModel and make fields. The constructor should also assign 0 to the speed field. Accessors: Design 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. Next, design a program that creates a car object, and then cals the accelerate method fibe 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..........
Thanks for the help guys pseudocode and flowchart please
|| UPDATED ||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
JAVA CODE :-
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
CAR Class :

public class Car {
private int yearModel;
private String make;
private int speed;
public Car(int yM, String m){
setYearModel(yM);
setMake(m);
setSpeed(0);
}
void accelerate() {
setSpeed(getSpeed()+5);
}
void brake() {
setSpeed(getSpeed()-5);
}
public int getYearModel() {
return yearModel;
}
public void setYearModel(int yearModel) {
this.yearModel = yearModel;
}
public String getMake() {
return make;
}
public void setMake(String make) {
this.make = make;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
void getCurrentSpeed() {
System.out.println("Current Speed
of the Car object "+getMake()+" "+getYearModel()+" is
"+getSpeed());
}
}
PSEUDOCODE :-


car class
private interger variable 'yearModel'
private String variable 'make'
private interger variable 'speed'
public contructor 'car' takes two arguments - 'integer
argument1 , String argument2'
set the year of model to
argument1
set maker to argument2
set speed to 0
void method 'accelerate'
increase the value of variable
'speed' of object by 5 units
void method 'brake'
decrease the value of variable
'speed' of object by 5 units
public integer method 'getYearModel'
returns the variable
'yearModel'
public void method 'setYearModel' takes an integer
argument
set the value of passed argument to
the variable 'yearModel'
public String method 'getMake'
returns the variable 'make'
public void method 'setMake' takes an String
argument
set the value of passed argument to
the variable 'make'
public integer method 'getSpeed'
returns the value of variable
'speed'
public void method 'setSpeed' takes an integer
argument
set the value of passed argument to
the variable 'speed'
void method 'getCurrentSpeed'
appends the details of the car
object on console
exit
FLOW CHART :-

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
MAIN Class :

public class Main {
public static void main(String[] args) {
Car car1 = new
Car(1998,"FORD");
Car car2 = new
Car(1995,"LOTUS");
for(int i = 0 ; i < 5 ; i++ )
{
car1.accelerate();
car1.getCurrentSpeed();
}
for(int i = 0 ; i < 5 ; i++ )
{
car1.brake();
car1.getCurrentSpeed();
}
for(int i = 0 ; i < 5 ; i++ )
{
car2.accelerate();
car2.getCurrentSpeed();
}
for(int i = 0 ; i < 5 ; i++ )
{
car2.brake();
car2.getCurrentSpeed();
}
}
}
PSEUDOCODE :-

main class
main method
first object of car is initiated using
constructor
second object of car is initiated using
constructor
for range 0 to 4
first object of car
accelerates
appends the updated speed
for range 0 to 4
first object of car
decelerates
appends the updated speed
for range 0 to 4
second object of car
accelerates
appends the updated speed
for range 0 to 4
second object of car
decelerates
appends the updated speed
exit
FLOWCHART :-

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
OUTPUT :-
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

|SORRY FOR ANY INCONVENIENCE|