Created
March 19, 2010 19:01
-
-
Save rtipton/338051 to your computer and use it in GitHub Desktop.
Java Getters and Setters
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
public class Bicycle | |
{ | |
private int cadence; | |
private int gear; | |
private int speed; | |
public Bicycle(int startCadence, int startGear, int startSpeed) | |
{ | |
cadence = startCadence; | |
gear = startGear; | |
speed = startSpeed; | |
} | |
public int getCadence() | |
{ | |
return cadence; | |
} | |
public void setCadence(int cadence) | |
{ | |
this.cadence = cadence; | |
} | |
public int getGear() | |
{ | |
return gear; | |
} | |
public void setGear(int gear) | |
{ | |
this.gear = gear; | |
} | |
public int getSpeed() | |
{ | |
return speed; | |
} | |
public void setSpeed(int speed) | |
{ | |
this.speed = speed; | |
} | |
public void ApplyBrake(int decrement) | |
{ | |
speed -= decrement; | |
} | |
public void speedUp(int increment) | |
{ | |
speed += increment; | |
} | |
public void PrintValues() | |
{ | |
System.out.println("Gear = " + gear); | |
System.out.println("Cadence = " + cadence); | |
System.out.println("Speed = " + speed); | |
System.out.println(""); | |
} | |
public static void main(String [] args) | |
{ | |
Bicycle myBike = new Bicycle(30,0,1); | |
System.out.println("Bicycle Parameters"); | |
System.out.println("==================="); | |
myBike.PrintValues(); | |
myBike.speedUp(30); | |
myBike.setGear(2); | |
System.out.println("New Bicycle Parameters"); | |
System.out.println("======================="); | |
myBike.PrintValues(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment