Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall 645-4739 1.

Similar presentations


Presentation on theme: "CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall 645-4739 1."— Presentation transcript:

1 CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall 645-4739 alphonce@buffalo.edu 1

2

3 Agenda Unified Modeling Language (UML) Methods in general

4 UML Unified Modeling Language –industry standard Class diagrams

5 UML Unified Modeling Language –express design without reference to an implementation language For example

6 Binary Class Relationships: directional binary  two classes are involved –source class has code modification –target class does not composition –source: WHOLE –target: PART in diagram: –line decoration is on source/WHOLE –show only detail that’s needed/desired

7 package cse115; public class Dog { public Dog() { } package cse115; public class Tail { public Tail() { }

8 package cse115; public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } package cse115; public class Tail { public Tail() { }

9 METHODS

10 package lab2; public class EcoSystem { private example1.Terrarium _t; public EcoSystem() { _t = new example1.Terrarium(); } public void addTwoCaterpillars() { example1.Caterpillar c1 = new example1.Caterpillar(); example1.Caterpillar c2 = new example1.Caterpillar(); _t.add(c1); _t.add(c2); c1.start(); c2.start(); } Constructor definition Method definition

11 package lab2; public class EcoSystem { private example1.Terrarium _t; public EcoSystem() { _t = new example1.Terrarium(); } public void addTwoCaterpillars() { example1.Caterpillar c1 = new example1.Caterpillar(); example1.Caterpillar c2 = new example1.Caterpillar(); _t.add(c1); _t.add(c2); c1.start(); c2.start(); } The scope of an instance variable declaration is the entire class body. The variable can (and should) be assigned an initial value (initialized) in… The variable can be used in any method of the class. Here’s a method definition. Both the constructor and this method are in the scope of the instance variable declaration. The variable can (and should) be assigned an initial value (initialized) in… the constructor.

12 Method definition syntax public void addTwoCaterpillars() { example1.Caterpillar c1 = new example1.Caterpillar(); example1.Caterpillar c2 = new example1.Caterpillar(); _t.add(c1); _t.add(c2); c1.start(); c2.start(); } method definition

13 Method definition syntax public void addTwoCaterpillars() { example1.Caterpillar c1 = new example1.Caterpillar(); example1.Caterpillar c2 = new example1.Caterpillar(); _t.add(c1); _t.add(c2); c1.start(); c2.start(); } Remember that method definition consists of both a method header a method body.

14 Method definition syntax public void addTwoCaterpillars() { example1.Caterpillar c1 = new example1.Caterpillar(); example1.Caterpillar c2 = new example1.Caterpillar(); _t.add(c1); _t.add(c2); c1.start(); c2.start(); } Here’s the method body.

15 Method definition syntax public void addTwoCaterpillars() { example1.Caterpillar c1 = new example1.Caterpillar(); example1.Caterpillar c2 = new example1.Caterpillar(); _t.add(c1); _t.add(c2); c1.start(); c2.start(); } The method header begins with an access control modifier, public in this case.

16 Method definition syntax public void addTwoCaterpillars() { example1.Caterpillar c1 = new example1.Caterpillar(); example1.Caterpillar c2 = new example1.Caterpillar(); _t.add(c1); _t.add(c2); c1.start(); c2.start(); } return type specification ‘void’ is a special return type specification, indicating that no value is returned by the method

17 Method definition syntax public void addTwoCaterpillars() { example1.Caterpillar c1 = new example1.Caterpillar(); example1.Caterpillar c2 = new example1.Caterpillar(); _t.add(c1); _t.add(c2); c1.start(); c2.start(); } Next comes the name of the method. Method names follow the same convention as local variables

18 Method definition syntax public void addTwoCaterpillars() { example1.Caterpillar c1 = new example1.Caterpillar(); example1.Caterpillar c2 = new example1.Caterpillar(); _t.add(c1); _t.add(c2); c1.start(); c2.start(); } parameter list – empty in this example remember:argument list  method call parameter list  method definition

19 Method definition syntax public void addTwoCaterpillars() { example1.Caterpillar c1 = new example1.Caterpillar(); example1.Caterpillar c2 = new example1.Caterpillar(); _t.add(c1); _t.add(c2); c1.start(); c2.start(); } method body – delimited by braces

20 Method definition syntax public void addTwoCaterpillars() { example1.Caterpillar c1 = new example1.Caterpillar(); example1.Caterpillar c2 = new example1.Caterpillar(); _t.add(c1); _t.add(c2); c1.start(); c2.start(); } statements & (local variable) declarations – consists of local variable declarations and statements

21 Returning A Value A void method has no return value A non-void method has a return value, and the method call is an expression whose value is the returned value

22 Accessor method A method which returns the value of a property (instance variable)

23 Accessor example public class EcoSystem { private example1.Terrarium _t; public EcoSystem() { _t = new example1.Terrarium(); } public example1.Terrarium getTerrarium() { return _t; }

24 Return type specification is the type of the returned value, example1.Terrarium in this case. public class EcoSystem { private example1.Terrarium _t; public EcoSystem() { _t = new example1.Terrarium(); } public example1.Terrarium getTerrarium() { return _t; }

25 Return statement consists of the keyword “return”, followed by an expression of type indicated by the return type specification. public class EcoSystem { private example1.Terrarium _t; public EcoSystem() { _t = new example1.Terrarium(); } public example1.Terrarium getTerrarium() { return _t; }

26 Method call/invocation When method is called, body of method is carried out. Local variables of method are allocated space in invocation record. Invocation record is on runtime stack.

27 In method call: Assume the following code appears in some method…what happens? EcoSystem es; es = new EcoSystem(); example1.Terrarium terra; terra = es.getTerrarium();

28 The variable terra refers to the same Terrarium object that _t refers to. EcoSystem es; es = new EcoSystem(); example1.Terrarium terra; terra = es.getTerrarium();

29 Exercise: Draw the object diagram EcoSystem es; es = new EcoSystem(); example1.Terrarium terra; terra = es.getTerrarium();

30 Exercise: Draw the memory diagram EcoSystem es; es = new EcoSystem(); example1.Terrarium terra; terra = es.getTerrarium();


Download ppt "CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall 645-4739 1."

Similar presentations


Ads by Google