1. Define a class called Car (Car.java) with a static constant integer variable
ID: 3631428 • Letter: 1
Question
1. Define a class called Car (Car.java) with a static constant integer variable CAPACITY that is set equal to 50. That is,private static final int CAPACITY = 50;
2. Add to Car a static member variable gasLeft, which stores the amount of gas the car has left, and initialize it to CAPACITY.
3. Create a static method drive(int distance) with no return type and one integer parameter. The method should see if the car has enough gas to go the desired distance specified by the parameter (assume the car uses 1 gallon per mile traveled) and, if it does not, it should throw an OutOfGasException; otherwise it should print out how far the car traveled and how much gas it has left.
4. Create a static method refill() with no return type and one double argument. The method should attempt to fill the tank with the desired amount of gas. If the user tries to fill the tank over its capacity, the program should throw an OverflowException. Otherwise, the method should print how many gallons were put into the tank and how many gallons are now in the tank.
5. Write 2 exception classes: OutOfGasException and OverflowException, each with 2 constructors.
6. Write a main method that implements the 2 methods above. It should, in a loop, ask the user how far he/she plans to drive and how much gas he/she has added. The idea is to test all of the normal and exceptional cases.
Assuming you have written everything correctly, your output should look like this:
Welcome to the Car Driving Program.
How far do you plan to drive? 30
You drove 30 miles.
You have 20.0 gallons left in your tank.
How much gas did you add? 20
You filled your tank with 20.0 gallons.
You have 40.0 gallons in your tank.
How far do you plan to drive? 50
You ran out of gas!
Thank you for using this program, goodbye.
Files needed for this lab to run:
Car.java
OutOfGasException.java
OverflowException.java
Explanation / Answer
Car.java - http://pastebin.com/dmZ915Us OutOfGasException.java - http://pastebin.com/eFUKFca0 OverflowException.java - http://pastebin.com/6UdsaidB I uploaded the codes there because I couldn't upload three files here. Also If I typed, it would look ugly due to improper indentation and syntax highlighting. All input and output according to your needs. Please rate:)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.