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 non-void method –accessor method method with parameters –mutator method method calls in detail association relationship

4 Accessor method A method which returns information, the value of an object’s property. These methods typically have names that start with “get”.

5 Accessor example simple example of a method that returns a value public class EcoSystem { private example1.Terrarium _t; public EcoSystem() { _t = new example1.Terrarium(); } public example1.Terrarium getTerrarium() { return _t; }

6 Mutator methods A method which accepts information and sets the value of an object’s property. So-called because method changes (mutates) the value of a property. Mutator methods typically have names that start with “set”.

7 Mutator example public class EcoSystem { private example1.Terrarium _t; public EcoSystem() { _t = new example1.Terrarium(); } public void setTerrarium(example1.Terrarium t){ _t = t; }

8 Blackboard example EcoSystem es = new EcoSystem(); es EcoSystem _t Terrarium

9 Blackboard example EcoSystem es = new EcoSystem(); example1.Terrarium terra = new example1.Terrarium(); es EcoSystem _t Terrarium terra

10 Terrarium Blackboard example EcoSystem es = new EcoSystem(); example1.Terrarium terra = new example1.Terrarium(); es.setTerrarium(terra); es EcoSystem _t Terrarium terra

11 Blackboard example EcoSystem es = new EcoSystem(); runtime stack heap es 875 950 1025_t 1025 950 EcoSystem Terrarium invocation record

12 Blackboard example EcoSystem es = new EcoSystem(); example1.Terrarium terra = new example1.Terrarium(); runtime stack heap es terra 875 950 1025_t 1025 950 EcoSystem Terrarium 875

13 Blackboard example EcoSystem es = new EcoSystem(); example1.Terrarium terra = new example1.Terrarium(); es.setTerrarium(terra); runtime stack heap es terra 875 950 1025_t 1025 950 EcoSystem Terrarium 875 t invocation record 875

14 _t = t; Blackboard example EcoSystem es = new EcoSystem(); example1.Terrarium terra = new example1.Terrarium(); es.setTerrarium(terra); runtime stack heap es terra 875 950 1025_t 1025 875 EcoSystem Terrarium 875 t Control transfers to called method. Argument’s value is assigned to parameter. After method body is executed, control returns to statement after method call. 875 _t = t assignment copies contents of parameter to the instance variable

15 Blackboard example EcoSystem es = new EcoSystem(); example1.Terrarium terra = new example1.Terrarium(); es.setTerrarium(terra); runtime stack heap es terra 875 950 1025_t 1025 875 EcoSystem Terrarium 875 After method call, invocation record is removed from runtime stack.

16 java.awt.Color class Instances of the java.awt.Color class represent colors. A color is described by how much of each of the three colors red, green and blue are mixed together. There are some predefined color objects in the java.awt.Color class, such as: java.awt.Color.RED java.awt.Color.BLUE

17 getColor Both the Ant and the Caterpillar have getColor methods. By calling their getColor methods we can find out the color of our Ant and Caterpillar objects. The getColor method requires no argument in the method call.

18 Example Suppose that the variable a refers to an Ant. Then the following method call gets a reference to the color of the Ant object: a.getColor() We can assign that reference to a variable: java.awt.Color col = a.getColor();

19 setColor Both the Ant and the Caterpillar have setColor methods. By calling their setColor methods we can change the color of our Ant and Caterpillar objects. The setColor method requires that a reference to a java.awt.Color object be provided as an argument in the method call.

20 Example Suppose again that the variable a refers to an Ant, and suppose further that thevariable c refers to a Caterpillar. Then the following method call sets the color of the Ant object to red: a.setColor(java.awt.Color.RED) Likewise, the following method call sets the color of the Caterpillar object to blue: c.setColor(java.awt.Color.BLUE)

21 Making c and a have the same color c.setColor(a.getColor()) Swapping colors with two additional variables: java.awt.Color colorOne = a.getColor(); java.awt.Color colorTwo = c.getColor(); c.setColor(colorOne); a.setColor(colorTwo); Exercise: can you swap the colors with just one additional variable.

22 Revisiting Clifford

23 Another relationship One collar for its life…? A dog has-a tail; the tail is a part of the dog. We need something different to model a relationship like dog and collar.

24 Association Also called “knows a”. A relationship of knowing (e.g. Dog-Collar as opposed to Dog-Tail) – (back to Clifford!) No necessary lifetime link We’ll look at two different implementations of “knows a”: –The first we will see today, and is very similar to our implementation of “has a”. –The second, which we will see next time, is a bit more complex but is also more flexible.

25 First implementation In Java code, the first involves 3 changes to the “knowing” class: –Declaration of instance variable of the “known” class/type (because the “knowing” object will want to communicate with the “known” object). –Assignment of existing “known” instance to the instance variable (because the instance variable must refer to an object). –Parameter of “known” class in “knowing” class constructor (because the creator of an instance of the “knowing” class needs to supply an instance of the “known” class).

26 Dog – Collar example in Java public class Dog { private Collar _collar; public Dog(Collar collar) { _collar = collar; }


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

Similar presentations


Ads by Google