Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Car Class Write a class named Car that has the following fields: yearModel . The

ID: 3793180 • Letter: C

Question

Car Class 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 is 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 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. Accessor . The appropriate accessor methods 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. USING BLUE J PROVIDE TWO SOURCE CODE SHOWING RELATIONSHIP OF THE CODE AND OUTPUT. ONE CODE WITH TEMPERATURE AND OTHER WITH TEMPERATUREDEMO. IN TOTAL, YOU SHOULD SUBMIT 6 SOURCE CODE EACH HAS TWO SOURCE CODE. BELOW IS THE SAMPLE OF HOW SHOULD LOOK public class HelloWorldDemo { public static void main(String [] args) { HelloWorld myMessage = new HelloWorld(); System.out.println(myMessage.getMessage()); } } public class HelloWorld { private final String MSG="Hello World"; public String getMessage(){ return MSG; } }

Explanation / Answer

CarTester.java

package Test12;

import java.util.Random;
import java.util.Scanner;

public class CarTester {

  
   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       System.out.print("Please enter the make and model of the case: ");
       String make = scan.next();
       int year = scan.nextInt();
       Car c = new Car(make, year);
       System.out.println("New car created! "+c.getMake()+", "+c.getYearModel());
       Random r = new Random();
       System.out.println("Calling accelerate method...");
       for(int i=0; i<5;i++){
           c.accelerate();
           System.out.println("The current speed is "+c.getSpeed());
       }
       System.out.println("Calling brake method...");
       int j=0;
       for(j=0;j<5;j++){
           c.brake();
           System.out.println("The current speed is "+c.getSpeed());
       }
   }

}

Car.java


public class Car {
   private int yearModel;
   private String make;
   private int speed;
   public Car(String make, int year){
       this.make=make;
       this.yearModel = year;
       this.speed=0;
   }
   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;
   }
   public void accelerate(){
       if(speed + 5 <= 210){
       speed+=5;
       }
   }
   public void brake(){
       if(speed - 5 >= 0){
       speed-=5;
       }
   }
}  

Output:

Please enter the make and model of the case: Santro 2007
New car created! Santro, 2007
Calling accelerate method...
The current speed is 5
The current speed is 10
The current speed is 15
The current speed is 20
The current speed is 25
Calling brake method...
The current speed is 20
The current speed is 15
The current speed is 10
The current speed is 5
The current speed is 0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote