15
Copyright © 2004-2009 Curt Hill Mathematics Functions An example of static methods

Copyright © 2004-2009 Curt Hill Mathematics Functions An example of static methods

Embed Size (px)

Citation preview

Page 1: Copyright © 2004-2009 Curt Hill Mathematics Functions An example of static methods

Copyright © 2004-2009 Curt Hill

Mathematics Functions

An example of static methods

Page 2: Copyright © 2004-2009 Curt Hill Mathematics Functions An example of static methods

Copyright © 2004-2009 Curt Hill

The Problem• How do we evaluate expressions like:

– The square root of x– The natural exponential of x – The natural logarithm of x– The sine or cosine of x

• Most programming languages have some built-in functions to handle these or a library that contains them

• Java does as well, but it is somewhat different than other languages

• It uses the Math object

Page 3: Copyright © 2004-2009 Curt Hill Mathematics Functions An example of static methods

Copyright © 2004-2009 Curt Hill

The Math object

• It does not need to be imported or defined– A basic part of the language

• It contains constants and methods• The constants are two most

common ones:– PI– E

Page 4: Copyright © 2004-2009 Curt Hill Mathematics Functions An example of static methods

Copyright © 2004-2009 Curt Hill

Example

• Suppose we wish to write a program to compute the area of a circle

• The program would take the following form:– Prompt user for radius– Get the radius– Square the radius and multiply by PI– Display result

Page 5: Copyright © 2004-2009 Curt Hill Mathematics Functions An example of static methods

Copyright © 2004-2009 Curt Hill

The Example Program public static void main(String[] args) { double radius,area; Scanner inp = new Scanner(System.in); System.out.println( "Enter the circle's radius:"); radius = inp.nextDouble(); area = Math.PI * radius * radius; System.out.print("The area is: "); System.out.println(area); }

Page 6: Copyright © 2004-2009 Curt Hill Mathematics Functions An example of static methods

Copyright © 2004-2009 Curt Hill

What do you need to know?

• Each method has certain characteristics that you must know to use the method. These include:

• The containing object• The name of the method• The type it returns• The parameters it requires• What it does

– Not how it does its work

Page 7: Copyright © 2004-2009 Curt Hill Mathematics Functions An example of static methods

Copyright © 2004-2009 Curt Hill

Math Methods

Name Parameters Result type

sqrt double double

exp double double

sin double double

log double double

abs int/double/float same

pow double, double double

• All of these are from Math• What they do should be clear

Page 8: Copyright © 2004-2009 Curt Hill Mathematics Functions An example of static methods

Copyright © 2004-2009 Curt Hill

Details

• Many of these have range restrictions in addition to their type– Eg. Do not take the square root of a

negative• The trigonometric functions take

their parameter in radians, not degrees

Page 9: Copyright © 2004-2009 Curt Hill Mathematics Functions An example of static methods

Copyright © 2004-2009 Curt Hill

Example• We want to write a program to

compute the distance between two points

• Prompt user• Read in both points• Use Pythagorean Theorem• Display results

Page 10: Copyright © 2004-2009 Curt Hill Mathematics Functions An example of static methods

Copyright © 2004-2009 Curt Hill

The Example Program public static void main(String[] args) { double x1,x2,y1,y2,distance; Scanner inp = new Scanner(System.in); System.out.println ("Enter the x and y for the first point:"); x1 = inp.nextDouble(); y1 = inp.nextDouble(); System.out.println ("Enter the x and y for the second point:"); x2 = inp.nextDouble(); y2 = inp.nextDouble(); x1 = x1-x2; y1 = y1-y2; distance = Math.sqrt(x1*x1+y1*y1); System.out.print("The distance is: "); System.out.println(distance); }

Page 11: Copyright © 2004-2009 Curt Hill Mathematics Functions An example of static methods

Static Methods• Why are these methods referred to

as static?• They do not need an instance of

their class• So I use:

v = Math.sqrt(j);• Math is the class name not an

object name• Consider the difference with a non-

static methodCopyright © 2004-2009 Curt Hill

Page 12: Copyright © 2004-2009 Curt Hill Mathematics Functions An example of static methods

String Methods• The String class has a length

method which displays the number of characters in the string

• Consider:String a = “Hello”;String b = “Hi”;int al = a.length();int bl = b.length();

• The value assigned to al and bl is dependent on the size of the strings

Copyright © 2004-2009 Curt Hill

Page 13: Copyright © 2004-2009 Curt Hill Mathematics Functions An example of static methods

Static and Non-static Methods• Most methods produce a result

dependent on the value of the object:int al = a.length();int bl = b.length();

• These are dependent on the contents of the objects– These are non-static

• A static method is dependent only on parameters:double d = Math.sqrt(e);

Copyright © 2004-2009 Curt Hill

Page 14: Copyright © 2004-2009 Curt Hill Mathematics Functions An example of static methods

One more difference• A non-static method must specify

the objectint bl = b.length();

• A static method may specify either the class name or object namedouble d = Math.sqrt(e);

• We do not need to declare the Math class but other classes, such as String, do have static methods• We will see these later

Copyright © 2004-2009 Curt Hill

Page 15: Copyright © 2004-2009 Curt Hill Mathematics Functions An example of static methods

Copyright © 2004-2009 Curt Hill

One Last Thought

• The two constants have no parenthesis b = Math.E;

• The methods must have parentheses• Some methods have no parameters

– random returns a random number between 0 and 1

• An empty set of parentheses are still needed:a = Math.random();