Vous êtes sur la page 1sur 6

U NIVERSITY OF THE W ITWATERSRAND , J OHANNESBURG

COMS2003: Application and Analysis of Algorithms II


A3 Class Test 1 Memo 15 May 2009

Question 1 - 25 Marks
1. Write a method that to translate the code of a department to its name according to the following table: Code Name HR Human Resources IT Information Technology ENG Engineering Implement the method as (a) Nested if statements (b) A switch statement using the following template. public String getDeptName(String code){ \\Code goes here } Solution : Nested if - 8 marks Award 8 marks for correct code. If the statement are not nested but still correct, award 6 marks. For all the code given as solutions, bear in mind that the student could have correct code that bears no resemblance to the code in the memo. Mark students code on its own merits. public String getDeptName(String code){ if (code.equals("HR")){ return "Human Resources"; } else if (code.equals("IT")){ return "Information Technology"; } else if (code.equals("ENG")){ return "Engineering"; } } Solution : Switch - 10 marks Award 10 marks for correct code. The code is complicated by the fact that the switch statement only accepts constant integers as cases. If the student used Strings, award 7 marks. Subtract 1 mark if the case of an unknown department code was not handled.

public String getDeptName(String code){ public static String getDeptName(String code) { String[] translate={"ENG","HR","IT"}; int position = Arrays.binarySearch(translate, code); switch (position) { case 0: return "Engineering"; case 1: return "Human Resources"; case 2: return "Information Technology"; default: return "Department not found"; } } } 2. Identify when it makes sense to use a nested if statement rather than a switch statement Solution - 7 marks A switch statement only accepts integers as cases. If we need to compare anything else, a nested if statement works better. A switch statement is affected by the value of only one variable. If more than one variable needs to be compared, a nested if works better. Award 3 marks for noting that only integers are allowed and 4 marks for noting that only one variable can be compared against.

Question 2 - 25 Marks
1. Are there loops which can be expressed as a for loop which cannot be expressed as a while loop? No. - 5 marks 2. What is the purpose of expressing the loop variant? It ensures that the loop teminates - 5 marks 3. Consider the following class that stores student marks: public class Marks{ final int size = 40; final int totalMark = 120; int[] marks = new int[size]; double[] percentages = new int[size]; public void makePercentages(){ } } 2

The makePercentages method reads marks from the marks array, and calculates the corresponding percentages, storing these in the percentages array. As indicated, there are 40 students and the test has a total mark of 120. Implement the makePercentages method. Solution - 15 marks Award 15 marks for a completely correct solution. Award 5 marks if just the for loop is correct. If everything works, but the student has forgotten to make sure that he uses oating point arithmetic rather than integer arithmetic, award 12 marks. If the equation works but the loop is wrong, award 7 marks. public void makePercentages() { for (int i=0;i<size;i++){ percentages[i]=((double)marks[i]/120.0)*100; } }

Question 3 - 20 Marks
1. Explain what is meant by pass by reference and pass by value. Solution - 8 marks For 4 marks, pass by reference must be explained as the memory location of a variable being passed to a method. For another 4 marks, pass by value must be explained as a copy of the value of a variable being passed to a method. If it is not clear that the student understands that a copy is made, award only 2 marks. 2. Consider the following code : public class Dog { public String name; public int age; } public class Main { public static void changeThings(Dog aDog, int someInt, String aString) { aDog.age=24; someInt = 55; aString = new String("Bonjour"); } public static void main(String[] args) { Dog myDog = new Dog(); myDog.age = 15; myDog.name = "Snoopy"; 3

int numPeople = 40; String greeting = "Hello"; changeThings(myDog, numPeople, greeting); System.out.println("Dog : "+myDog.name +":"+myDog.age); System.out.println("People : " + numPeople); System.out.println("Greeting : "+greeting); } }

What would the output of the program be? Solution - 4 marks Award 1 mark for the correct age, another 2 for the correct number of people and another 1 for the correct greeting.

Dog : Snoopy:24 People : 40 Greeting : Hello 3. Explain your answer to the question above, with reference to the memory locations involved and the values contained therein Solution - 8 marks Award 2 marks for making it clear that myDog is passed by reference, meaning that its memory location is passed to the changeThings method. Award another 1 for noting that a copy is made of the reference, as the reference is passed by value. Award another 1 mark for noting that this means that the age value modied in the function is the same as the age value in the main method. Award 2 marks for showing that a copy of the numPeople variable is made, and called someInt, so that when it is modied, the numPeople variable is unchanged. Award 1 mark for showing that the greeting value is sent as a reference, meaning that a copy of its memory location is sent to the changeThings method. Award another 1 mark for showing that when a new String is created and assigned to aString, the original greeting is unmodied as the reference was copied.

Question 4 - 30 Marks
1. What does the static keyword do? Solution - 5 marks The static keyword indicates that a variable or method belongs to the class, rather than to instances of the class. This means that all instances of a class in effect share one copy of the 4

variable or method. 2. What is the output of the following program: class Something{ public static int value; public int property; } public class Other{ public static void main(String args[]){ Something one = new Something(); Something two = new Something(); one.value = 5; one.property = 9; two.value = 7; two.property = 4; System.out.println("Value : "+one.value); System.out.println("Property : "+one.property); } } Solution - 8 marks Value : 7 Property : 9 Award 5 marks for the rst line and three marks for the second. 3. Given the following code that performs some operations on two dimensional points, implement the Point class public class ProcessPoints{ public int distance(Point one, Point two){ int xDistance = one.getX() - two.getX(); int yDistance = one.getY() - two.getY(); double distance = Math.sqrt(Math.exp(xDistance, 2) + Math.exp(yDistance, 2)); return (int)distance; } public void movePoint(Point myPoint, int x, int y){ myPoint.setX(x); myPoint.setY(y); } } Solution - 17 marks 5

public class Point { int x; int y; public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } } Award 1 marks for correct class denition Award 2 marks for each variable declaration Award 3 marks for each correct method (including return type and parameters

Vous aimerez peut-être aussi