UNIVERSITY OF EAST ANGLIA School of Computing Sciences Main Series UG Examination 2017-18 PROGRAMMING 1 CMP-4008Y Time allowed: 2 hours Answer FOUR questions. All questions carry equal weight. Notes are not permitted in this examination. Do not turn over until you are told to do so by the Invigilator. CMP-4008Y Module Contact: Dr Gavin Cawley, CMP Copyright of the University of East Anglia Version 1
Page 2 1. (a) In the context of the Java programming language: (i) Distinguish between an instance variable and a local variable. (ii) Distinguish between a class and an object. (b) The implementation of the bubble sort algorithm (used to sort an array of integers into ascending order) shown in the code fragment below contains five errors. Identify these errors and give a corrected implementation. public static sort(int[] x) boolean sorted; do sorted = true; for (int i = 1; i < x.length; i++); if (x[i-1] < x[i]) int tmp = x[i]; x[i-1] = tmp; x[i] = x[i-1]; sorted = false; while (sorted); (c) A prime number is an integer greater than 1 that is only divisible, without leaving a remainder, by itself and by 1. For example seven is a prime number, but nine is not as it is divisible by three. Write a static method that returns an array containing the first n prime numbers, where n is a formal parameter of the method.
Page 3 2. (a) With the aid of diagrams and examples, describe the waterfall model of software development. (b) What is the output of the following code fragment? Justify your answer. Hint: the str.charat(i) returns the char with index i (i.e. the (i + 1) th character) in the String object referenced by str (20 marks) String str = "I think, therefore I am!"; for (int i = 0; i < str.length(); i++) switch (str.charat(i)) case a : case e : case i : case o : case u : continue; case, : break; case t : System.out.print( T ); case? : System.out.print(! ); System.out.print(str.charAt(i)); TURN OVER
Page 4 3. The duration of an event, for instance the time taken to run an Olympic marathon, can be represented by three integer values, representing the number of hours, minutes and seconds that have elapsed since the start. (a) Define a Java class describing a duration, represented by three integer instance variables. (b) Define an appropriate set of constructors. (c) Define appropriate accessor methods for the instance variables. (d) Define a method returning the total number of seconds elapsed for a Duration object as an int. (e) Define an instance method implementing the addition of two Duration objects, returning the result as a new Duration object. (f) Overload the tostring method inherited from class Object, to generate a String representing a Duration object in an appropriate format. (g) Define the class such that it implements the interface shown below public interface Comparable int compareto(object o); The compareto method returns a negative integer value if the Object to which the method is applied is to be considered less than the Object passed to the method as an argument, zero if the objects are to be considered equal and a positive integer otherwise. (2 marks)
Page 5 4. (a) Define what is meant by an algorithm. (b) How are algorithms formally described at the specification level? (c) State what is meant by a recursive algorithm. (d) What rules must be followed when implementing a recursive algorithm? (e) Consider the recursive algorithm in Algorithm 1. Informally, describe what this algorithm does and give the output of the algorithm for an input array A = [5,8,2,4,6]. Algorithm 1 AnAlgorithm(A[1..n], length) Require: Input: Integer array A[1..n], Integer length 1: if length = 1 then 2: return A[1]; 3: end if 4: return A[length]+AnAlgorithm(A,length 1); (2 marks) (f) Formally describe an iterative implementation of the recursive algorithm given in Algorithm 1. (g) When is a recursive algorithm tail recursive? Is the algorithm in Algorithm 1 tail recursive? TURN OVER
Page 6 5. (a) In the context of Java, what is an Exception and what are they used for? (b) Explain how Exceptions are handled in Java using the try, catch, and finally keywords. (c) Consider the following Java code: public static void progexamalgo() System.out.println("Please enter a month" + " between 1 and 12"); Scanner scan = new Scanner(System.in); int input = scan.nextint(); String season = getseason(input); System.out.println(season); public static String getseason(int input) String season = "Winter"; if(input > 2 && input <= 5) season = "Spring"; else if(input > 5 && input <=8) season = "Summer"; else if(input > 8 && input <= 11) season = "Autumn"; return season; Informally, explain what the progexamalgo algorithm does. Describe each step in a typical successful execution of the program and give an example output. (d) Assuming all relevant classes are correctly imported, what will happen if a user runs progexamalgo and: (i) enters 4 when prompted? (ii) enters Feb when prompted? (iii) enters 4.6 when prompted? (iv) enters 2 when prompted? Justify each of your answers. (e) Rewrite progexamalgo so that the code will exit normally if the user input is invalid. The code should throw an Exception if a valid integer outside of the range 1-12 is specified, and catch any other Exception. If an Exception of any kind is caught, a message should be printed to the user stating Invalid input". (f) Declare an enum called SEASONS that has four possible values: Spring, Summer, Autumn, and Winter. This enum should have public access and not require an instantiated instance of any object.
Page 7 6. (a) In the context of Java, what does inheritance mean? (b) Given the following class: public abstract class Building protected int numrooms; protected int numfloors; public abstract void printinfo(); (i) Write Java code for a class House that inherits from Building. It should have a field for the number of bedrooms, a field for the number of bathrooms, and an implementation of printinfo that prints the number of bedrooms. (ii) Write Java code for a class Office that inherits from Building. It should have a field for the number of desks, and an implementation of printinfo that prints the number of desks. (iii) Write Java code for a class Road. Road should have a single field to store an array of Building objects, and a method printroad that loops through all Building objects stored by the Road and calls printinfo on each. You do not need to include constructors, accessors, or mutators in your answers. (c) Assume that you have a Road object that contains both House and Office objects within its collection of Building objects. Explain what will happen when you call the printroad method for this Road. (d) Draw a UML diagram to describe the relationship between Building, House, Office, and Road. You do not need to include constructors, accessors, or mutators in your UML diagram. END OF PAPER