FACULTY OF SCIENCE AND TECHNOLOGY EXAMINATION FOR THE BSC (HONS) INFORMATION TECHNOLOGY; BSC (HONS) INFORMATION SYSTEMS & BSC (HONS) COMPUTER SCIENCE; YEAR 1 ACADEMIC SESSION 2014; SEMESTER 2 PRG1203: OBJECT ORIENTED PROGRAMMING FUNDAMENTALS FINAL JULY 2014 EXAM CYCLE TIME: 2 HOURS + 10 MINUTES READING TIME INSTRUCTIONS TO CANDIDATES This question booklet contains two sections. Section A: Answer the compulsory questions. Section B: Answer two questions out of three questions All answers must be written in the answer booklets provided using blue or black INK IMPORTANT NOTES TO CANDIDATES Materials Allowed Standard Items : Pen, Pencil, Eraser or Correction Fluid, Ruler Special Items : Non Programmable Calculators It is your responsibility to ensure that you do NOT have in your possession any unauthorised notes or any other means that would improperly help you in your work. If you have any unauthorised materials with you, hand it to the invigilator BEFORE reading any further. DO NOT REMOVE THIS QUESTION PAPER FROM THE EXAMINATION HALL [This paper contains FOUR questions printed on SEVEN pages]
Section A Compulsory Question. Question 1 (50 marks) a) State whether the following statement is true or false. (6 marks) i. The keyword this is used to access the superclass constructor. A subclass cannot directly access private members of its superclass. A superclass can directly access the members of its subclass. A constructor must have the same name as the class name and without return type. v. Overloaded methods must have the same name. vi. The Keyword extends used to define the relationship between classes. b) Briefly define the following terms: i. Class Object Data member Constructor v. Inheritance vi. Overloaded constructor c) Explain the difference between the term instantiation and initialization. Provide one example of each using the following class. (6 marks) (6 marks) -name:string -age:int Student +getname():string +getage():int +setname(string):void +setage(int):void 1
d) Consider the following Java program. public class FinalExam { public static void main(string[] args) { int n1=10, n2=8; int result = getsum(3,6); System.out.println("The sum of 3 and 6 is " + result); System.out.println("The sum of 10 and 8 is " + getsum(n1,n2)); result = getsum(n1,-4); } static int getsum(int num1, int num2) { return num1 + num2; } } i. What will be output of the above program? Briefly explain the purpose of String[] args in the main method and how to use it in a Java program. What is the purpose of the keyword static in the above program? (3 marks) e) City Sports Club is developing a system to keep track of its member s information and to calculate their total membership fee. Each member s data consists of a name, IC number, length of membership (in years) and yearly membership fee. (10 marks) Write the corresponding UML Class Diagram for the Member class. The class diagram should include the following: i. Attributes A default constructor An overloaded constructor to initialize all attributes. Methods (accessor methods) 2
f) Consider a program to manage a bank deposit account. The account data consists of an IC number and the amount in the deposit. The yearly interest is calculated as follows. If the amount is less than 5000, the interest rate is 1.2 %, if the amount is in between 5000 to 50000 inclusive; the rate is 3.0 %, if the amount is more than 50000, the interest rate is 3.5 %. Write the code (Java class) for the above scenario by identifying the following elements: i. Object (1 mark) Attributes ( data of objects) Methods (behavior of objects) to be performed including yearly interest. Constructors (default & overloaded) (8 marks) 3
Section B Answer any TWO questions from this section. Question 2 (25 marks) a) A class BookOrder is used to process an online order of book purchase. The class diagram for the BookOrder class is given below : BookOrder -title: String -isbn: String -quantity: int -unitprice: float +gettitle(): String +getisbn(): String +getquantity(): int +getunitprice(): float +settitle(string): void +setisbn(string): void +setquantity(int): void +setunitprice(float): void +calculatetotalprice(): float +printdetails(): void From the superclass BookOrder, write the code (Java class) for the following: i. Create a subclass called BookShippingOrder. (1 mark) A data member named shippingcharge for shipping and handling charge with value RM 8.00 (a constant value with RM 8.00). An overloaded constructor that has title, isbn, and quantity ordered in the argument list, and a default constructor. Accessor method for the shippingcharge data member. v. An overriding method calculatetotalprice() to compute the total price including the shipping and handling charge. 4
b) Consider an application to store 20 whole numbers in an array. Answer the below questions: i. Write the code fragment to create an array named numberlist, to hold the 20 whole numbers. Assuming that a Scanner object, sc, has been created, write the code fragment to store the numbers in the array when the user enters the numbers at the keyboard. Assume the following variable is declared to store the total number of odd numbers in the list. int oddcount = 0; Write code fragment to calculate and display the total number of odd numbers in the list. c) Explain the term typecasting and give an example for each implicit and explicit typecasting. Question 3 (25 marks) a) Briefly explain the key differences that distinguish between: (12 marks) i. The keywords this & super Overriding method and overloading method The keywords extends and implements b) Assume that you are working with the design for a Library loan management application. (8 marks) The application must enable the user to view the loan history list, view books currently on loan, delete selected loan information, and renew the loan for selected or all books. A login is also required. Only a user with a successful login is allowed to access the application. Draw the use-case diagram that models the above application scenario: c) What is recursion? Give an example for recursive method in Java. (5 marks) 5
Question 4 (25 marks) a) Consider the following class and answer the below questions: Staff -name: String -staff_id: String #divisionno: int +Staff(): +Staff(String, String, int): +getname():string +setname(string): void +getstaff_id():string +setstaff_id(string):void +getdivisionno():int +setdivisionno(int):void i. Why is it a good practice for data members to be declared as private? When it would be appropriate for the data member name to be designated a protected? What is the symbol # represents in the above class? Identify any two mutator methods in the Staff class and provide the implementation for it. v. Write the code fragment for the tostring method to display the details of the above Staff class. b) Consider the UML diagram for the class Loan and interface LoanCalculator. (13 marks) Loan -icnum:string -noofyears:int -amount:float +Loan() +Loan(String, float) +seticnum(string):void +geticnum():string +setnoofyears(int):void +getnoofyears():int +setamount(float):void +getamount():int <<interface>> LoanCalculator +downpercentage:float +calyearint(float):float +calinideposit():float 6
The LoanCalculator interface has one constant float downpercentage as down payment percentage initialized to 3%, one abstract method public float calyearint(float interestrate), and an abstract method public float calinideposit(). The interest rate is in percentage (e.g. 3.5) The yearly interest is calculated based on the following formula: Yearly interest = interestrate * amount The down payment is calculated based on the following formula Total Amount Initial Deposit >20000 downpercentage * amount <=20000 100 Create the Java implementation of the class Loan and interface LoanCalculator. END OF EXAMINATION PAPER 7