Presentation is loading. Please wait.

Presentation is loading. Please wait.

3/25: Scope Rules, More Methods about RollDie.java & modifications Scope rules More methods Program of the day.

Similar presentations


Presentation on theme: "3/25: Scope Rules, More Methods about RollDie.java & modifications Scope rules More methods Program of the day."— Presentation transcript:

1 3/25: Scope Rules, More Methods about RollDie.java & modifications Scope rules More methods Program of the day

2 About RollDie.java – pt. 1 //RollDie.java //Roll a six-sided die 6000 times import javax.swing.*; public class RollDie { public static void main ( String args[] ) { int freq1 = 0, freq2 = 0, freq3 = 0, freq4 = 0, freq5 = 0, freq6 = 0 ; int face;

3 About RollDie.java – pt. 2 for ( int roll = 1; roll <= 6000 ; roll++ ) { face = 1 + (int) ( Math.random() * 6 ); switch ( face ) { case 1: ++freq1; break; case 2: ++freq2; break; case 3: ++freq3; break; case 4: ++freq4; break; case 5: ++freq5; break; case 6: ++freq6; break; }

4 About RollDie.java – pt. 3 JTextArea outputArea = new JTextArea ( 7, 10 ); outputArea.setText ( "Face\tFrequency" + "\n1 \t" + freq1 + "\n2 \t" + freq2 + "\n3 \t" + freq3 + "\n4 \t" + freq4 + "\n5 \t" + freq5 + "\n6 \t" + freq6 ); JOptionPane.showMessageDialog ( null, outputArea, "Rolling a die 6000 times", JOptionPane.INFORMATION_MESSAGE); System.exit (0); } }

5 Modifying RollDie.java we need to replace the 6000 in the for loop with a user-defined value, so we add in int rolls; rolls = Integer.parseInt ( JOptionPane.showInputDialog ( “How many rolls?” ) ); and replace the 6000 with rolls. for ( int roll = 1; roll <= rolls ; roll++ ) { face = 1 + (int) ( Math.random() * 6 );

6 Automatic Variables: Duration Identifiers are used for variable names. Identifiers have a specific duration (or lifetime): –How long the identifier exists in memory (RAM). –Identifiers representing local variables exist ONLY while the program is inside that block. –A block is a set of compound statements in a program that contains declarations.

7 Automatic Variables: Duration Identifiers have a specific duration (or lifetime): –They are automatically created in memory upon entering the block and automatically destroyed in (deleted from) the computer’s memory when that block is exited. –They are said to have automatic duration. –They are called automatic duration variables, or simply automatic variables.

8 Instance Variables & Initialization Instance (class-wide) variables are initialized by default: –primitive data types to zero (0). –boolean types to false. –references (names given to objects) to null. Automatic (block-local) variables must be initialized manually. –you must initialize them or get a compiler error.

9 Scope Rules Identifiers have a specific scope: –where the identifier can be referenced in the program. –Class scope: accessible throughout the body of the class (between the class {}’s) EX: instance variables, methods –Block scope: accessible inside their block. EX: local variables, method parameters

10 Hidden Instance Variables Instance variables can be “hidden” if a local variable has the same name. While the program is in the local variable’s block, the instance variable is “hidden”. Scoping.java gives a taste of this. (pg. 276)

11 Program of the day Scoping.java After getting it to run correctly, try to analyze why the Container c is there.


Download ppt "3/25: Scope Rules, More Methods about RollDie.java & modifications Scope rules More methods Program of the day."

Similar presentations


Ads by Google