-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
40 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* implement a class car with the following properties. A car has a certain fuel effeciency (measured in miles/gallon or liters/km - pick one) | ||
* and a certain amount of fuel in the gas tank. The effeciency is specified in the constructor, and the initial fuel level is 0. | ||
* Supply a method drive that simulates driving the car for a certain distance, reducing the amount of gasoline in the fuel tank. | ||
* Also supply methods getGasInTank, returning the current amount of gasoline in the fuel tank and addGas to add fuel to the tank. Sample usage: | ||
* car myHybrid = new Car(50); // 50 miles per gallon | ||
* myHybrid.addGas(20); // Tank 20 gallons | ||
*/ | ||
|
||
public class Car { | ||
private double MPG; // Miles Per Gallon | ||
private double tank; // Miles | ||
|
||
public Car(double MPG) { | ||
this.MPG = MPG; | ||
tank = 0; | ||
} | ||
|
||
public void addGas(double gallons) { | ||
tank += (gallons * MPG); | ||
} | ||
|
||
public void drive(double miles) { | ||
tank -= miles; | ||
} | ||
|
||
public double getGasInTank() { | ||
return tank/MPG; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,4 +30,4 @@ public void raiseSalary(double increase) { | |
} | ||
salary *= increase; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
public class carTester { | ||
static Car myHybrid; | ||
public static void main(String... args) { | ||
myHybrid = new Car(50); | ||
myHybrid.addGas(20); | ||
myHybrid.drive(100); | ||
System.out.println("Gas remaining: " + myHybrid.getGasInTank()); | ||
} | ||
} |