Question

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. 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.
  • decelerate- The decelerate method should decrease the speed field by 8 each time it is called.

CarApp class:

Write a CarApp class that does the following:

  1. Create a Car object using the values 2010 for yearModel and “Ford” for make.
  2. Call the accelerate method five times and increase the speed by five each time.
  3. After each acceleration, retrieve and display the speed.
  4. Call the decelerate method three times and decrease the speed by 8 each time.
  5. After each deceleration, retrieve and display the speed.

Sample run:

Current speed: 5

Current speed: 10

Current speed: 15

Current speed: 20

Current speed: 25

Current speed: 17

Current speed: 9

Current speed: 1

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


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 += 5;
    }

    public void brake() {
        speed -= 8;
    }
}

class TestCar {
    public static void main(String[] args) throws IllegalArgumentException {
        Car car = new Car(2018, "Ferrari");
        for (int i = 0; i < 5; i++) {
            car.accelerate();
            System.out.println("Current speed: " + car.getSpeed());
        }
        for (int i = 0; i < 3; i++) {
            car.brake();
            System.out.println("Current speed: " + car.getSpeed());
        }
    }
}
Add a comment
Know the answer?
Add Answer to:
Java Program 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 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...

  • 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...

  • 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...

  • 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...

  • 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,...

  • 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...

  • C++ Programing In this exercise, you will design a class memberType. The class has the following...

    C++ Programing In this exercise, you will design a class memberType. The class has the following data members: memberName. A string that holds the name of a person memberID. A string that holds the member identification number numBooks. An int that holds the number of books bought purchaseAmt. A double that holds the amount spent on books In addition, the class should have the following constructor and other member functions. Constructor. The constructor should accept the person’s name and member...

  • Write a class named PhoneBookEntry that has fields for a person's name and phone number.The class...

    Write a class named PhoneBookEntry that has fields for a person's name and phone number.The class should have a constructor and appropriate accessor and mutator methods. Thenwrite a program that creates at least five PhoneBookEntry objects and stores them in anArrayLitt. Use a loop to display the contents of each object in the ArrayList.

  • Description: This project focuses on creating a Java class, Date.java, along with a client class, DateCleint.java,...

    Description: This project focuses on creating a Java class, Date.java, along with a client class, DateCleint.java, which will contain code that utilizes your Date.java class. You will submit both files (each with appropriate commenting). A very similar project is described in the Programming Projects section at the end of chapter 8, which you may find helpful as reference. Use (your own) Java class file Date.java, and a companion DateClient.java file to perform the following:  Ask user to enter Today’s...

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