PLEASE CREATE RAPTOR FLOWCHART: Programming Excercise: 2 - Design a class named
ID: 3773924 • Letter: P
Question
PLEASE CREATE RAPTOR FLOWCHART: Programming Excercise: 2 -
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 reference 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. These values should be assigned to the object yearModel and make fields. th constructor should also assign 0 to the speed field.
Accessor: Design appropriate accessor method to give the values stored in an object's yearModel, make, and speed fields.
accelerate: The accelerate method should add 5 from the speed field each time it is called.
brake: The brake method should subtract 5 from rhe speed field each time it is called.
Next, design 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 break method, get current speed of the car and display it.
Car Class HINT: Your accelerate method should add 5 to the current speed of the car, so think of it something like Set this.speed = this.speed + 5. You would do the same thing for the brake method. Then, you can use a loop in your main module to call these methods and then display the status using the getSpeed method.
Explanation / Answer
here is my code and hop it is usefull
public class Car {
private int yearmake; // Variable yearmake of car
private String model; //Variable of car company
private int speed; // Variable of your car speed
// here mainly the getter and setter method as the pojo class defines that will give getter and setter property for the every variable inside class as according to OP it wont let u change Variable directly
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public int getYearmake() {
return yearmake;
}
public void setYearmake(int yearmake) {
this.yearmake = yearmake;
}
public Car(int speed) {
this.speed = speed;
}
// thus the constructor which will accepts and initialize your car with data i mean class
public Car(int yearmake, String model, int speed) {
this.yearmake = yearmake;
this.model = model;
this.speed = speed;
System.out.println("Year Make " + yearmake);
System.out.println("Model " + model);
System.out.println("Speed " + speed);
}
//here method of the making accelerate car by speed of 5
public int acclarate(int speed) {
speed = speed + 5;
System.out.println("Speed Acclarated " + speed);
return speed;
}
// method for reducing speed of 5
public int Breaking(int speed) {
speed = speed - 5;
System.out.println("Speed Breaking " + speed);
return speed;
}
// main method
public static void main(String[] args) {
// accept from user input
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the car's made year model: ");
int year = keyboard.nextInt();
keyboard.nextLine();
System.out.println("Enter the car's make Company: ");
String make = keyboard.nextLine();
System.out.println("Enter the car's speed: ");
int speedIn = keyboard.nextInt();
// initialize car model with constructor
Car c = new Car(year, make, speedIn);
//increasing speed with use of method and loop
for (int i = 0; i < 5; i++) {
int speedchange = c.acclarate(c.getSpeed());
c.setSpeed(speedchange);
}
//decreasing speed according to your requriement with use of method and loop
for (int i = 0; i < 5; i++) {
int decreasedpeed = c.Breaking(c.getSpeed());
c.setSpeed(decreasedpeed);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.