Java Methods - PowerPoint PPT Presentation

1 / 44
About This Presentation
Title:

Java Methods

Description:

Title: Java Methods Subject: Chapter 13 Author: Maria Litvin and Gary Litvin Last modified by: John Spicer Created Date: 8/16/2002 1:24:03 PM Document presentation format – PowerPoint PPT presentation

Number of Views:190
Avg rating:3.0/5.0
Slides: 45
Provided by: MariaL226
Category:

less

Transcript and Presenter's Notes

Title: Java Methods


1
The Math Class
2
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.

3
static
4
Top-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)
7
private BufferedImage buffer null
private static BufferedImage buffer null
Run StaticDraw.java
8
(No Transcript)
9
(No Transcript)
10
The Math Class
11
The 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

12
The 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.
13
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(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)
14
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 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.
15
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

16
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)
17
The 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)
18
Random Numbers
19
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.

20
Random Numbers
(int) (Math.random() 10) 1
21
Random Numbers
(int) (Math.random() 10) 1
Determines the range of numbers. Ten possible
numbers can be generated.
22
Random Numbers
Determines the starting number.
(int) (Math.random() 10) 1
23
Random Numbers
What range of numbers would be generated?
(int) (Math.random() 15) 6
6 - 20
0 - 14
24
Top Down Design
25
Top-Down Programming (contd)
  • Start JCreator.
  • Open the file Lab06.java.
  • Lab06.java is located in your Lab06 folder.

26
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.

27
Top-Down Design
  • The area of one slice of pizza can be calculated
    by dividing the area of the pizza (circle) by 8.

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

29
Top-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
30
Top-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
31
Top-Down Design (contd)Output
If a pizza has a diameter of 12 inches then one
slice has approximately 217 calories.
32
Top-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.

33
Top-Down Design (contd)
  • radius diameter / 2
  • area p r2
  • area of one slice area / 8
  • calories area of one slice 15.35

34
Top-Down Design (contd)
  • input() and output() have been provided.

35
Top-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().

36
Top-Down Design (contd)process()
  • public void process()

37
Top-Down Design (contd)process()
  • public void process()
  • double r diameter / 2.0

38
Top-Down Design (contd)process()
  • public void process()
  • double r diameter / 2.0
  • double area (Math.PI Math.pow(r, 2))

39
Top-Down Design (contd)process()
  • public void process()
  • double r diameter / 2.0
  • double area (Math.PI Math.pow(r, 2))
  • double areaOfSlice area / 8

40
Top-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

41
Top-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.
42
Top-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.
43
Questions?
44
Begin Lab 06
Write a Comment
User Comments (0)
About PowerShow.com