11/2: Method definitions, Math.random

Download Report

Transcript 11/2: Method definitions, Math.random

3/20: 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;
double yInt;
//note that I declared these
//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 );
}
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;
}
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;
}
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 ;
}
Method Definitions
public void paint ( Graphics g )
{
g.drawString(square(x) + “ “,25,25);
}
the method square is called in this line. Because it is
in parentheses, x is the value to use as the input.
public int square ( int y )
{
return y * y ; We know that this method
}
is defined in this class because
there is no other class name
before the method name.
Method Definitions
public void paint ( Graphics g )
{
g.drawString(square(x) + “ “,25,25);
}
x fits the requirement that it must be an integer.
public int square ( int y )
{
return y * y ;
}
Method Definitions
public void paint ( Graphics g )
{
g.drawString(square(x) + “ “,25,25);
}
using x as the input, the method calculates a return value.
public int square ( int y )
{
return y * y ;
}
Method Definitions
public void paint ( Graphics g )
{
x * x
g.drawString(square(x) + “ “,25,25);
} the returned value is what replaces the square(x).
This value is of type int.
public int square ( int y )
{
return y * y ;
}
Example: Maximum.java (p. 256)
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 ) );
}
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. 258-259 carefully
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.
Math.random()
• EX: Math.random ()
• scale it by 6: Math.random()* 6 makes 0 to <6
• shift it by 1: Math.random() + 1 makes 1 to <2
• 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 );
Program of the day
• pg. 263 RollDie.java
• Modify the program to allow the user to input
how many times to roll the die.