Download presentation
Presentation is loading. Please wait.
Published byMorgan Bradley Modified over 9 years ago
1
Copyright © 2004-2009 Curt Hill Mathematics Functions An example of static methods
2
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
3
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–E
4
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
5
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); }
6
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
7
Copyright © 2004-2009 Curt Hill Math Methods NameParametersResult type sqrtdouble expdouble sindouble logdouble absint/double/floatsame powdouble, doubledouble All of these are from Math What they do should be clear
8
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
9
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
10
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); }
11
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 method Copyright © 2004-2009 Curt Hill
12
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
13
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
14
One more difference A non-static method must specify the object int bl = b.length(); A static method may specify either the class name or object name double 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
15
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();
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.