Skip to content

Instantly share code, notes, and snippets.

@brycethomas
Created August 18, 2011 15:07
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 brycethomas/1154251 to your computer and use it in GitHub Desktop.
Save brycethomas/1154251 to your computer and use it in GitHub Desktop.
Apples simulator - amended main method and toString() method
// prints out something like "Apple 27 (GRANNY_SMITH, 3)"
public String toString() {
return String.format("Apple %d (%s, %s)",size,type,batchNumber);
}
public static void main(String[] args) {
Random random = new Random(); // this is the line missing from the workshop pdf
final int SIZE = random.nextInt(5) + 1;
Apple[] apples = new Apple[SIZE]; // a randomly sized apples array
for (int i = 0; i < SIZE; ++i) {
AppleType[] types = AppleType.values();
int whichType = random.nextInt(types.length);
int size = random.nextInt(20) + 10;
apples[i] = new Apple(size, types[whichType]); // create random apples
}
System.out.println("New apples: " + Arrays.deepToString(apples));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment