1 / 26

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

jerica
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

  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. EcoSystemes = new EcoSystem(); Blackboard example EcoSystem es Terrarium _t

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

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

  11. EcoSystemes = new EcoSystem(); es 1025 Blackboard example runtimestack invocation record 875 950 Terrarium heap 1025 950 _t EcoSystem EcoSystem

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

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

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

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

  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.Colorcol = 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.ColorcolorOne = a.getColor(); java.awt.ColorcolorTwo = 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; } }

More Related