BSc. (Hons.) Software Engineering Cohort: BSE/04/PT Examinations for 2005-2006 / Semester 2 MODULE: OBJECT ORIENTED PROGRAMMING MODULE CODE: BISE050 Duration: 2 Hours Reading Time: 5 Minutes Instructions to Candidates: 1. Answer all questions. 2. Questions may be answered in any order but your answers must show the question number clearly. 3. Always start a new question on a fresh page. 4. All questions do not equal marks. 5. Total marks 70. This question paper contains 3 questions and 6 pages. Page 1 of 6
ANSWER ALL QUESTIONS QUESTION 1: (25 MARKS) (a) Draw out a UML class diagram that captures the following information: An Employee is a kind of Person. An ATM is a kind of Machine. Both an Employee and an ATM can behave as if they were a Cashier, in that it is possible to both withdraw cash from them and deposit cash with them. (b) Now consider the Person class and the Cashier interface listed below. Write the Java code for an implementation of the Employee class of part 1(a). Do not complete the body of any methods in the Employee class - just place comments to outline the actions that should occur: public class Person { public int age; public int secid_number; public interface Cashier { public void withdraw(int account_id, int amount); public void deposit(int account_id, int amount); (c) The Java class for Person in part 1(b) does not illustrate best practice in Java programming. i. Modify the Person class to make it robust so that it is not possible for negative values of the fields of an instance of Person to be set. ii. Define a constructor for the Person class that enables an instance to be created with specific age and secid_number values. iii. Define a constructor for the Employee class that also enables specific age and secid_number values to be set. iv. What is the difference between declaring a field private and declaring it protected? Question 1: (continued) (6 marks) (Continued) Page 2 of 6
(d) We now want to be able to use the Person class in an application. i. What is the name that must be used for the method that defines the entry point to the application? ii. How should this method be declared? iii. How would you create an instance of a Person when using this method? iv. Write the Java code for a method that enables us to update the age of a Person. v. Name a Java package that could be used to avoid having to input and output data at a command prompt. (e) Write statements in Java for the following: i. to compare two strings s1 and s2 for equality; ii. to declare a useful constant Pi, whose value is 3.14159; iii. an array, equip, of 20 objects of type Equipment. (3 marks) (f) Consider the following statements int[] smallnumbers = {5, 6, 7, 8, 9, 10; int[] largenumbers = {510, 610, 710, 810, 910, 1010, 1110; System.arraycopy(smallNumbers, 2, largenumbers, 1, 3); for (int i = 0; i < largenumbers.length; i++) System.out.println(i + ": " + largenumbers[i]); What is the output after the code is executed? (2 marks) (g) Suppose we have created a Point class which has fields holding the x- and y-coordinates of a point in 2-dimensional space. Suppose we create an instance p of the Point class as follows: Point p = new Point(1.0, 2.0); We next write the following statement: Point q = p; Suppose now we assign the x co-ordinate of q the value 42.0: q.x = 42.0; What value is output when we execute the statement: System.out.println(p.x); (2 marks) Page 3 of 6
QUESTION 2: (25 MARKS) Vehicle -int numberofwheels +vehicle() #vehicle(int wheels) +int getnumberofwheels() +boolean hasengine() Car Bicycle MotorCycle Figure 1: A class hierarchy Consider the class hierarchy of vehicles shown in Figure 1, with the following additional information: Cars always have four wheels; Bicycles and their subclasses always have two wheels. Note: there are only default (i.e. parameter-less) constructors available to the clients of the hierarchy. (a) Convert the hierarchy of Figure 1 to Java. (7 marks) (b) For each of the following statements pertinent to classes of the hierarchy state whether they are true or false in Java and give a brief explanation why it is or it is not the case. i. Vehicle [] v1 = new Vehicle[3]; ii. Vehicle v2 = new Vehicle(); iii. Vehicle v3 = new MotorCycle(); iv. Car c1 = new Bicycle(); v. Bicycle b = new MotorCycle(); (2 marks each) (Continued) Page 4 of 6
Question 2: (continued) (c) Describe overriding and method overloading. Write some code to show an example. (d) An interface is a little like an abstract class that must be extended in exactly the manner that its abstract methods specify. Give two differences of an interface from an abstract class. Page 5 of 6
QUESTION 3: (20 MARKS) (a) The following is a simple example of a Java Class: // File: Welcome.java public class Welcome { public static void main(string args[]) { System.out.println( Welcome to Java World ); Convert this file into an applet that prints out the same message when it is executed on a web page. Ensure that the text is written at the pixel position (10, 10) from the top left corner of the applet window. (6 marks) (b) We can now call this applet from inside a web page. Suppose we wish to display the applet inside an applet window that is 200 pixels wide and 50 pixels high. What is the line of html code that needs to be inserted into an html file to do this? (2 marks) (c) Now modify the applet from Part 3(a) so that the text is written out with the following properties: Font is Arial, Bold; Font size is 26 point; Text colour is blue. (d) In Part 3(c), the text string and the font size have been hard-wired into the applet definition. However, we can pass both of these in to the applet as parameters. Modify the code you have written so far to enable both the text to be written on the screen, and the font size to be entered as parameters. (e) Now we can reuse the applet in a number of different web pages, and print out different strings in different sizes. Write down the html fragment that will enable the applet to be called, with parameter values for the text string and font size set as Aren t I clever!, and 18, respectively. ***END OF QUESTION PAPER*** Page 6 of 6