Skip to content

Instantly share code, notes, and snippets.

@kylemsguy
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kylemsguy/b20e81274255c045c7ea to your computer and use it in GitHub Desktop.
Save kylemsguy/b20e81274255c045c7ea to your computer and use it in GitHub Desktop.
public class Car {
// instance variables
private float fuel;
private boolean started;
private int distanceTravelled;
// static variable to keep track how many cars there are GLOBALLY.
private static int numCars = 0;
private static final float mileage = 0.15; // constant for mileage in L/km
private static void addCarCount(){
// increments number of cars that exist; only accessible by members of this class.
numCars++;
}
public static int getCarCount(){
// Allows the outside world to get how many cars there are
return numCars;
}
public Car(){
// constructor for Car
fuel = 100f; // literals are longs by default, and decimal literals are doubles by default
distanceTravelled = 0;
started = false;
// increments global car count
addCarCount();
// just to show that car has been created.
System.out.println("New car created.");
}
public float getFuel(){
// just a getter for the private fuel level
return fuel;
}
public int getDistanceTravelled(){
return distanceTravelled;
}
public void startCar() throws Exception {
// Attempts to start the car. If the car is out of fuel then it throws an exception.
if(fuel > 0){
started = true;
} else {
throw new Exception("Not Enough Fuel.");
}
}
public void stopCar(){
// Stops the car.
started = false;
}
public void addFuel(float fuel){
// this <-- a keyword signifying "this" instance of the class.
// needed when variable names conflict like this.
// There is too much "this" here.
this.fuel += fuel;
}
public void travel(int distance) throws Exception{
// checks if car started
if(started){
// calculates fuel needed to complete trip
float fuelNeeded = distance * mileage;
// checks if enough fuel
if(fuelNeeded > fuel){
// not enough fuel. Throw fit.
throw new Exception("Not Enough Fuel.");
} else {
// Subtract fuel used for trip.
fuel -= fuelNeeded;
// Add mileage to car
distanceTravelled += distance;
// Print amount of distance travelled
System.out.println("I traveled " + Integer.toString(distance) + "km");
}
} else {
// Car is not started. Throw fit.
throw new Exception("Car is not started!");
}
}
public static void main(String[] args){
// main method is static. That means it cannot access any instance vars without creating
// instance of a class
System.out.println("Number of cars: " + Integer.toString(Car.getCarCount())); // 0
Car car1 = new Car();
System.out.println("Number of cars: " + Integer.toString(Car.getCarCount())); // 1
Car car2 = new Car();
Car car3 = new Car();
System.out.println("Number of cars: " + Integer.toString(Car.getCarCount())); // 3
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment