- Joined
- Dec 19, 2007
- Messages
- 344
I just finished my first Java program for my CS162 class. It might look simple to a day to day programmer but this was one hell of a project for me. 

Code:
/*****************************************************************
Car simulation which allows the user to drive, add gas, change and
check the oil, honk the horn and check the odometer all under real
life restrictions.
@author Mike P.
@version 2009.01.28
*****************************************************************/
public class Car {
/** total miles which is read by the odometer **/
private double mileage;
/** miles until the next oil change **/
private double milesTillNextOilChange;
/** gallons of gas which is in the car **/
private double gallonsOfGas;
/** miles per gallon **/
private double mpg;
/** full tank of gas in the car **/
private double fullTank;;
/** miles before running out of gas **/
private double milesBeforeRunningOutOfGas;
/*****************************************************************
* Sets the miles per gallon, a full tank, mileage, gas in the car,
* miles till the next oil change, and miles before the car runs
* out of gas.
* @return none
* *****************************************************************/
public Car () {
mpg = 22.7;
fullTank = 12.5;
mileage = 0;
gallonsOfGas = 0;
milesTillNextOilChange = 3000;
milesBeforeRunningOutOfGas = 0;
}
/*****************************************************************
* Sets a new miles per gallon
* @return none
* *****************************************************************/
public Car (double mpg) {
this.mpg = mpg;
fullTank = 12.5;
mileage = 0;
gallonsOfGas = 0;
milesTillNextOilChange = 3000;
milesBeforeRunningOutOfGas = 0;
}
/*****************************************************************
* Tests if the car is already out of gas, the driver runs out of
* gas before completeing the trip but the odometer is updated
* correctly, or if the driver completes the trip without running
* out of gas.
* @param miles must be in "int" or "double" form
* @return none
* *****************************************************************/
public void drive (double miles) {
mileage = mileage + miles;
milesTillNextOilChange = milesTillNextOilChange - miles;
milesBeforeRunningOutOfGas = milesBeforeRunningOutOfGas + miles;
if(gallonsOfGas == 0) {
System.out.println("You are out of gas!");
mileage = mileage - miles;
milesTillNextOilChange = milesTillNextOilChange + miles;
milesBeforeRunningOutOfGas = milesBeforeRunningOutOfGas - miles;
}
else if(miles >= (gallonsOfGas * mpg)) {
mileage = mileage - miles;
milesBeforeRunningOutOfGas = milesBeforeRunningOutOfGas - miles;
milesTillNextOilChange = milesTillNextOilChange + miles;
milesBeforeRunningOutOfGas = (milesBeforeRunningOutOfGas + (gallonsOfGas * mpg));
System.out.print("You drove " + (int)milesBeforeRunningOutOfGas + " miles before running out of gas.");
mileage = mileage + (gallonsOfGas * mpg);
milesBeforeRunningOutOfGas = milesBeforeRunningOutOfGas + (gallonsOfGas * mpg);
milesTillNextOilChange = milesTillNextOilChange - (gallonsOfGas * mpg);
gallonsOfGas = 0;
milesBeforeRunningOutOfGas = 0;
}
else {gallonsOfGas = ((gallonsOfGas * mpg) - miles) / mpg;
System.out.print("You drove " + (int)miles + " miles.");
}
}
/*****************************************************************
* Prints a warning if the number of gallons is less than or equal
* to zero, prints a warning message if the number of gallons
* results in the tank overflowing but still fills the tank, or
* adds the provided amount of gallons to the tank.
* @param gallons must be in "int" or "double" form
* @return none
* *****************************************************************/
public void addGas (double gallons) {
if(gallons <= 0) {
System.out.println("You have no gas!");
gallons = 0;
}
gallonsOfGas = gallonsOfGas + gallons;
if(gallonsOfGas > fullTank) {
gallonsOfGas = fullTank;
System.out.println("You overfilled the gas tank. You now have " + fullTank + " gallons in the tank.");
}
else {System.out.println("You added " + gallons + " gallons and now have " + gallonsOfGas + " gallons in the tank.");
}
}
/*****************************************************************
* The current mileage.
* @return mileage
* *****************************************************************/
public double checkOdometer () {
return (int)mileage;
}
/*****************************************************************
* The current amount of gas in the car.
* @return gallons of gas
* *****************************************************************/
public double checkGasGauge () {
return gallonsOfGas;
}
/*****************************************************************
* Checks if the oil needs or doesn't need to be replaced.
* @return none
* *****************************************************************/
public void checkOil () {
if(milesTillNextOilChange <= 0) {
System.out.println("It's time for an oil change!");
}
else {System.out.println("The oil looks OK for now.");
}
}
/*****************************************************************
* Changes the oil and tells the user when the next oil change
* is due.
* @return none
* *****************************************************************/
public void changeOil () {
milesTillNextOilChange = 3000;
System.out.println("You just added oil, the next change should occur at " + (int)(milesTillNextOilChange + mileage) + " miles.");
}
/*****************************************************************
* Prints the car's horn.
* @return none
* *****************************************************************/
public void honkHorn() {
System.out.println("Get Out Of The Way! Get Out Of The Way!");
}
}
Last edited:






