11/2: Math.random, more methods About DrawLine.java modifications –allow user input –draw a...

24
11/2: Math.random, more methods • About DrawLine.java modifications – allow user input – draw a curve • Method definitions • Math.random()

Transcript of 11/2: Math.random, more methods About DrawLine.java modifications –allow user input –draw a...

Page 1: 11/2: Math.random, more methods About DrawLine.java modifications –allow user input –draw a curve Method definitions Math.random()

11/2: Math.random, more methods

• About DrawLine.java modifications– allow user input– draw a curve

• Method definitions

• Math.random()

Page 2: 11/2: Math.random, more methods About DrawLine.java modifications –allow user input –draw a curve Method definitions Math.random()

Allow User Input (1 of 2)• Add in an init method for two input dialog boxes.

double coeff; //note that I declared thesedouble yInt; //two variables as global.

public void init () {

cText = JOptionPane.showInputDialog(“Coefficient, please.”);

yText = JOptionPane.showInputDialog (“Y-intercept, please.”);

coeff = Double.parseDouble ( cText );yInt = Double.parseDouble ( yText );

}

Page 3: 11/2: Math.random, more methods About DrawLine.java modifications –allow user input –draw a curve Method definitions Math.random()

Allow User Input (2 of 2)

• Use coeff & yInt in the drawDot method

int drawDot ( int a ) { int b; //must cast to int below

b = (int) (coeff * a + yInt) ; return b;

}

Page 4: 11/2: Math.random, more methods About DrawLine.java modifications –allow user input –draw a curve Method definitions Math.random()

Draw a Curve• Change the formula in the drawDot method from:

int drawDot ( int a ) {

int b;b = 2 * a + 10 ;

return b; }

• to: int drawDot ( int a )

{ int b;

b = 2 * ( a * a ) + 10 ; return b; }

Page 5: 11/2: Math.random, more methods About DrawLine.java modifications –allow user input –draw a curve Method definitions Math.random()

Method Definitions

• in SquareInt.java (simple version below), we see a method called square:public void paint ( Graphics g )

{

g.drawString(square(x) + “ “,25,25);}

public int square ( int y ){

return y * y ;}

Page 6: 11/2: Math.random, more methods About DrawLine.java modifications –allow user input –draw a curve Method definitions Math.random()

Method Definitions

public void paint ( Graphics g )

{

g.drawString(square(x) + “ “,25,25);}

public int square ( int y ){

return y * y ;}

the method square is called in this line. Because it is in parentheses, x is the value to use as the input.

Page 7: 11/2: Math.random, more methods About DrawLine.java modifications –allow user input –draw a curve Method definitions Math.random()

Method Definitions

public void paint ( Graphics g )

{

g.drawString(square(x) + “ “,25,25);}

public int square ( int y ){

return y * y ;}

x fits the requirement that it must be an integer.

Page 8: 11/2: Math.random, more methods About DrawLine.java modifications –allow user input –draw a curve Method definitions Math.random()

Method Definitions

public void paint ( Graphics g )

{

g.drawString(square(x) + “ “,25,25);}

public int square ( int y ){

return y * y ;}

using x as the input, the method calculates a return value.

Page 9: 11/2: Math.random, more methods About DrawLine.java modifications –allow user input –draw a curve Method definitions Math.random()

Method Definitions

public void paint ( Graphics g )

{

g.drawString(square(x) + “ “,25,25);}

public int square ( int y ){

return y * y ;}

the returned value is what replaces the square(x). This value is of type int.

x * x

Page 10: 11/2: Math.random, more methods About DrawLine.java modifications –allow user input –draw a curve Method definitions Math.random()

Example: Maximum.java (p. 212)public void init (){ (stuff left out…)double max = maximum ( num1, num2, num3 );outputArea.setText ( “Max is “ + max );

}

public double maximum ( double x, double y, double z )

{return Math.max ( x, Math.max ( y, z ) );

}

Page 11: 11/2: Math.random, more methods About DrawLine.java modifications –allow user input –draw a curve Method definitions Math.random()

Important: Coercion of Arguments• Java will automatically promote variables to

larger types when necessary; EX: int to double.

• In Math.pow, the requested types of input are double. However, Java will create a double version of an int variable to use in that method automatically.

• go to Java class index for Math.pow• read p. 213-214 carefully

Page 12: 11/2: Math.random, more methods About DrawLine.java modifications –allow user input –draw a curve Method definitions Math.random()

Math.random()• generates a random double number in the range

0.0 <= x < 1.0

• To utilize this function, we use scaling & shifting.

• scaling: multiplying the function by a number to get the range we want.

• shifting: adding a number to the function to get the beginning point of the range that we want.

Page 13: 11/2: Math.random, more methods About DrawLine.java modifications –allow user input –draw a curve Method definitions Math.random()

Math.random()• EX: Math.random ()• shift it by 1: Math.random() + 1 makes 1 to <2• scale it by 6: Math.random()* 6 makes 0 to <5

• to simulate rolling a die, we want 6 possibilities (or a range of 6), from 1 to 6. They have to be integers.

(int) ( Math.random() * 6 + 1 );

Page 14: 11/2: Math.random, more methods About DrawLine.java modifications –allow user input –draw a curve Method definitions Math.random()

1st Program of the day

• pg. 220 RollDie.java

• Modify the program to allow the user to input how many times to roll the die.

Page 15: 11/2: Math.random, more methods About DrawLine.java modifications –allow user input –draw a curve Method definitions Math.random()

About RollDie.java – pt. 1//RollDie.java//Roll a six-sided die 6000 timesimport 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;

Page 16: 11/2: Math.random, more methods About DrawLine.java modifications –allow user input –draw a curve Method definitions Math.random()

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; } }

Page 17: 11/2: Math.random, more methods About DrawLine.java modifications –allow user input –draw a curve Method definitions Math.random()

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);}

}

Page 18: 11/2: Math.random, more methods About DrawLine.java modifications –allow user input –draw a curve Method definitions Math.random()

Modifying RollDie.javawe 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 );

Page 19: 11/2: Math.random, more methods About DrawLine.java modifications –allow user input –draw a curve Method definitions Math.random()

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.

Page 20: 11/2: Math.random, more methods About DrawLine.java modifications –allow user input –draw a curve Method definitions Math.random()

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.

Page 21: 11/2: Math.random, more methods About DrawLine.java modifications –allow user input –draw a curve Method definitions Math.random()

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.

Page 22: 11/2: Math.random, more methods About DrawLine.java modifications –allow user input –draw a curve Method definitions Math.random()

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

Page 23: 11/2: Math.random, more methods About DrawLine.java modifications –allow user input –draw a curve Method definitions Math.random()

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. 192)

Page 24: 11/2: Math.random, more methods About DrawLine.java modifications –allow user input –draw a curve Method definitions Math.random()

2nd Program of the day

• Scoping.java

• After getting it to run correctly, analyze why the Container c is there.