UNIVERSITY OF EAST ANGLIA School of Computing Sciences Main Series UG Examination 2013-14 PROGRAMMING FOR NON-SPECIALISTS CMPC2X02 Time allowed: 2 hours Section A (Attempt all questions: 80 marks) Section B (Attempt one question: 40 marks) Notes are not permitted in this examination. Do not turn over until you are told to do so by the Invigilator. CMPC2X02 Module Contact: Dr Geoff McKeown, CMP Copyright of the University of East Anglia Version 1
Page 2 1. (a) In the context of Java: SECTION A (i) distinguish between a class and an object; (ii) explain the differences between primitive data types and reference types; (iii) Distinguish between local variables and instance variables. (b) Explain what is meant by a static method in Java. Give a static method that takes a double representing a mass in pounds and returns a double representing the equivalent mass in kilograms. [If p is a mass in pounds, the equivalent mass in kilograms is given (approximately) by k = p 0.454.] [8 marks] 2. (a) In the following Java expressions, a, b, c and d are int variables. a * (9 - b) % c + d (c > 4) && (a <= 20) a!= (b + c) (i) Draw an evaluation tree for each of these expressions. [7 marks] (ii) Use these trees to evaluate each expression, assuming a has value 16, b has value 3, c has value 5 and d has value 2. (b) (i) Give a Java statement to define a constant identifier representing the number of hours worked in a normal working week (37.5 hours). (ii) Suppose workershours is an array of doubles whose elements represent the number of hours worked during a particular week. Write a fragment of Java code to determine the number of workers who worked overtime during that week.
Page 3 3. (a) Give the structure of a class defining a new type of object. [5 marks] (b) A class Car has the following fields: private String registration; private int doors; private String colour; private CarDealer supplier; (i) Define a class variable, totalcars, whose purpose is to keep track of the total number of Car objects constructed during execution of a program that uses the Car class. (ii) Define a constructor for this class which has a formal parameter corresponding to each field. (iii) Define an accessor method called getcolour whose purpose is to return the value of the colour field of the Car. (iv) Assume that the class CarDealer has a method with the header public Address getlocation() which returns the address of a CarDealer object. Define an accessor method for the class Car called getdealeraddress whose purpose is to return the address of the dealer who supplied this Car. (v) Assuming that an equals method is defined in the CarDealer class, define a method for the Car class that determines whether or not this Car has been supplied by a given CarDealer. TURN OVER
Page 4 4. (a) The Java standard library contains an interface called Comparable. Explain what is meant by the term interface in this context. (b) Explain what is meant by each of the following terms in the context of Java: (i) method overloading; (ii) method overriding; (c) Distinguish between an array and an ArrayList in Java. (d) (i) Give a Java statement to create an ArrayList called namelist to represent a list of workers names. (ii) Suppose newworker is the name of a new worker. Give a Java statement to insert this name at the end of namelist. (iii) Give an assignment statement assigning the number of workers in namelist to a variable workforcestrength.
Page 5 SECTION B 5. (a) Describe what is meant by each of the following terms in the context of Java programming: (i) inheritance; (ii) polymorphism; (iii) dynamic binding. (b) Assume that we have the following classes: Person, Administrator, Lecturer, Student, UnderGrad, PostGrad and Secretary. Administrator, Lecturer and Student are each subclasses of Person. PostGrad is a subclass of Student and Secretary is a subclass of Administrator. (i) Draw the inheritance hierarchy for the above classes. [7 marks] (ii) Which of the following assignments are legal and why? Person p1 = new Lecturer(); Person p2 = new Secretary(); Student s1 = new Person(); Student s2 = new PostGrad(); p1 = s2; [12 marks] (c) Write a brief explanation of each of the following kinds of error: (i) compile-time errors; (ii) run-time errors; (iii) logical errors. TURN OVER
Page 6 6. (a) A class LibraryBook has four fields: title, author, classification and status. The first three of these fields are of type String whilst status is of a user-defined type called BookStatus. At any time during its existence the status of a LibraryBook object is one of the following: reference only, on-loan or available for lending. (i) Define an enum type for a LibraryBook s status. (ii) Write a constructor for the LibraryBook class with explicit arguments to define the instance variables corresponding to the author and title fields. [5 marks] (iii) Write a method for the LibraryBook class to set the status of this LibraryBook to reference only, provided its status has not already been set. If the status of this LibraryBook has already been set an IllegalAttemptToSetStatusException should be thrown. (You may assume that the IllegalAttemptToSetStatusException class is available.) [7 marks] (iv) Write a boolean method for the LibraryBook class that returns true if the status of this LibraryBook is reference only, and false otherwise.
Page 7 (b) (i) Define what is meant by an algorithm. (ii) Describe the binary search algorithm for searching for a given value in a sorted array of values and state its worst-case run-time complexity. [10 marks] (iii) Show the working of your algorithm when it is used to search for 35 in the following array: a = [12,13,15,19,23,26,32,35,38,40,46,52]. END OF PAPER