Title: Java Methods
1The Math Class
2Objectives
- Learn about the different methods of the Math
class. - Learn how to call the methods of the Math class.
- Learn how to generate a range of random numbers.
3static
4Top-Down Programming (contd)
- Start JCreator.
- Open the file StaticDraw.java.
- StaticDraw.java is located in your Lab06 folder.
- Run StaticDraw.java
5(No Transcript)
6(No Transcript)
7private BufferedImage buffer null
private static BufferedImage buffer null
Run StaticDraw.java
8(No Transcript)
9(No Transcript)
10The Math Class
11The Math Class
- The Math class is part of the java.lang package
- The Math class contains methods that perform
various mathematical functions - These include
- absolute value
- square root
- exponentiation
- trigonometric functions
12The Math Class
Some of the methods of the Math class are
overloaded.
Method Summary Method Summary
static double abs(double a) Returns the absolute value of a double value. abs(double a) Returns the absolute value of a double value.
static float abs(float a) Returns the absolute value of a float value. abs(float a) Returns the absolute value of a float value.
static int abs(int a) Returns the absolute value of an integer value. abs(int a) Returns the absolute value of an integer value.
static long abs(long a) Returns the absolute value of a long value. abs(long a) Returns the absolute value of a long value.
13The Math Class
Method Summary
static double ceil(double a) Returns the smallest whole value that is not less than the argument.
static double cos(double a) Returns the trigonometric cosine of an angle.
static double floor(double a) Returns the largest whole value that is not greater than the argument and is equal to a mathematical integer.
static double hypot(double x, double y) Returns sqrt(x2 y2).
static double max(double a, double b) Returns the greater of two double values. (overloaded method)
static double min(double a, double b) Returns the smaller of two double values. (overloaded method)
14The Math Class
Method Summary
static double pow(double a, double b) Returns the value of the first argument raised to the power of the second argument.
static double random() Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0.
static long round(double a) Returns the closest int to the argument.
static double sqrt(double a) Returns the correctly rounded positive square root of a double value.
static double toDegrees(double angrad) Converts an angle measured in radians to the equivalent angle measured in degrees.
static double toRadians(double angdeg) Converts an angle measured in degrees to the equivalent angle measured in radians.
15The Math Class
- The methods and fields of the Math class are
static. - Static methods and fields can be invoked through
the class name no object of the Math class is
needed
16The Math Class
- The methods and fields of the Math class are
static. - Static methods and fields can be invoked through
the class name no object of the Math class is
needed
double r 6.5 double v 4.0/3 Math.PI
Math.pow(r, 3)
17The Math Class
double a 6.5 double b 4.3 double largest
Math.max(int a, int b)
double a 6.5 double b 4.3 double largest
Math.max(a, b)
18Random Numbers
19Random Numbers
- Math.random() generates a random number between
0.0 and 0.999
- Math.random() 10 generates a random number
between 0.0 and 9.999 - (int) (Math.random() 10) generates a random
number between 0 and 9. - (int) (Math.random() 10) 1 generates a random
number between 1 and 10.
20Random Numbers
(int) (Math.random() 10) 1
21Random Numbers
(int) (Math.random() 10) 1
Determines the range of numbers. Ten possible
numbers can be generated.
22Random Numbers
Determines the starting number.
(int) (Math.random() 10) 1
23Random Numbers
What range of numbers would be generated?
(int) (Math.random() 15) 6
6 - 20
0 - 14
24Top Down Design
25Top-Down Programming (contd)
- Start JCreator.
- Open the file Lab06.java.
- Lab06.java is located in your Lab06 folder.
26Top-Down Design
- WAP that calculates the calories in one slice of
pepperoni pizza. The diameter of the pizza will
be entered from the keyboard. The pizza will be
cut into eight equal pieces. We will assume that
there are 15.35 calories in one square inch of
pizza. -
27Top-Down Design
- The area of one slice of pizza can be calculated
by dividing the area of the pizza (circle) by 8.
28Top-Down Design
- The number of calories can be calculated by
multiplying the area of one slice of pizza times
15.35 (the number of calories in one square inch
of pizza).
29Top-Down Design (contd)
- What instance fields are required to solve the
problem? - NOTE Fields should be the values we are solving
for and the values we will read from the
keyboard.
int diameter double calories
30Top-Down Design (contd)Input
- Prompt the user to enter the diameter of the
pizza. - Read an integer from the keyboard.
Enter the diameter of the pizza
12
31Top-Down Design (contd)Output
If a pizza has a diameter of 12 inches then one
slice has approximately 217 calories.
32Top-Down Design (contd)Process
- Calculate the radius of the pizza.
- Calculate the area of the pizza.
- Calculate the area of one slice of pizza.
- Calculate the number of calories in one slice of
pizza.
33Top-Down Design (contd)
- radius diameter / 2
- area p r2
- area of one slice area / 8
- calories area of one slice 15.35
34Top-Down Design (contd)
- input() and output() have been provided.
35Top-Down Design (contd)
- Two instance fields have been instantiated
int diameter double calories
- diameter is read from the keyboard in the input()
method. - calories is what we are solving for in process().
36Top-Down Design (contd)process()
37Top-Down Design (contd)process()
- public void process()
- double r diameter / 2.0
-
-
38Top-Down Design (contd)process()
- public void process()
- double r diameter / 2.0
- double area (Math.PI Math.pow(r, 2))
-
39Top-Down Design (contd)process()
- public void process()
- double r diameter / 2.0
- double area (Math.PI Math.pow(r, 2))
- double areaOfSlice area / 8
-
40Top-Down Design (contd)process()
- public void process()
- double r diameter / 2.0
- double area (Math.PI Math.pow(r, 2))
- double areaOfSlice area / 8
- calories areaOfSlice 15.35
-
41Top-Down Design (contd)
Run The Program (1st Run)
12
Enter the diameter of the pizza
If a pizza has a diameter of 12 inches then one
slice has approximately 217 calories.
42Top-Down Design (contd)
Run The Program (2st Run)
17
Enter the diameter of the pizza
If a pizza has a diameter of 17 inches then one
slice has approximately 435.5 calories.
43Questions?
44Begin Lab 06