Question

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

java

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:

5↵

10↵

15↵

20↵

25↵

20↵

15↵

10↵

5↵

0↵

0 0
Add a comment Improve this question Transcribed image text
Answer #1

public class Car {
  
private int yearModel;
private String make;
private int speed;

public Car(int yearModel, String make) {
this.yearModel = yearModel;
this.make = make;
speed=0;
}

public int getYearModel() {
return yearModel;
}

public String getMake() {
return make;
}

public int getSpeed() {
return speed;
}
  
public void accelerate(){
speed=speed+5;
}
  
public void brake(){
  
if(speed>=5){
speed=speed-5;
}else{
speed=0;
}
}

  
  
}

TestProgram:

public class Abc74 {
public static void main(String[] args){
Car c=new Car(2017, "Nissan");
for(int i=0;i<5;i++){
c.accelerate();
System.out.println(c.getSpeed());
}
for(int i=0;i<5;i++){
c.brake();
System.out.println(c.getSpeed());
}
}
  
  
}

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
java Write a class named Car that has the following fields: • yearModel: The yearModel field...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • python Design a class named Car that has the following fields: yearModel: The yearModel field is...

    python 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. These values should be...

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

    Java Program 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, such as “Ford”, “Chevrolet”, etc. speed- This 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....

  • Car Class Background: Write a class named Car that will be used to store information and...

    Car Class Background: Write a class named Car that will be used to store information and control the acceleration and brake of a car. Functionality: 1. Write a class named "Car” that has the following member variables: • year Model - an int that holds the car's year model • make-A string that holds the make of the car • speed - an int that holds the car's current speed 2. In addition the class should have the following member...

  • In Python Programming: Write a class named Car that has the following data attributes: _ _year_model...

    In Python Programming: Write a class named Car that has the following data attributes: _ _year_model (for the car’s year model) _ _make (for the make of the car) _ _speed (for the car’s current speed) The Car class should have an _ _init_ _ method that accepts the car’s year model and make as arguments. These values should be assigned to the object’s _ _year_model and _ _make data attributes. It should also assign 0 to the _ _speed...

  • C++ Program 1a. Purpose Practice the creation and use of a Class 1b. Procedure Write a...

    C++ Program 1a. Purpose Practice the creation and use of a Class 1b. Procedure Write a class named Car that has the following member variables: year. An int that holds the car’s model year. make. A string object that holds the make of the car. speed. An int that holds the car’s current speed. In addition, the class should have the following member functions. Constructor. The constructor should accept the car’s year and make as arguments and assign these values...

  • (C++)This is the problem that I'm supposed to write a program for: This is the layout...

    (C++)This is the problem that I'm supposed to write a program for: This is the layout that we are supposed to fill out for the project: If anything is unclear, comment and I'll try to clarify. Thank you! Objective Reading Dala Files Project This is a variation of ng challenge 133 de Write a class named Car that has the following pelvate member variables. model Year An ie car's uodd year. make A string that bolds the make of the...

  • Room Class....... in JAVA!!!!! Write a class named Room that has the following fields: Length: Double...

    Room Class....... in JAVA!!!!! Write a class named Room that has the following fields: Length: Double variable that holds the rooms length(in feet). Breadth: The double variable that holds the rooms breadth(in feet). Height: Double variable that holds rooms height in feet. Color: String object that holds the color you want the room to be painted with. The class should have a constructor to initialize the fields for a particular instance (or object) of the class as given: All double...

  • 2. Create a class named Student that contains the following » idStudent. The idStudent is an...

    2. Create a class named Student that contains the following » idStudent. The idStudent is an int variable that holds the student's matric number. name. The name field references a String object holds the student's name. . major. The major field references a String object holds the student's major. . classification. The classification field references a String object holds the student's classification level (bongsu, kecil, muda, sulung) . Constructor should accept the student's matric number, name, major and classification as...

  • Write a java program that creates a class Car. Class Car contains the following private member...

    Write a java program that creates a class Car. Class Car contains the following private member fields: make, model, year, and miles. The class Car has a constructor that takes as arguments the car make, model, and year. In addition, the constructor sets miles to 100 as default. Class Car has accessors and mutators to get and set the make, model, year and miles on the car. The program should complete the following tasks: Ask the user to enter make,...

  • Lab 10C - Creating a new class This assignment assumes that you have read and understood...

    Lab 10C - Creating a new class This assignment assumes that you have read and understood the following documents: http://www.annedawson.net/Python3_Intro_OOP.pdf http://www.annedawson.net/Python3_Prog_OOP.pdf and that you're familiar with the following example programs: http://www.annedawson.net/python3programs.html 13-01.py, 13-02.py, 13-03.py, 13-04.py Instructions: Complete as much as you can in the time allowed. Write a Python class named "Car" that has the following data attributes (please create your own variable names for these attributes using the recommended naming conventions): - year (for the car's year of manufacture)...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT