Upload
coby
View
28
Download
1
Embed Size (px)
DESCRIPTION
CSE 1341 Principles of Computer Science I. Spring 2008 Mark Fontenot [email protected]. 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 - PowerPoint PPT Presentation
Citation preview
Note Set 5 Overview
Methods – In DepthStatic methodsReview of parametersReturn values and the call stack
MethodAllow you to break a program up in to self-contained unitsProcess called modularization – to modularize your codeWrite 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 problemAllows 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…………….
MethodsShould perform one taskShould perform that task effectively
Basic idea:Method invoked by a method callMethod performs and completes its taskReturns a result to the caller OR simply returns control of the
program
Static Methods and FieldsStatic Methods do not require an object to perform its task
Math.pow(…) – you don’t need an object of type MathInteger.parseInt()Double.parseDouble()yourClass.main()
Static FieldNot a data member of an object One doesn’t exist for each object instantiatedWhere (besides Math.PI and Math.E) have you seen static
fields?
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 }}
xy
xy
s1 s2
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 ClassNamewhere ClassName contains the main that you want to executeBut 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.
ParametersSend 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 oneargument for each parameter
Arguments copied to parametersin order that they appear.
double maxValue = findMax(5, 10, 3);
Method Calls in Method CallsPossible to embed one call inside another
return Math.max(x, Math.max(y, z));
Must be evaluated first
Result is used as 2nd arg to outer call to max
Excellent example of software reuse
Aside - Stringsuse + 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 WorldHello World 33Hello World 6
Stopping a function from executingNo return statement – function just completes execution
Think about mutator methodsreturn;
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 StructureUnderstanding stacks is useful for understanding return
Stacks are LIFOLast In – First OutLast 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 CodeImplement 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
Class CircleGeometryCreate Utility Class CircleGeometry with the following static
methodsdistance – 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