150 likes | 334 Views
CS1020 Data Structures and Algorithms I Tutorial 2. Advanced OO Concepts. Q1 Programming Model. Indicate whether the following statements about procedural programming and OOP are true or false and state your reason.
E N D
CS1020 Data Structures and Algorithms ITutorial 2 Advanced OO Concepts
Q1 Programming Model Indicate whether the following statements about procedural programming and OOP are true or false and state your reason. It is easier to add or delete functions in OOP compared to procedural programming. If I want to write a simple code (e.g. less than 100 lines), using OOP will be less tedious and faster (performance wise). [CS1020 Tutorial 2 AY2013/4 S2]
Q1 Programming Model If I want to build a huge project with many functions and involving a lot of people, I should use procedural programming instead of OOP. If I want to disallow user to alter some key attributes in my program, I should use procedural programming instead of OOP. [CS1020 Tutorial 2 AY2013/4 S2]
Q2 OO Design Principles Attributes should be private unless there is a good reason to make them public. A Student class is used to keep track of students within a school. Each student may take up to 7 modules in a semester. class Student { publicString studentName; privateString[] modules; privateintnumOfModules = 0; privateintnumOfStudents; private static double cumulativeGPA; publicStudent(String studentName) { studentName= studentName; cumulativeGPA= 0.0; numOfStudents++; } … Since java automatically initializes static variables of primitive data types to some default values, this is not an error. However, it is always a good practice to initialize a variable before using it. Should add "this.". The modifier "static" should be added to all class attributes but not to instance attributes. Need to initialize modules. Identify the problems in this program. [CS1020 Tutorial 2 AY2013/4 S2]
Q2 OO Design Principles // Accessor for student name privateString getStudentName(String studentName) { studentName = studentName; returnstudentName; } private static intgetNumOfStudents() { returnnumOfStudents; } private static String[] getModules() { return modules; } … Extra statement Accessors and mutators should be public. The modifier "static" should be added to all class methods but not to instance methods. [CS1020 Tutorial 2 AY2013/4 S2]
Q2 OO Design Principles // Method to add modules private booleanaddModule(String newModule) { if (numOfModules < 7) { modules[numOfModules] = newModule; numOfModules++; return true; } else { System.out.println("Module workload exceeded!"); return false; } } private intgetNumOfModules() { returnnumOfModules; } private static double getCumulativeGPA() { return cumulativeGPA; } } Same issues as mentioned in the previous slide. [CS1020 Tutorial 2 AY2013/4 S2]
Q3 UML Design and modeling; Overriding • A small school wants to develop an information system to keep track of all its assets in its rooms. • There are only 3 rooms in the school, namely: LT1, LT2, Tutorial Room 1 • Each room has a room name and one or more assets. • Each asset has a name, a type, and a serial number. • Room names and asset information will never be changed. • It should be possible to add new assets to a room and search for an asset within a room. • Overriding methods should be used in the design. [CS1020 Tutorial 2 AY2013/4 S2]
Q3 UML Design and modeling; Overriding A small school wants to develop an information system to keep track of all its assets in its rooms. Two classes: Room and Asset • Each room has a room name and one or more assets. Each asset has a name, a type, and a serial number. Room: room name (string), assets (collection of Asset objects) Asset: asset name (string), type (string), serial number (integer) • Draw a UML diagram that models the school’s requirements. Include suitable constructors and accessors/mutators to provide access control. • What are the classes in this problem? • What are the attributes of each class? [CS1020 Tutorial 2 AY2013/4 S2]
Q3 UML Design and modeling; Overriding • Room names and asset information will never be changed.It should be possible to add new assets to a room and search for an asset within a room.Overriding methods should be used in the design. Room: constructor: Room(…), accessors: getRoomName(…) overriding methods: toString(…), equals(…) other methods: addAsset(…), hasAsset(…) Asset: constructor: Asset(…), accessors: getAssetName(…), getItemType (…), getAssetNumber(…), overriding methods: toString(…), equals(…) • What are the methods in each class? [CS1020 Tutorial 2 AY2013/4 S2]
Q3 UML Design and modeling; Overriding • Each room has a room name and one or more assets. Aggregation • What is the relationship between the two classes? [CS1020 Tutorial 2 AY2013/4 S2]
Q3 UML Design and modeling; Overriding public class Asset { private String name; private String itemType; privateintassetNumber; public Asset(String name, String itemType, intassetNumber){ this.name = name; this.itemType = itemType; this.assetNumber= assetNumber; } public String getName() { return name; } … // accessors for item type and asset number omitted You should practice writing classes with attributes, constructors, accessors, mutatorsand overriding methods until it becomes natural to you. Write the codes to satisfy the UML model [CS1020 Tutorial 2 AY2013/4 S2]
Q3 UML Design and modeling; OverridingOverriding public String toString() { returnitemType + ": " + name + " (Serial: " + getAssetNumber() + ")"; } public booleanequals(Object other) { if (other instanceof Asset) { Asset otherAsset = (Asset) other; return(otherAsset.assetNumber == this.assetNumber &&otherAsset.itemType.equals(this.itemType) && otherAsset.name.equals(this.name)); } else{ return false; } } } … [CS1020 Tutorial 2 AY2013/4 S2]
Q3 UML Design and modeling; OverridingOverriding public class Room { privateString roomName; privateAsset[] roomAssets; publicRoom(String roomName) { this.roomName= roomName; this.roomAssets= new Asset[0]; } publicString getRoomName() { returnroomName; } public booleanhasAsset(Asset searchAsset) { for(inti = 0; i < roomAssets.length; i++) if(searchAsset.equals(roomAssets[i])) return true; return false; } [CS1020 Tutorial 2 AY2013/4 S2]
Q3 UML Design and modeling;Overriding public void addAsset(Asset newAsset) { if (roomAssets == null) { roomAssets= new Asset[1]; roomAssets[0] = newAsset; } else { Asset[] tmpNewRoomAssets = new Asset[roomAssets.length+1]; tmpNewRoomAssets[tmpNewRoomAssets.length-1] = newAsset; for (inti = 0; i < roomAssets.length; i++) tmpNewRoomAssets[i] = roomAssets[i]; this.roomAssets= tmpNewRoomAssets; } } [CS1020 Tutorial 2 AY2013/4 S2]
Q3 UML Design and modeling; OverridingOverriding publicString toString() { String description = "Room: " + roomName + "\n"; for(inti = 0; i < roomAssets.length; i++) description += "- " + roomAssets[i] + "\n"; return description; } } [CS1020 Tutorial 2 AY2013/4 S2]