Skip to content

Instantly share code, notes, and snippets.

@ninjapanzer
Last active October 12, 2015 19:59
Show Gist options
  • Select an option

  • Save ninjapanzer/feb3a0a527e4a824dece to your computer and use it in GitHub Desktop.

Select an option

Save ninjapanzer/feb3a0a527e4a824dece to your computer and use it in GitHub Desktop.
Car Class with Tests
package javaapplication30;
/**
* a class that shows a cars model year, the company and model of the car as
* well as the cars speed
*/
public class Car {
private int modelYear, speed;
private String makeModel;
public Car(int year, String makeModel){//constructo for car
this.modelYear = year;
this.makeModel = makeModel;
this.speed = 0;
}
public int getModelYear(){//gets the cars model year
return this.modelYear;
}
public String getMakeAndModel(){//gets cars make and model
return this.makeModel;
}
public int getSpeed(){//gets cars current speed
return this.speed;
}
/**
* increases the cars speed by 5
*/
public void accelerate(){
this.speed += 5;
}
/**
* decreases cars speed by 5
*/
public void brake(){
this.speed -= 5;
}
public static void test(){
if(test_accelerate()){
System.out.println("Accelerate passed");
}else{
System.out.println("Accelerate failed");
}
if(test_brake()){
System.out.println("Brake passed");
}else{
System.out.println("Brake failed");
}
}
public static boolean test_accelerate(){
Car c = new Car(200, "HI");
c.accelerate();
return c.getSpeed() == 5;
}
private static boolean test_brake() {
Car c = new Car(200, "HI");
c.brake();
return c.getSpeed() == -5;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment