Question

Car Class Background: Write a class named Car that will be used to store information and control the acceleration and brake o
Car Class Background: Write a class named Car that will be used to store information and control the acceleration and brake o
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Car.java

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

public Car(int yearModel, String make) {
this.yearModel = yearModel;
this.make = make;
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()
{
setSpeed(getSpeed() + 5);
}
  
public void brake()
{
if(getSpeed() - 5 == 0)
System.out.println("Car has stopped!\n");
else
setSpeed(getSpeed() - 5);
}
}

CarTest.java (Main class)

public class CarTest {
  
public static void main(String[] args)
{
Car car = new Car(2006, "Toyota");
  
System.out.println("Accelerating 5 times..");
for(int i = 0; i < 5; i++)
{
car.accelerate();
System.out.println("Current speed: " + car.getSpeed());
}
  
System.out.println("\nBraking 5 times..");
for(int i = 0; i < 5; i++)
{
car.brake();
System.out.println("Current speed: " + car.getSpeed());
}
}
}

********************************************************* SCREENSHOT *******************************************************

run: Accelerating 5 times.. Current speed: 5 Current speed: 10 Current speed: 15 Current speed: 20 Current speed: 25 Braking

Add a comment
Know the answer?
Add Answer to:
Car Class Background: Write a class named Car that will be used to store information and...
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
  • 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...

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

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

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

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

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

  • write a C++ program that uses a class named Movie to store the following information about...

    write a C++ program that uses a class named Movie to store the following information about a movie: - Title - Director - Year Released - Running time (in minutes) + print() : Function that will print the information for this movie in a nice format Include a constructor that allows all four of these member data values to be specified at the time a Movie variable is created. The program should create four Movie variables and call its print()...

  • Write a program to define ​a ​virtual car. Car has these features: number of wheels, max speed, current speed, isMoving, remainingFuel, distanceTravelled. The car can move when the programmer decides...

    Write a program to define ​a ​virtual car. Car has these features: number of wheels, max speed, current speed, isMoving, remainingFuel, distanceTravelled. The car can move when the programmer decides to accelerate. With each acceleration current speed increases by 10. Fuel decreases depending on the speed of the car and follows this formula: fuel= fuel-(speed/2). Your program should display “Out of fuel” when there is no remaining fuel. Fuel starts form 100. ​Use a constructor to initialize the car object....

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

  • Codes: (car.h ; car.cpp ; carDemo.cpp) /* ----------------------- Car ----------------------- - make: string - year :...

    Codes: (car.h ; car.cpp ; carDemo.cpp) /* ----------------------- Car ----------------------- - make: string - year : int ----------------------- + Car() + setMake(m: string) : void + getMake() : string + setYear(y: int) : void + getYear() : int ---------------------- */ #ifndef CAR_H #define CAR_H #include <iostream> using namespace std; class Car { private: string make; int year; public: Car(); Car(string); Car(int); Car(string, int); void setMake (string); string getMake() {return make;} void setYear (int); int getYear() {return year;} }; #endif #include...

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