14
Spring 2008 Mark Fontenot [email protected] CSE 1341 Principles of Computer Science I Note Set 5

CSE 1341 Principles of Computer Science I

  • 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

Page 1: CSE  1341 Principles of Computer Science I

Spring 2008

Mark [email protected]

CSE 1341Principles of Computer Science I

Note Set 5

Page 2: CSE  1341 Principles of Computer Science I

Note Set 5 Overview

Methods – In DepthStatic methodsReview of parametersReturn values and the call stack

Page 3: CSE  1341 Principles of Computer Science I

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

Page 4: CSE  1341 Principles of Computer Science I

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

Page 5: CSE  1341 Principles of Computer Science I

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?

Page 6: CSE  1341 Principles of Computer Science I

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

Page 7: CSE  1341 Principles of Computer Science I

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.

Page 8: CSE  1341 Principles of Computer Science I

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);

Page 9: CSE  1341 Principles of Computer Science I

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

Page 10: CSE  1341 Principles of Computer Science I

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

Page 11: CSE  1341 Principles of Computer Science I

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.

Page 12: CSE  1341 Principles of Computer Science I

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

Page 13: CSE  1341 Principles of Computer Science I

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

Page 14: CSE  1341 Principles of Computer Science I

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