1 / 24

CS12230 Introduction to Programming Lecture 2-1 – relationships!

CS12230 Introduction to Programming Lecture 2-1 – relationships!. Brief Review. I hope you can create a ‘simple’ class diagram with associated code?. public class Treasure{ private String type; p rivate float value; public Treasure(String t, float startv ) { type=t; value= startv ; }

Download Presentation

CS12230 Introduction to Programming Lecture 2-1 – relationships!

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. CS12230Introduction to ProgrammingLecture 2-1 – relationships!

  2. Brief Review I hope you can create a ‘simple’ class diagram with associated code? public class Treasure{ private String type; private float value; public Treasure(String t, float startv) { type=t; value=startv; } Etc etc } Treasure --------------------- -String type -float value --------------------- +Treasure (String t, float startv) +String toString() etc etc • All objects of this class will have these instance variables and be able to do the methods • and then …..

  3. You can use BlueJ to …. • Type in the code for the class • Create lots of objects of this class • Call their methods on both the object bench and the code pad

  4. Are you happy up to here???

  5. Review and details public Treasure(String t, int startp) { type=t; power=startp; } What does this mean? Treasure t1= new Treasure(“gold”, 123); Classobjectconstructor arguments namenamename t1 is an object variable that is built using the Treasure class blueprint and at the point you say ‘new’ you grab some memory in the computer of ‘right shape’ The argument “gold” is first put into the parameter ‘t’ then it is moved into the instance variable ‘type’ of the t1 object. Then the same with 123.

  6. Just a bit more on constructors Person p; //says ‘p will point to Person type objects p=new Person(); //now let p point to one OR // and save its location (p is 123) p= new Person(“fred”,”622452”); //both lets p point and also fills //the appropriate instance vars. p This has an address – say 123 fred

  7. Now …

  8. An Object-oriented program is • A set of COOPERATING OBJECTS that do something eg. module registration has Student objects, Module objects, Room objects …. • These objects are instances of classes, so that means that there are • RELATIONSHIPS BETWEEN THE CLASSES

  9. Class diagram (also possible relationships) OMIT IF NOT ‘INTERESTING’ Player --------------------- -String name -int powerPoints -Treasure[] booty -Image image --------------------- +void takeTurn() MUDGame --------------------- -Player[] players -Treasure[] treasures --------------------- +void run() 1..1 0..* 1..1 0..* 1..1 0..1 Treasure --------------------- -String specialPower -Player holder -Image image --------------------- +void applyPower() 1..1 Image --------------------- -String filename 0..* 1..1 1..1

  10. RELATIONSHIPS and CARDINALITY This is very important – try it yourself If you don’t get it – PLEASE, PLEASE ask again Every Tree has (a minimum of 0 and a maximum of) many Leafs you may have seen or 1:* in middle of line WE DO NOT USE THAT NOTATION IT IS NO LONGER STANDARD The side the cardinality is on is crucial!!!!! Every Tree has exactly 1 RootBall Leaf Tree 0..* 1..1 Tree RootBall

  11. A few more examples Car – Person Chair – Student Apartment – Building Lecturer – Module – Student

  12. Now look back and tell me what the arrows mean in the Game Class Diagram!

  13. This is the Class Diagram. We think about the classes as ways of organising the things in the program The relationships say: Each Game can have many Players and many Treasures Each Player may have many Treasures / Each Treasure can be owned by at most 1 Player Game 0..* 0..* 0..* Treasure Player 0..1 Then, as the program runs, objects of these classes are created and told to do things. To start with there will be nothing, then a game, then some treasures perhaps, then the players game The diagram changes as the game is played – perhaps a player dies

  14. This is the Class Diagram. We think about the classes as ways of organising the things in the program The relationships say: Each Game can have many Players and many Treasures Each Player may have many Treasures / Each Treasure can be owned by at most 1 Player MUDGame 0..* 0..* 0..* Treasure Player 0..1 Then, as the program runs, objects of these classes are created and told to do things. To start with there will be nothing, then a game, then some treasures perhaps, then the players game A Player can pick up the ring

  15. So, How do we model this kind of relationship? Car --------------------- -String license -int engineSize -Person owner ---------------------- +Car(String l) +void setOwner(Person o) +void drive() Person --------------------- -String name -String phone --------------------- +Person (String n, String p) +void changeName() 1..1 Every Car has exactly one Owner Every Treasure has ??? Players who hold it

  16. Note an instance variable of type Person And the code ….

  17. So look at the scenario • We have 2 people fred and lynda • They each have a car • Lynda sells her car to fred

  18. car2 “K123ASD” person2 “fred” “other info” owner person1 “Lynda” “other info” Car car1=new Car(“p425aud”,”volvo”); Car car2 = new Car(“k123asd”,”mini”); Person person1=new Person(“Lynda”); Person person2=new Person(“fred”); car1.setOwner(person1); car2.setOwner(person2); “P425AUD” owner car1

  19. car2 “K123ASD” person2 “fred” “other info” owner person1 “Lynda” “other info” car1.setOwner(person2); “P425AUD” owner car1

  20. And the code again….

  21. Beware Null Pointers !!!! • Check out the toString() and toStringBad()

  22. We will do same for Monster and Treasure in the next lab hour • Every Treasure has 0..1 Monsters who is/are currently holding it • Every Monster has ??? Treasures • Etc etc do on board maybe • Unfortunately we can’t do the code for ‘lots’ yet!

More Related