1 / 28

CSE115: Introduction to Computer Science I

CSE115: Introduction to Computer Science I. Dr. Carl Alphonce 219 Bell Hall 645-4739 alphonce@buffalo.edu. Phones off Signs out Picnic 1:00-3:00 . Agenda. Methods. METHODS. Constructor definition. package lab2; public class EcoSystem { private example1.Terrarium _t;

iniko
Download Presentation

CSE115: Introduction to Computer Science I

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Phones off Signs out Picnic 1:00-3:00 

  3. Agenda • Methods

  4. METHODS

  5. Constructor definition 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(); } } Method definition

  6. 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 instance variable can (and should) be assigned an initial value (initialized) in… the constructor. It can be used in any method in class body. 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(); } }

  7. Method definition syntax method definition 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(); }

  8. Method definition syntax • Remember that method definition consists of both a method header a method body. 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(); }

  9. Method definition syntax • Here’s the method body. 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(); }

  10. Method definition syntax • The method header begins with an access control modifier, public in this case. 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(); }

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

  12. Method definition syntax Next comes the name of the method. Method names follow the same convention as local variables 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(); }

  13. Method definition syntax parameter list – empty in this example remember: argument list  method call parameter list  method definition 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(); }

  14. Method definition syntax method body – delimited by braces 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(); }

  15. Method definition syntax • statements & (local variable) declarations – consists of local variable declarations and statements, which in this case are all method calls. 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(); }

  16. Returning a value • A void method has no return value, and the method call is not an expression (*) • A non-void method has a return value, and the method call is an expression whose value is the returned value * Technically not quite true – void is a type, whose sole value is also called void. Some languages call the type void by the name Unit. Its only role in Java is as the return type specification of methods which do not return a value.

  17. Accessor method • A method which returns the value of a property (instance variable)

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

  19. 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.TerrariumgetTerrarium() { return _t; } }

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

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

  22. 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(); } } Recall this code from an earlier slide:

  23. EcoSystemes = new EcoSystem(); es.addTwoCaterpillars(); • Assume the following code appears in some method…what happens when it is executed? • Let’s draw the memory and object diagrams (on the board) to give us a better understanding.

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

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

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

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

More Related