240 likes | 350 Views
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 ; }
E N D
CS12230Introduction to ProgrammingLecture 2-1 – relationships!
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 …..
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
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.
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
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
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
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
A few more examples Car – Person Chair – Student Apartment – Building Lecturer – Module – Student
Now look back and tell me what the arrows mean in the Game Class Diagram!
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
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
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
Note an instance variable of type Person And the code ….
So look at the scenario • We have 2 people fred and lynda • They each have a car • Lynda sells her car to fred
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
car2 “K123ASD” person2 “fred” “other info” owner person1 “Lynda” “other info” car1.setOwner(person2); “P425AUD” owner car1
Beware Null Pointers !!!! • Check out the toString() and toStringBad()
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!