UNIVERSITY OF EDINBURGH course CS0077 FACULTY OF SCIENCE AND ENGINEERING DIVISION OF INFORMATICS SCHOOL OF COMPUTER SCIENCE Computer Science 1 Bh Degree Examination Date: Saturday 26th May 2001 Time: 12:00 13:30 (one and a half hours) Place: Roxburgh Place Room: Halls A and B Board of examiners Chair: Stuart Anderson. External Examiner: Muffy Calder. Instructions to candidates 1. Check that the question paper contains pages 1 12 after this cover page. If it does not, inform the invigilator. 2. You should attempt as many questions as possible. 3. The questions in this paper vary widely in difficulty. During your first pass through the paper, you are advised not to dwell on questions to which the answer is not readily apparent. 4. Write your answer to each question in the box or table provided. If you wish to write more in answer to a question, continue on the blank page opposite, indicating that you have done so. 5. Please write legible and concise answers. 6. The marks allocated to each part of a question are indicated in the margin. There are 100 marks in total.
Question 1 Suppose we have a program which implements the following class hierarchy fragment: ClassA / \ ClassB ClassC / \ ClassD ClassE Suppose further that we have five variables declared and assigned by this program: ClassA a = new ClassA(); ClassB b = new ClassB(); ClassC c = (classc) (new ClassE()); ClassD d = null; ClassE e = new ClassE(); Fill in the result of executing the following program fragments. If you think that the Java compiler will reject the fragment, write does not compile. Remember that the Java compiler knows the class hierarchy and will reject programs where the types are always incompatible, whatever the run-time value of variables. System.out.println(a instanceof ClassA); System.out.println(a instanceof ClassB); System.out.println(b instanceof ClassA); System.out.println(b instanceof ClassC); System.out.println(c instanceof ClassA); System.out.println(c instanceof ClassB); System.out.println(c instanceof ClassE); System.out.println(d instanceof ClassD); System.out.println(d instanceof Object); System.out.println(e instanceof Object);
Question 2 A library class ArrayBinaryTree has an array field a declared by: private Object a[]; This array is used to represent a binary search tree of objects. The public methods of ArrayBinaryTree ensure that the tree is always balanced, which means that it has the smallest depth possible for a given number of elements stored in the tree. The tree is arranged in the array like this: a[0] a[1] a[2] a[3] a[4] a[5] a[6] If the tree does not have a node at a particular position, then the corresponding element in the array is null. i. The diagram shows an array big enough to hold a tree with up to 7 elements. If the number of elements stored in the tree is n, what is the size of the array needed? [2 marks] ii. Show, as trees, the possible ways of storing the integers 1, 2, 3, 4, 5, 6 in an ArrayBinaryTree object. iii. Write a method findleast which returns the integer index of the least element in the tree, making use of the binary search tree property. You may assume that the tree always has at least one node. [5 marks]
Question 3 (a) This question concerns interfaces in Java. Recall that the purpose of an interface is to describe some methods which are to be implemented by a class. i. All methods in an interface are abstract. What does this mean? ii. Since all methods are abstract, what else should be provided to explain an interface? iii. Show how you would write the standard Comparable interface in Java, which declares the compareto method. This method takes an object parameter and returns an integer, defining an ordering relation on objects in a class. (b) This question concerns abstract classes in Java. i. What is the difference between an abstract class and an ordinary class? ii. Show how you would write an abstract class AbstractReverseComparable which implements the Comparable interface and contains a concrete definition of a method reversecompareto method in terms of compareto, which reverses the sense of the compareto comparison. This means that if a.compareto(b) = 1, then a.reversecompareto(b) = 1, and similarly for 1 and 1. [4 marks]
Question 4 (a) Client-server systems require a method of identification of particular services on particular hosts. What is the naming and numbering system which is used for this identification? How are connections between hosts implemented in the Java programming language? [5 marks] (b) A Java program in the client-server model passes in turn each element of an array of messages (msgs) from the client to the server. The server acknowledges all non-empty messages. As usual, the server s input stream (serverin) is the client s output stream (serverout) and the client s input stream (clientin) is the server s output stream (clientout). An extract of the code is shown below. On the Server: On the Client: while (line!= null) { for (int i = 0 ; i < msgs.length ; i++) { if (line.equals("")) { serverout.println(msgs[i]); // ignore empty lines serverout.flush(); } else { System.out.println(clientIn.readLine()); System.out.println (line); } clientout.println("acknowledged"); clientout.flush(); } line = serverin.readline(); } After the system has been in use for some time an error occurs. What do you think is the likely error? How are these sorts of errors usually dealt with in client-server systems? [5 marks]
Question 5 (a) Provide an equivalent Java byte code version for the Java method below. [Hint: you should use the instructions iconst, ifge, iload and ireturn.] int minabs (int x) { if (x < 0) return 0; else return x; } (b) Consider a Java virtual machine which has the following values in memory and on the stack. 1 2 memory 5 4 3 stack 2 Giving this starting state, show the intermediate states of the memory and the stack as each instruction is executed. [7 marks] 1 imul 2 istore 1 3 iload 2 4 iinc 2 1 5 istore 2 6 iconst 1 7 istore 2
Question 6 (a) You are to implement the graphical interface of a Java program to play chess. Your interface is to draw a chessboard as shown below, made up of alternating black and white squares. The frame in which the chessboard is to be drawn is 800 800 pixels, so each of the black and white squares above measures 100 100 pixels. Recall that the Color class provides the constants Color.white and Color.black and a graphics context Graphics g provides the following methods: public void setcolor (Color c) public void fillrect (int x, int y, int width, int height) Using these ingredients, provide an implementation of the paint() method for a class which displays the chessboard. [6 marks] (b) The program needs to register mouse clicks in the frame in order to identify the squares on the chessboard by their row and column position. These positions are used to index into a two-dimensional array of ChessPiece objects and so their values are to range between 0 and 7. Implement the method public void mousepressed(mouseevent e) for the MouseAdapter class listening for these mouse clicks. Recall that the MouseEvent class provides methods getx() and gety(). Update the int variables row and column accordingly. [4 marks]
Question 7 (a) Briefly describe the four steps you would undertake in debugging a program. [4 marks] [ Question continues on next page ]
(b) The method given below is intended to find the number that is the minimum of all the numbers in the Vector v that are larger than the minimum of the elements in v. You may assume that v contains at least two elements, and that that the first two elements are different numbers. int second (Vector v) { // 1 int min; // 2 int secmin; // 3 // 4 if (((Integer)v.elementAt(0)).intValue()<= // 5 ((Integer)v.elementAt(1)).intValue()){ // 6 min = ((Integer)v.elementAt(0)).intValue(); // 7 secmin = ((Integer)v.elementAt(1)).intValue(); // 8 } else { // 9 min = ((Integer)v.elementAt(1)).intValue(); // 10 secmin = ((Integer)v.elementAt(0)).intValue(); // 11 } // 12 // 13 for (int i = 2; i < v.size(); i++) { // 14 if (((Integer)v.elementAt(i)).intValue() <= min) { // 15 int tmp = min; // 16 min = ((Integer)v.elementAt(i)).intValue(); // 17 secmin = tmp; // 18 } else if (((Integer)v.elementAt(i)).intValue() <= secmin) { // 19 secmin = ((Integer)v.elementAt(i)).intValue(); // 20 } // 21 } // 22 return secmin; // 23 } // 24 i. Describe a vector of Integers that will give the correct result using the above method. [2 marks] ii. Describe a vector of Integers that will give incorrect results using the above method. [2 marks] iii. Outline how you would go about repairing the fault in the program so it would give correct results with both your tests. [2 marks]
Question 8 Consider the following brief description of the Informatics Teaching Office (ITO): The ITO maintains information about each student taking a taught course in the Division of Informatics. Each student follows a programme of studies comprising a collection of individual courses and for each course an assessment record is maintained. Each student s director of studies maintains oversight over the selection of courses and the students progress. An analyst has begun to build a class model for this system and has identified: Student, Programme, Course, Assessment and DoS have been identified as class names in building the class model. (a) Explain what is meant by an association and how associations are used to make a more elaborate class model. Illustrate your answer by identifying some associations between the classes identified in the question. (b) Explain how you might go about validating a class model. Use the example above to illustrate your answer. (c) The DoS has the responsibility to check students they supervise are making satisfactory progress. Describe, using the classes identified above and the associations you have identified, how a DoS can carry out this responsibility. You may identify additional associations if necessary. [2 marks] (d) What is meant by refactoring in object-oriented design? Explain its role in the design process. [2 marks]
Question 9 The Java language provides an operation + for concatenating two strings. (a) Using only the operations of addition, subtraction and concatenation, by repeated concatenation or otherwise, write a Java implementation of a method static String repeat(string s, int n) such that, if s is a String and n is a natural number (i.e. a non-negative integer), then repeat(s, n) is the result of concatenating n copies of s. [4 marks] [ Question continues on next page ]
(b) The following function e defined on strings makes 2 n copies of the string s. The definition uses the function + to concatenate strings. e(s, 0) = s e(s, n + 1) = e(s, n) + e(s, n) The following Java method is intended to be an implementation of this function: static String exp(string s, int n) { if (n == 0) { return s; } else { String res = exp(s,n-1); return res+res; } } Show by induction on n that e(s, n) = exp(s, n). State clearly the base case, induction step, and induction hypothesis. [6 marks]
Question 10 This question concerns stack-based evaluation of arithmetic expressions. (a) Draw syntax trees for the following expressions: 5 ((2 3) + 4) (5 2) (3 + 4) ((5 2) 3) + 4 (b) Give the expressions derived from each of these trees by (i) infix and (ii) postfix tree traversal. (c) Using the Java byte code instructions imul, iadd, iconst, show the result of compiling the expression 5 ((2 3) + 4). [4 marks]