140 likes | 272 Views
CSE 1341 Principles of Computer Science I. Spring 2008 Mark Fontenot mfonten@engr.smu.edu. Note Set 5. Note Set 5 Overview. Methods – In Depth Static methods Review of parameters Return values and the call stack. Method. Allow you to break a program up in to self-contained units
E N D
CSE 1341Principles of Computer Science I Spring 2008 Mark Fontenot mfonten@engr.smu.edu Note Set 5
Note Set 5 Overview • Methods – In Depth • Static methods • Review of parameters • Return values and the call stack
Method • Allow you to break a program up in to self-contained units • Process called modularization – to modularize your code • Write once – use over and over • Don’t reinvent the wheel – but make sure you build it well so it doesn’t pop • Why modularize? • Allow for the divide-and-conquer approach to problem solving • Break it down into smaller pieces and solve each smaller problem • Allows for greater software reusibility • Build some blocks – then re-use them. • If you repeat the same algorithm in your code 20 times and it is wrong…………….
Methods • Should perform one task • Should perform that task effectively • Basic idea: • Method invoked by a method call • Method performs and completes its task • Returns a result to the caller OR simply returns control of the program
Static Methods and Fields • Static Methods do not require an object to perform its task • Math.pow(…) – you don’t need an object of type Math • Integer.parseInt() • Double.parseDouble() • yourClass.main() • Static Field • Not a data member of an object • One doesn’t exist for each object instantiated • Where (besides Math.PI and Math.E) have you seen static fields?
Example x y s1 x y s2 public class SampleClass { private int x; private int y; public static int z; //instance methods } public class TestSampleClass { public static void main(String[] args){ SampleClass s1, s2; //other stuff } } Notice: each object does not have a “z” data member
Why is Main Static? • JVM can invoke main without having to first create an object • When you execute java from command line, you type: • java ClassName • where ClassName contains the main that you want to execute • But always starts in main – so you don’t have to specify • Every class *can* contain a main method. • jvm will execute the main that is in the class you indicate at command line.
Parameters public double findMax (double a, double b, double c) { int max = a; if(max < b) max = b; if(max < c) max = c; return max; } Remember: must be one argument for each parameter Arguments copied to parameters in order that they appear. double maxValue = findMax(5, 10, 3); Send data into a method can send multiple pieces of data
Method Calls in Method Calls return Math.max(x, Math.max(y, z)); Must be evaluated first Result is used as 2ndarg to outer call to max Excellent example of software reuse Possible to embed one call inside another
Aside - Strings String s; s = "Hello"; s += " World"; System.out.println(s); s += " "; int y = 3; System.out.println(s + y + 3); System.out.println(s + (y + 3)); Hello World Hello World 33 Hello World 6 • use + to concatenate • Actually creates a new string object holding the concatenation • Watch out for conversion/casting issues with strings.
Stopping a function from executing • No return statement – function just completes execution • Think about mutator methods • return; • method can still have void return type. • returns to the calling method • return expression; • must have a non-void return type. • return-type is the data type of value being returned.
Understanding Calls - Stack Data Structure • Understanding stacks is useful for understanding return • Stacks are LIFO • Last In – First Out • Last value put in (pushed) is first value removed (pop) • When method A calls method B, the system (jvm) must know where to return to in A when B is finished • Activation Record – information on local variables in a method
Write Code * * * * * * * * * * * * * * * Notice the space here Implement method printStars that will print a triangle of stars. It should accept an integer representing the number of lines used to make the triangle. The method should not print a triangle with more than 20 rows. Example triangle for input of 5:
Class CircleGeometry • Create Utility Class CircleGeometry with the following static methods • distance – calculates the Euclidean distance between two points in a Cartesian plane. Accepts 4 integer values: x1, x2, y1, y2 • circumference – calculates the circumference of a circle: Accepts 4 integer values: x1, x2, y1, y2; x represents center of circle, y represents point on edge of circle. • area – calculates the area of a circle: Accepts 4 integer values: x1, x2, y1, y2; x represents the center of circle, y represents point on edge of circle