Skip to content

Instantly share code, notes, and snippets.

@danlangford
Created February 14, 2013 05:46
Show Gist options
  • Save danlangford/4950834 to your computer and use it in GitHub Desktop.
Save danlangford/4950834 to your computer and use it in GitHub Desktop.
an example of a Class (aka Object) with properties (aka variable) and function(ality)
package javaapplication1;
public class JavaApplication1 {
public static void main(String[] args) {
Double danAmt = 1.0, joshAmt = 1000.0;
SlotMachine danSlots = new SlotMachine(),
joshSlots = new SlotMachine();
danSlots.setAmount(danAmt);
joshSlots.setAmount(joshAmt);
Double dWin = danSlots.playGame(),
jWin = joshSlots.playGame();
System.out.println("cool dan played $"+danAmt+" and won $"+dWin);
System.out.println("cool josh played $"+joshAmt+" and won $"+jWin);
}
} // end class
package javaapplication1;
public class SlotMachine {
// need to store partial dollars like .25
private Double amount = 0.0;
// good default game
private Game game = Game.BLACKJACK;
public enum Game {
TEXAS_HOLDEM, BLACKJACK, KINO;
}
// users need to add money
public void setAmount(Double amount){
this.amount = amount;
}
// users need to play the game
public Double playGame(){
System.out.println("you are playing "+game.name());
Double winnings = randomWinnings();
return winnings;
}
private Double randomWinnings(){
Double seed = Math.random(),
winnings = this.amount * seed * 2;
return winnings;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment