Java Programming Code Examples for Week 2 Alice E. Fischer
January 29 and Feb 3 2015
Java Programming - L2. . .
1/31
Birthday Day
Outline
Hungry
Dice
Hungry In-class Exercise
Dice
Birthday Day Homework
Java Programming - L2. . .
2/31
Birthday Day
Outline
Hungry
Dice
Example: Hungry Purpose and new elements Code Output
Java Programming - L2. . .
3/31
Birthday Day
Outline
Hungry
Dice
Purpose and New Elements This silly program introduces the dynamic world. New elements are: I
A class with two data members, one of them final.
I
Initialization of a final member in the class definition.
I
A main function that instantiates a Hungry object and executes its primary method.
I
Use of the infinite for loop with an if...break.
I
Style: comment lines before functions.
Java Programming - L2. . .
4/31
Birthday Day
Outline
Hungry
Dice
Birthday Day
Class Skeleton import java.util.Scanner; public class Hungry { final int full = 17; int level = 0;
// Demand 17 jellybeans. // Count the beans you have.
// ----------------------------------------------public static void main (String args[]) { Hungry H = new Hungry(); H.go(); } // ----------------------------------------------public void go () { ... } } Java Programming - L2. . .
5/31
Outline
Hungry
Dice
Birthday Day
The Primary method // --------------------------------------------------public void go () { Scanner sc = new Scanner(System.in); String s; int n; System.out.println( "\nHey, Joe, I’m hungry..."); for(;;){ n = sc.nextInt(); level += n; if (level >= full) break; System.out.println (" Thank you...more...?"); } System.out.println(" Thank you. Come again...\n"); } Java Programming - L2. . .
6/31
Outline
Hungry
Dice
Birthday Day
Output
Hey, Joe, I’m hungry. Feed me some jellybeans. How many do you have? 18 Thank you. Come again tomorrow! ---------------------------------------------------------Hey, Joe, I’m hungry. Feed me some jellybeans. How many do you have? 4 Thank you for 4 jellybeans. I’m still hungry. How many more do you have? 15 Thank you. Come again tomorrow! Java Programming - L2. . .
7/31
Outline
Hungry
Dice
Birthday Day
In-class Exercise
Modify the Hungry Program.
I
Start with the Hungry program
I
Add a constructor to the Hungry class, with one parameter, the target number of Jellybeans.
Java Programming - L2. . .
8/31
Outline
Hungry
Dice
Example: Dice Purpose and Model New Elements The Constructor Rolling the Dice The main function
Java Programming - L2. . .
9/31
Birthday Day
Outline
Hungry
Dice
Birthday Day
Purpose and Model To model a set of dice, we must know how many there are and how many sides they have. Context or input must supply the info. The Dice constructor must store these numbers and allocate an array of ints to represent the random numbers showing on dice-tops. in main: Dice d = new Dice(5, 6); d.roll(); d
count sides dice
5 6
5 Java Programming - L2. . .
in Dice constructor: dice = new Int[count];
3
6
1
3 10/31
2
Outline
Hungry
Dice
New Elements I
Private data members, public methods.
I
A constructor with parameters that initializes the data members
I
this, used twice in the constructor
I
A default constructor
I
Math.random()
I
An accessor (getValue), used to print the results of roll()
I
A toString() method
I
An array of objects, in main(), allocated in a for loop
I
Calling class methods from within the class
Java Programming - L2. . .
11/31
Birthday Day
Outline
Hungry
Dice
Birthday Day
Class Skeleton import java.util.*; public class Die { private int faces; // Number of sides on the die. private int value; // The face that is showing. Die ( int faces ) { ... } Die ( ) { ... } public int roll(){ ... } public int getValue(){ return value; } public String toString(){ ... } public static void main( String[] args ) { ... } } Java Programming - L2. . .
12/31
Outline
Hungry
Dice
Birthday Day
The Dice Constructors I
I
I
I
I
No part of this object can be initialized in the class declaration because everything depends on user input. This constructor has two parameters. I chose to give the parameters the same names as the class members, creating an ambiguity (one name, two different meanings). I use this. when I want to refer to the data member, and omit it to refer to the parameter. You DO NOT need “this” unless there is ambiguity. The parameters supply two integers that are stored in the two int members. Then the third data member is initialized using the first parameter to allocate array space. dice = new int[count];
Java Programming - L2. . .
13/31
Outline
Hungry
Dice
The Dice Constructors // Constructor: initialize members. Die ( int faces ) { this.faces = faces; value = roll(); System.out.println( this ); } // Default constructor: Die ( ) { faces = 6; value = roll(); } Java Programming - L2. . .
14/31
Birthday Day
Outline
Hungry
Dice
Birthday Day
Rolling the Dice: Random Numbers I
Java supplies two random number generators: a simple one, and a better kind that is much more complex. We will use the simple one in the Math class.
I
Math.random() returns a real number, approximately uniformly distributed between 0.0 amd 1.0.
I
Our dice values are random integers between 1 and N. Thus, we need to convert the real to an int in the required range.