Presentasi sedang didownload. Silahkan tunggu

Presentasi sedang didownload. Silahkan tunggu

IMPLEMENTATION Pertemuan 25 Matakuliah: Konsep object-oriented Tahun: 2009.

Presentasi serupa


Presentasi berjudul: "IMPLEMENTATION Pertemuan 25 Matakuliah: Konsep object-oriented Tahun: 2009."— Transcript presentasi:

1

2 IMPLEMENTATION Pertemuan 25 Matakuliah: Konsep object-oriented Tahun: 2009

3 Implementation

4 Di Bab ini terfokus kepada Bagaimana mengkonversi model final menjadi sebuah code (program) dan mendemontrasikan bahai mana peta hubungan antara mode dan program, dalam hal ini mencakup –Bagaimana sebuah class dan hubungan antar class di terjemahkan dalam sebuah code/program –Bagaimana sebuah object dibuat dari sebuah class –Bagaimana sebuah message melakukan passing dan alur kontrol yang dimodelkan dalam sequence diagram di implementasikan didalam program Dalam bab ini implementasinya menggunakan bahasa java

5 Mengimplementasikan class diagram

6 Card Class Perbedaan utama antara model dengan program adalah operasi konstruktor Operasi konstruktor adalah sebuah operasi yang dilakukan otomatis ketika sebuah obyek atau instance sebuah class dibuat. Sebuah obyek dibuat dengan menggunakan kata new(dalam programming) Operasi konstruktor dapat diinisialisasi oleh programer bisa juga otomatis

7 –Ketika melihat code berikut, perlu diingat point berikut ini Ketika sebuah obyek card baru dibuat, konstruktor mengeset nilai attribut number dengan nilai yang ada dari parameter num Segala sesuatu yang berada diantara /* dan */ adalah sebuah komentar Segala sesuatu yang berada diantara // sampai pindah garis baru adalah komentar

8 /* parrent class for cards */ public class Card { int number; // card number public Card(int num) { number = num; // constructor copies parameter to number } // constructor not shown in class diagram int getNum() // gives card number { return number; } Card number : int getNum()

9 Staff card class /* staffCard inherits the attribute number from card, staff card contain holder name, department and expiry date */ public class StaffCard extends Card { string name; // holders name string department; // holders department string expiryDate; // card expiry date StaffCard (string belonsTo,string inDept,string expiresOn, int cardNo) { super(carNo); // call parent class constructor to assign card number name = belongTo; // StaffCard constructor copies parameter to name departement = inDept; expiryDate = expiresOn; } Card number : int getNum() StaffCard name : string departement : string expirydate : string Inherits relationship attributes

10 VisitorCard Class /* VisitorCard inherits from Card */ public class VisitorCard extends Card { string expiryDate; VisitorCard() { super(9); expiryDate=“ ”; } Card number : int getNum() VisitorCard expirydate : string

11 Mengimplementasikan model dinamik Membuat sebuah obyek Mengikuti urutan message Bagian2 program

12 Creation of the objects Di bagian ini akan didemontrasikan bagaimana mengcreate sebuah obyek Ref.Creating Class / ObjectNew Object ACarParkSystem:CarPark B :ValidCards C :Card D :CardReader E :EntranceBarier

13

14 mengikuti urutan message Dibagian ini mendemontrasikan bagaimana urutan message di sequence diagram berhubungan dengan code program. Mencari class yang mengirim message dan menerima message seperti mencari harta karun

15 Ref. No.Sending ClassPg.Message/ReturnReceiving Class 1CarPark153CarArriving()inSensor:Sensor 2Sensor157arrivalGenerated()Simulator 3 158Returns trueSensor 4 157Returns trueCarPark 5 153spaceLeft()CarPark 6 154Return trueCarPark 7 153cardReadOK()MainCardReader :CardReader 8CardReader156getCardNo()Simulator 9 157Retruns cardNoCardReader 10CardReader156validCardNo(cardNo)okCard : ValidCards 11ValidCards155* getNum()knowCard[i] : Card 12Card156Returns numberValidCards 13ValidCards155Returns trueCardReader 14CardReader156raise()inBarrier : EntranceBarrier 15EntranceBarrier157carPresent()inSensor : Sensor 16Sensor158stillThereGenerated()Simulator 17Simulator158Returns falseSensor 18Sensor158Returns falseEntranceBarrier 19EntranceBarrier157lower()EntranceBarrier 20EntranceBarrier157decSpaces()ownerCarPark : CarPark 21CarPark153spacesLeft()CarPark 22CarPark154Returns trueCarPark

16 Sequence diagram showing message sequence for the “enter the car park” use cases

17 Bagian code program Classes listed ClassPage CarParkSystem.java152 CarPark.java152 ValidCards.java154 Card.java155 CardReader.java156 EntranceBarrier.java157 Sensor.java157 Simulator.java158

18 CarParkSystem.java /* main driver class for car park simulation*/ public class CarParkSystem { public static CarPark aCarPark; public static void main(string[] args) { // set up car park with 1 entrance, 1 exit, // 5 spaces, 3 of which are full initially aCarPark = new CarPark(1,1,5,3); } A

19 CarPark.java /* class representing car park */ public class CarPark { private int capacity; // max number of spaces private int spaces; // actual spaces Sensor inSensor; // sensor for entry Sensor outSensor; // sensor for exit ExitBarrier outBarrier; // barrier for exit ValidCards currentCards; // list of valid cards CardReader mainCardReader; // card reader on entry FullSign fullLight; // class to be continued in next page

20 public CarPark(int noExits, int noEnts,int in Capacity, int noSpaces) { simulator.setMaxCardNo(10); // set max number for attempted cards capacity = inCapacity; // give value to capacity spaces = noSpaces; // give value to spaces currentCards = new ValidCards(); // create list of valid cards mainCardReader = new CardReader(this,currentCards); // create card reader // ‘this’ is standard java term used to refer to the class currently being defined InSensor = new Sensor(); // create sensor outSensor = new Sensor(); fullLight = new FullSign(); outBarrier = new ExitBarrier(this); // create exit barrier // class definition for car park to be continued B D

21 for(int carNo; carNo<50; carNo) // simulate for 50 car { if(inSensor.carArriving()) // if next event is car arrivall { System.out.print(“\Car Arriving “); if(spaceLeft()) // if any room in park { if(mainCardReader.cardReadOK()) // if valid card entered { // the card reader will handle cars entry if(!spaceLeft()) // switch on full light if full fullLight.switchOn(); fullLight.display(); // display full sign status } else System.out.print(“No Entry – Car Park Full”); }; // for block statement to be continued

22 if(spaces != capacity && outSensor.CarLeaving()) // if car park not empty and exit simulated { System.out.print(“\nCar Leaving”); outBarrier.raise(); if(spaces == 1) // if only space is one just vacated fullLight.switchOff(); fullLight.display(); } } // end of for block statement } // end of car park constructor public void decSpace() { spaces = spaces – 1; System.out.print(“Spaces Left = “ + spaces + “ “); }

23 public void incSpaces() { // add 1 to spaces available spaces = spaces + 1; System.out.print(“Spaces left = “+ spaces +” ”); } public boolean spaceLeft() { // indicates if spaces available if(spaces>0) { return true; } else { System.out.print(“No Space Left”); return false } } // end of class CarPark…. 6 and 22

24 ValidCard.java /* class representing list off valid cards */ public class ValidCards { Card knownCards[]; // list of valid card int i; ValidCards() { // set up lift of known card. Better system would store in linked list or file knownCard = new Card[8]; // assign memory for valid cards // add 6 staff cards knownCard[0] = new StaffCard(“Fred”,”Sales”,” ”,1); knownCard[1] = new StaffCard(“Sue”,”Sales”,” ”,2); knownCard[2] = new StaffCard(“Frank”,”Research”,” ”,3); knownCard[3] = new StaffCard(“Mary”,”Testing”,” ”,4); knownCard[4] = new StaffCard(“Bill”,”Research”,” ”,5); knownCard[5] = new StaffCard(“Jill”,”Testing”,” ”); // add 2 visitor card knownCard[6] = new VisitorCard(); knownCard[7] = new VisitorCard(); } // end of constructor ValidsCard C

25 public boolean validCard(int CardNo) // function to check the valid card { boolean valid = false; // initial value // search known card to see if number corresponds for( i = 0; i < 8; i++) { if(knownCard[i].getNum() == cardNo) { valid = true; } if(valid) { System.out.print(“Valid Card “+ cardNo + “.”); return true; } else { System.out.print(“Invalid Card “+ cardNo + “.”); return false; } 11 13

26 Card.java public class Card { int number; // card number public Card(int num) { number = num; } int getNum() { return number; } 12

27 StaffCard.Java /* staffCard inherits the attribute number from card, staff card contain holder name, department and expiry date */ public class StaffCard extends Card { string name; // holders name string department; // holders department string expiryDate; // card expiry date StaffCard (string belonsTo,string inDept,string expiresOn, int cardNo) { super(carNo); // call parent class constructor to assign card number name = belongTo; // StaffCard constructor copies parameter to name departement = inDept; expiryDate = expiresOn; }

28 VisitorCard.java /* VisitorCard inherits from Card */ public class VisitorCard extends Card { string expiryDate; VisitorCard() { super(9); expiryDate=“ ”; }

29 CardReader.java /* class representing Card reader */ public class CardReader { private ValidCard okCards; // list of valid cards EntranceBarrier inBarrier; // barrier controled by card reader public CardReader(CarPark aCarPark, ValidCards centralList) // constructor { okCards = centralList; // create association inBarrier = new EntranceBarrier(aCarPark); // create aggregation } E

30 public boolean CardReadOK() // is card valid ? { int cardNo; // number of card in machine cardNo = Simulator.getCardNo();//simulate reading if(okCards.validCard(cardNo) // validate card { inBarrier.raise(); return true; // let car in if ok } else { System.out.print(“Entry refused, invalid card number”); // deny entry return false; }

31 Barrier.java /* class to represent car park barrier parent class for entrance and exit barrier */ public class Barrier { boolean up = false; // status off barrier CarPark ownerCarPark;// car park to which barrier belongs public Barrier(CarPark aCarPark) // constructor { ownerCarPark = aCarPark; } public void raise() { up = true; System.out.print(“Barrier raised”); } // barrier class to be continued

32 public lower() { up = false; System.out.print(“Barrier lowered”); } } // end of barrier class

33 EntranceBarrier.java /* Entrance Barrier Class which specialises Barrier */ public class EntranceBarrier extends Barrier { public EntranceBarrier(CarPark aCarPark) { super(aCarPark); // pass parameter to parent barrier class } public void raise() // let a car in { super.raise() // use parents method to raise barrier while(OwnerCarPark.inSensor.carPresent()) { // do nothing just wait } lower(); } 15 19

34 void lower() // card { super.lower(); ownerCarPark.decSpaces(); } } // end of class barrier 20

35 ExitBarrier.java /* class for exit barrier */ public class ExitBarrier extends Barrier { public ExitBarrier(CarPark aCarPark) { super(aCarPark); } public void raise() { super.raise(); while(ownerCarPark.outSensor.CarPresent()) { // do nothing,just wait } lower(); } void lower() { super.lower(); onwerCarPark.incSpaces(); }

36 Sensor.java /* class to indicate presence of car at entrance or exit sensor */ public class Sensor { public boolean carArriving() //simulated arrival of car { return(Simulator.arrivalGenerated()); } public boolean carLeaving() //simulated departure of car { return(Simulator.departureGenerated()); } public boolean carPresent() // simulated sensor detecting car { System.out.print(“Car present”); return(Simulator.stillThereGenerated()); } 2 and 4 16 and 18

37 FullSign.java /* class for the full sign */ class FullSign { boolean lightOn; FullSign() { lightOn = false; // initialy } void switchOn() { lightOn = true; } void switchOff() { lightOn = false; }

38 void display() { System.out.print(“Full Light is “); if(lightOn) System.out.println(“ON”); else System.out.println(“OFF”); } } // end of class FullSign

39 Simulator.java /* Class to produce random arrival, departures, and car number */ public class Simulator { static int maxCarNo; // higest card number can be issued public static boolean arrivalGenerated() { //this method true if an arrival has occurred. return(Math.random() <0.7) ;// 0.7 is empirical value to } //give full range of car park states public static boolean departureGenerated() { return(Math.random() >0.7); } 3

40 public static boolean stillThereGenerated() { // this method true if car is still detected by sensor return(Math.random() < 0.5); } public static int getCardNo() { return(int) (maxCardNo*Math.random()); } } // end of class simulator 17 9


Download ppt "IMPLEMENTATION Pertemuan 25 Matakuliah: Konsep object-oriented Tahun: 2009."

Presentasi serupa


Iklan oleh Google