Question

java bluej Create a class called Vehicle, that contains five fields, current speed, power, accelerate, decelerate...

java bluej

Create a class called Vehicle, that contains five fields, current speed, power, accelerate, decelerate and top speed, all of which type is int.  Define a constructor that takes and sets the accelerate, decelerate; sets the power to 20, and top speed to 120. Leave the current speed as 0. Also define a constructor that takes no parameters. The power field should be set to the value of 30 in this constructor, top speed to 160, and accelerate and decelerate to reasonable amounts. Leave the current speed as 0. Define the mutators speed up and brake, whose effect is to increase or decrease the value of current speed by the (power + accelerate) and (power + decelerate) respectively. The mutator methods should not let the current speed to be set to a value higher than the top speed, or lower than zero. Add a mutator method that sets the value of the power. Add a check to make sure that the new power value is not set higher than 50 or lower than 10.

Define an accessor method to return the value of current speed.

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

class Vehicle {
   private int currentSpeed;
   private int power;
   private int accelerate;
   private int decelerate;
   private int topSpeed;
   public Vehicle(int aAccelerate, int aDecelerate) {
       super();
       accelerate = aAccelerate;
       decelerate = aDecelerate;
       power=20;
       currentSpeed=0;
       topSpeed=120;
   }
   public Vehicle() {
       accelerate = 10;
       decelerate = 10;
       power=30;
       topSpeed=160;
       currentSpeed=0;
   }
   public void brake(int s) {
       s=s+power;
       currentSpeed-=s;
       if(currentSpeed<0)
           currentSpeed=0;
       if(currentSpeed>topSpeed)
           currentSpeed=topSpeed;
   }
   public void setCurrentSpeed(int aCurrentSpeed) {
       if(aCurrentSpeed<=10 && aCurrentSpeed<=50)
       currentSpeed = aCurrentSpeed;
   }
   public int getCurrentSpeed() {
       return currentSpeed;
   }
   @Override
   public String toString() {
       return "Vehicle [currentSpeed=" + currentSpeed + ", power=" + power + ", accelerate=" + accelerate
               + ", decelerate=" + decelerate + ", topSpeed=" + topSpeed + "]";
   }
  
}

public class TestVehicle {
   public static void main(String[] args) {
       Vehicle v = new Vehicle();
       System.out.println(v);
   }
}

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me

Add a comment
Know the answer?
Add Answer to:
java bluej Create a class called Vehicle, that contains five fields, current speed, power, accelerate, decelerate...
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
  • 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...

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

  • USING BLUEJ: You will write from scratch a class called Heater that represents an adjustable thermostat....

    USING BLUEJ: You will write from scratch a class called Heater that represents an adjustable thermostat. Follow the detailed steps below. This is based on Exercises 2.93 - 2.94 (6e) / 2.92 - 2.93 (5e) in the book, but with slight modifications, so be sure to follow the instructions below. Create a new BlueJ project named LastName-heater using your last name. Create a class named Heater At the top of the source code for Heater, add documentation like this: /**...

  • JAVA PROGRAMING - TESTING CLASSES AND INHERITANCE Create two classes - a vehicle class and gasguzzler...

    JAVA PROGRAMING - TESTING CLASSES AND INHERITANCE Create two classes - a vehicle class and gasguzzler class. Neither classes have I/O. Use a test class as well. Vehicle class has methods and properties that all vehicles have. This class has two properties - speed (rate of travel in miles per hour) and weight (in pounds) Vehicle Class Public Methods: Vehicle objects can be constructed 2 different ways: by specifying the weight and the speed, or by specifying the weight only...

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

  • Car class Question Design a class named car that has the following fields: YearModel: The yearModel...

    Car class Question 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. The values...

  • Java: Create a class called Vehicle with the following features: a. It has three private data...

    Java: Create a class called Vehicle with the following features: a. It has three private data members (instance variables): one is the manufacturer’s name (String manufacturer), the second is the number of cylinders in the engine (int cylinder), and the third is the owner (Person owner). The class Person is described above. b. It has three constructors, a no-argument constructor, a constructor with three parameters, and a copy constructor. The no-argument constructor will set the manufacturer to “None”, cylinder to...

  • 1a. Create a class named Inventory - Separate declaration from implementation (i.e. Header and CPP files)...

    1a. Create a class named Inventory - Separate declaration from implementation (i.e. Header and CPP files) 1b. Add the following private member variables - a int variable named itemNumber - an int variable named quantity - a double variable named cost - a double variable named totalCost. Total cost is defined as quantity X cost. - updateTotalCost() which takes no arguments and sets the values of the variable totalCost. 1c. Add the following public functions: - Accessor and Mutators for...

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

  • Java HW Define a class called Counter whose objects count things. An object of this class...

    Java HW Define a class called Counter whose objects count things. An object of this class records a count that is a nonnegative integer. Include methods to set the counter to 0, to increase the count by 1, and to decrease the count by 1. Be sure that no method allows the value of the counter to become negative. Include an accessor method that returns the current count value and a method that outputs the count to the screen. There...

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