Skip to content

Commit

Permalink
Car type shit
Browse files Browse the repository at this point in the history
  • Loading branch information
Ardusa committed Oct 17, 2023
1 parent 7810103 commit 82d1c08
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
30 changes: 30 additions & 0 deletions Car.java
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;
}
}
2 changes: 1 addition & 1 deletion Employee.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ public void raiseSalary(double increase) {
}
salary *= increase;
}
}
}
9 changes: 9 additions & 0 deletions carTester.java
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());
}
}

0 comments on commit 82d1c08

Please sign in to comment.