University of Cape Town Department of Computer Science Computer Science CSC115F Class Test 2 Solutions Wednesday 6 April 2005 Marks: 40 Approximate marks per question are shown in brackets Time: 40 minutes The use of calculators is permitted NAME: Surname Initials STUDENT NO: COURSE CODE: CSC This paper consists of 6 questions and 9 pages (including this cover page). Mark Allocation Question Marks Internal External Quest Marks Internal External 1 [5] 4 [5] 2 [8] 5 [11] 3 [5] 6 [6] Total Total Grand Total Final Mark Internal Examiner: External Examiner: 1
Question 1. [5 marks] a) What is iteration? Executing the same task or set of statements multiple times b) What is a counter-controlled loop? Name such a statement in Java. Loops that usually execute for a fixed number of times OR A special counter variable is used to control the loop and may be referred to within the loop Java provides the for statement [1] [2] c) What is a condition-controlled loop? Name such a statement in Java. If we do not know the number of iterations a priori (in advance), we can use a condition-controlled (or event-controlled) loop - where the loop executes while a condition is true Two statements: while OR do while [2] Question 2. [ 8 marks] Write a simple menu selection program that asks the user for a menu choice (integer value). If the user enters 1, the string Do this is displayed. If the user enters 2 or 3, the string Do that is displayed. If any other integer is entered, Do the other is displayed. You must use a switch statement in your answer. Use the structure below as a template for your answer: public class Menu int choice = Keyboard.readInt("Enter your choice (any integer)"); 2
switch (choice) case 1: System.out.println("Do this"); break; case 2: case 3: System.out.println("Do that"); break; default: System.out.println("Do the other"); [8] Question 3. [5 marks] Write a Java class that uses for loops to produce the following pattern 3456 345 34 3 Use the structure below as a template for your answer: public class Pattern1 for (int i = 3; i >= 0; i--) for (int j = 3; j <= 3 + i; j++) System.out.print(j); 1/2 System.out.println(); 1/2 3
[5] Question 4. [5 marks] Write a Java class that uses for loops to produce the following pattern ++++@ +++@@ ++@@@ +@@@@ @@@@@ Use the structure below as a template for your answer: public class Pattern2 for (int i = 0; i < 5; i++) for (int j = 5-i; j > 1; j--) System.out.print('+'); for (int j = 0; j <= i; j++) System.out.print('@'); [5] 4
Question 5. [11 marks] The code below is intended to find the floating point average of a list of integer numbers terminated by the user entering a "0" value, but it contains some errors: public class TestErrors int avg, num, count; // statement to change in (b) while(num!= 0) num = Keyboard.readInt("Enter next number: "); count++; avg = avg + num; avg = avg/count; System.out.println("The average is: " + avg); (a) Will this program compile? Give reasons for your answer. NO - avg, num, count not initialized will not compile (b) if you change the following statement in the program above [1] int avg, num, count; // statement to change in (b) to int avg = 0, num = 0, count = 0; // changed statement What kinds of error(s) occur during execution? Explain your answer. Logic errors e.g. num tested before it is read in average is an integer not declare as float Run-time error Divide by 0 [5] 5
(c) Rewrite the code so that it will execute correctly. public class TestErrors float avg=0f; int count=0; int num = Keyboard.readInt("Enter next number: "); while(num!= 0) count++; avg = avg + num; num = Keyboard.readInt("Enter next number: "); if (count > 0) avg = avg/count; System.out.println("The average is: " + avg); else System.out.println("Could not calculate average - no numbers entered"); [5] 6
Question 6. [ 6 marks] You are provided with the following program which is intended to help children learn mental addition public class MentalAdd int response, guessnum = 1, op1 = 8, op2 = 7; System.out.println("What is " + op1 + " plus " + op2 + "?"); response = Keyboard.readInt("Enter guess number " + guessnum + ": "); while(response!= op1 + op2) guessnum++; response = Keyboard.readInt("Enter guess number " + guessnum + ": "); System.out.println("Correct!"); Rewrite this code to: (a) use a do-while rather than a while loop and (b) stop on the first correct guess or after five incorrect guesses (in which case the program should display "Incorrect" and also provide the correct answer (op1 + op2)). public class MentalAdd int response, guessnum = 1, op1 = 8, op2 = 7; System.out.println("What is " + op1 + " plus " + op2 + "?"); do response = Keyboard.readInt("Enter guess number " + guessnum + ": "); guessnum++; while((response!= op1 + op2) && (guessnum <= 5)) ; 7
if (response == op1 + op2) System.out.println("Correct!"); else System.out.println("Incorrect!"); System.out.println("Correct Answer is : " + (op1 + op2)); 8