Java Object Oriented Programming The Math Class Objectives
Java Object Oriented Programming The Math Class
Objectives: § 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. Lab 05 -2
Java Object Oriented Programming static
Top-Down Programming (cont’d) § Start JCreator. § Open the file “Static. Draw. java”. § Static. Draw. java is located in your Lab 06 folder. § Run Static. Draw. java Lab 05 -4
Lab 02 -5
Lab 02 -6
private static Buffered. Image buffer = null; Run “Static. Draw. java” Lab 02 -7
Lab 02 -8
Lab 02 -9
Java Object Oriented Programming The Math Class
The Math Class l l l 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 Lab 05 -11
The 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 Lab 05 -12
The 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); Lab 05 -13
The Math Class Some of the methods of the Math class are overloaded. Method Summary static double abs(double a) Returns the absolute value of a double value. static float abs(float a) Returns the absolute value of a float value. static int abs(int a) Returns the absolute value of an integer value. static long abs(long a) Returns the absolute value of a long value. Lab 05 -14
The 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(x 2 + y 2). 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) Lab 05 -15
The 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 to. Degrees(double angrad) Converts an angle measured in radians to the equivalent angle measured in degrees. static double to. Radians(double angdeg) Converts an angle measured in degrees to the equivalent angle measured in radians. Lab 05 -16
Java Object Oriented Programming Random Numbers
Random 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. Lab 05 -18
Random Numbers (int) (Math. random() * 10) + 1 Lab 05 -19
Random Numbers (int) (Math. random() * 10) + 1 Determines the range of numbers. Ten possible numbers can be generated. Lab 05 -20
Random Numbers Determines the starting number. (int) (Math. random() * 10) + 1 Lab 05 -21
Random Numbers What range of numbers would be generated? (int) (Math. random() * 15) + 6 0 - 14 Lab 05 -22
Random Numbers What range of numbers would be generated? (int) (Math. random() * 15) + 6 6 - 20 Lab 05 -23
Java Object Oriented Programming Top Down Design
Top-Down Programming (cont’d) § Start JCreator. § Open the file “Lab 05. java”. § Lab 05. java is located in your Lab 05 folder. Lab 05 -25
Top-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. Lab 05 -26
Top-Down Design The area of one slice of pizza can be calculated by dividing the area of the pizza (circle) by 8. One Slice of Pizza = area / 8 Lab 05 -27
Top-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). Lab 05 -28
Top-Down Design (cont’d) 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; Lab 05 -29
Top-Down Design (cont’d) Input § Prompt the user to enter the diameter of the pizza. § Read an integer from the keyboard. Enter the diameter of the pizza: 12 Lab 05 -30
Top-Down Design (cont’d) Output diameter read from the keyboard If a pizza has a diameter of 12 inches then one slice has approximately 217 calories calculated in process() Lab 05 -31
Top-Down Design (cont’d) Process l l Calculate the radius of the pizza. Calculate the area of one slice of pizza. Calculate the number of calories in one slice of pizza. Lab 05 -32
Top-Down Design (cont’d) radius = diameter / 2 area = π * r 2 area of one slice = area / 8 calories = area of one slice * 15. 35 Lab 05 -33
Top-Down Design (cont’d) input() and output() have been provided. Lab 05 -34
Top-Down Design (cont’d) 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(). Lab 05 -35
Top-Down Design (cont’d) process() public void process() { } Lab 05 -36
Top-Down Design (cont’d) process() public void process() { double r = diameter / 2. 0; } Lab 05 -37
Top-Down Design (cont’d) process() public void process() { double r = diameter / 2. 0; double area = Math. PI * Math. pow(r, 2); } Lab 05 -38
Top-Down Design (cont’d) process() public void process() { double r = diameter / 2. 0; double area = Math. PI * Math. pow(r, 2); double area. Of. Slice = area / 8; } Lab 05 -39
Top-Down Design (cont’d) process() public void process() { double r = diameter / 2. 0; double area = Math. PI * Math. pow(r, 2); double area. Of. Slice = area / 8; calories = area. Of. Slice * 15. 35; } Lab 05 -40
Top-Down Design (cont’d) Run The Program: (1 st Run) Enter the diameter of the pizza: 12 If a pizza has a diameter of 12 inches then one slice has approximately 217 calories. Lab 05 -41
Top-Down Design (cont’d) Run The Program: (2 st Run) Enter the diameter of the pizza: 17 If a pizza has a diameter of 17 inches then one slice has approximately 435. 5 calories. Lab 05 -42
Java Object Oriented Programming Questions?
Java Object Oriented Programming Begin Lab 05
- Slides: 44