Presentation is loading. Please wait.

Presentation is loading. Please wait.

Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 5.

Similar presentations


Presentation on theme: "Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 5."— Presentation transcript:

1 Spring 2008 Mark Fontenot mfonten@engr.smu.edu CSE 1341 Principles of Computer Science I Note Set 5

2 Note Set 5 Overview Methods – In Depth Static methods Review of parameters Return values and the call stack

3 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…………….

4 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

5 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?

6 Example 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 } xyxy xyxy s1s2 Notice: each object does not have a “z” data member

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

8 Parameters Send data into a method can send multiple pieces of data 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);

9 Method Calls in Method Calls Possible to embed one call inside another return Math.max(x, Math.max(y, z)); Must be evaluated first Result is used as 2 nd arg to outer call to max Excellent example of software reuse

10 Aside - Strings use + to concatenate Actually creates a new string object holding the concatenation Watch out for conversion/casting issues with 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

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

12 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

13 Write Code 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: * * * * * * * * * * * Notice the space here

14 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


Download ppt "Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 5."

Similar presentations


Ads by Google