CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Agenda non-void method –accessor method method with parameters –mutator method method calls in detail association relationship
Accessor method A method which returns information, the value of an object’s property. These methods typically have names that start with “get”.
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; }
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”.
Mutator example public class EcoSystem { private example1.Terrarium _t; public EcoSystem() { _t = new example1.Terrarium(); } public void setTerrarium(example1.Terrarium t){ _t = t; }
Blackboard example EcoSystem es = new EcoSystem(); es EcoSystem _t Terrarium
Blackboard example EcoSystem es = new EcoSystem(); example1.Terrarium terra = new example1.Terrarium(); es EcoSystem _t Terrarium terra
Terrarium Blackboard example EcoSystem es = new EcoSystem(); example1.Terrarium terra = new example1.Terrarium(); es.setTerrarium(terra); es EcoSystem _t Terrarium terra
Blackboard example EcoSystem es = new EcoSystem(); runtime stack heap es _t EcoSystem Terrarium invocation record
Blackboard example EcoSystem es = new EcoSystem(); example1.Terrarium terra = new example1.Terrarium(); runtime stack heap es terra _t EcoSystem Terrarium 875
Blackboard example EcoSystem es = new EcoSystem(); example1.Terrarium terra = new example1.Terrarium(); es.setTerrarium(terra); runtime stack heap es terra _t EcoSystem Terrarium 875 t invocation record 875
_t = t; Blackboard example EcoSystem es = new EcoSystem(); example1.Terrarium terra = new example1.Terrarium(); es.setTerrarium(terra); runtime stack heap es terra _t 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
Blackboard example EcoSystem es = new EcoSystem(); example1.Terrarium terra = new example1.Terrarium(); es.setTerrarium(terra); runtime stack heap es terra _t EcoSystem Terrarium 875 After method call, invocation record is removed from runtime stack.
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
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.
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();
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.
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)
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.
Revisiting Clifford
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.
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.
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).
Dog – Collar example in Java public class Dog { private Collar _collar; public Dog(Collar collar) { _collar = collar; }