Question

Write a class named Car that has the following fields: • yearModel: The yearModel field is...

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 should be implemented to access the values stored in the object's yearModel, make, and speed fields. • accelerate: The accelerate method should add 5 to the speed field when 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 contains 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 print it on a separate line. Then, call the brake method five times, each time printing the current speed of the car on a separate line.

Sample Run

java Car

5↵

10↵

15↵

20↵

25↵

20↵

15↵

10↵

5↵

0↵

Homework Answers

Answer #1

Code is Given Below:

========================

Car.java

======================

public class Car {
   //declaring variables
   private int yearModel;
   private String make;
   private int speed;
   //parameter constructor
   public Car(int yearModel, String make) {
       super();
       this.yearModel = yearModel;
       this.make = make;
       this.speed = 0;
   }
   //Accessor: methods
   public int getYearModel() {
       return yearModel;
   }
   public String getMake() {
       return make;
   }
   public int getSpeed() {
       return speed;
   }
   //accelerate method that will increase speed by 5
   public void accelerate() {
       speed=speed+5;
   }
   //accelerate method that will decrease speed by 5
   public void brake() {
       speed=speed-5;
   }
  

}

test class

CarDriver.java

=====================

public class CarDriver {
   public static void main(String[] args) {
       //creating car object
       Car c=new Car(2018,"Toyota");
       //calling accelerate 5 times and printing speed each time
       for(int i=1;i<=5;i++) {
           c.accelerate();
           System.out.println(c.getSpeed());
       }
      
       //calling brake 5 times and printing speed each time
       for(int i=1;i<=5;i++) {
           c.brake();
           System.out.println(c.getSpeed());
       }
      
   }

}

Output

==============

Code Snapshot:

==================

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Define a class named Employee that has four data fields: name: String hoursWorked: double hourlyPayrate: double...
Define a class named Employee that has four data fields: name: String hoursWorked: double hourlyPayrate: double bonusRate: double The class has two constructors: 1. constructor without arguments: initialize name to empty string, and all other data fields to 0.0 2. constructor with a String type arguments and three double type arguments. Use them to initialize the data fields properly The class also has: 1. Getter method for each data field to return the value of the data field 2. Setter...
Write code for a JAVA class for a "Car". The Car class has 2 properties: modelType...
Write code for a JAVA class for a "Car". The Car class has 2 properties: modelType and currentSpeed. Make sure to write code for instance variables (the properties), getters/setters and a default constructor, overloaded constructor. ALSO, include one method called "accelerate" that increases the speed by "1.0" miles per hour
Write a class called Hamburger in python The Hamburger class should have the following fields: weight...
Write a class called Hamburger in python The Hamburger class should have the following fields: weight – an int storing the weight of the hamburger patty in ounces doneness – a String which stores how well the burger is cooked i.e., rare, medium, well done, etc. cheese – a boolean indicating whether the burger has cheese or not toppings – a list of Strings storing the toppings on the burger i.e., lettuce, tomato, mayonnaise, etc. The Hamburger class should have...
Write Python code to define a class called Student. The Student class should have the following...
Write Python code to define a class called Student. The Student class should have the following private fields: name – to store the name of a student attend – to store how many times a student attend the class grades – a list of all student’s grades The Student class should have the following methods: a constructor getter method for the name field attendClass method: that increments the attend field (should be called every time a student attends the class)....
write a c++ program A class called car (as shown in the class diagram) contains: o...
write a c++ program A class called car (as shown in the class diagram) contains: o Four private variables: carId (int), carType (String), carSpeed (int) and numOfCars (int). numOfCars is a static counter that should be  Incremented whenever a new Car object is created.  Decremented whenever a Car object is destructed. o Two constructors (parametrized and copy constructor) and one destructor. o Getters and setters for the Car type, ID, and speed. And static getter function for numOfCars....
c++ Design a class named Account that contains (keep the data fields private): a) An int...
c++ Design a class named Account that contains (keep the data fields private): a) An int data field named id for the account. b) A double data field named balance for the account. c) A double data field named annualInterestRate that stores the current interest rate. d) A no-arg constructor that creates a default account with id 0, balance 0, and annualInterestRate 0. e) The accessor and mutator functions for id, balance, and annualInterestRate. f) A function named getMonthlyInterestRate() that...
Shapes2D Write the following four classes to practice using an abstract class and polymorphism. Submit all...
Shapes2D Write the following four classes to practice using an abstract class and polymorphism. Submit all four classes. Shape2D class For this class, include just an abstract method name get2DArea() that returns a double. Rectangle2D class Make this class inherit from the Shape2D class. Have it store a length and a width as fields. Provide a constructor that takes two double arguments and uses them to set the fields. Note, the area of a rectangle is the length times the...
Design a class that can be used to simulate rolling a multi-faced die. 2. Write a...
Design a class that can be used to simulate rolling a multi-faced die. 2. Write a Java class called Die in a class file called Die.java.    3. The Die class will have three private class attributes: a. The first attribute, called numSides, is of type int and represents the number of sides on the die. b. The second attribute, called sumRolls, is static of type int and will keep track of the sum of multiple die rolls by multiple...
in jGRASP INVENTORY CLASS You need to create an Inventory class containing the private data fields,...
in jGRASP INVENTORY CLASS You need to create an Inventory class containing the private data fields, as well as the methods for the Inventory class (object). Be sure your Inventory class defines the private data fields, at least one constructor, accessor and mutator methods, method overloading (to handle the data coming into the Inventory class as either a String and/or int/float), as well as all of the methods (methods to calculate) to manipulate the Inventory class (object). The data fields...
Exercise 2 Write a class named Vehicle Holds make and model (strings) and year (int) Contains...
Exercise 2 Write a class named Vehicle Holds make and model (strings) and year (int) Contains this method: Vehicle(String make, String model, int year) { this.make = make; this.model = model; this.year = year; } Add getters and setters for each variable Write a class called FordMustang that extends the Vehicle class               Create instance of object               Pass variables               Print
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT