2000SCExamAnswers

2000SCExamAnswers - 415.101 ST THE UNIVERSITY OF AUCKLAND EXAMINATION FOR BA BSc ETC 2000 COMPUTER SCIENCE Principles of Programming(Time Allowed

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
415.101 ST CONTINUED THE UNIVERSITY OF AUCKLAND EXAMINATION FOR BA BSc ETC 2000 COMPUTER SCIENCE Principles of Programming (Time Allowed: TWO hours) Surname Forenames Student ID Login (UPI) NOTE: Attempt ALL questions. Write your answers in the space provided. There is space at the back for answers that overflow the allotted space Calculators are not permitted. Section Marks Out of Question 1 5 Question 2 5 Question 3 11 Question 4 5 Question 5 15 Question 6 10 Question 7 10 Question 8 12 Question 9 7 Question 10 15 Question 11 5 Total 100
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Question/Answer Sheet - 2 - 415.101ST Surname: . ............................... Forenames: . ............................... CONTINUED Question 1 (5 marks) You need to find the logic errors in an applet that has been written to test whether a number is odd or even. The applet is supposed to look like the screen shot below when it first starts: There is a TextField with the value 1000, a Button labelled "TEST", and the String "EVEN" is painted on the screen. When the user enters a number in the TextField , and presses the button labelled "TEST", the String which is painted on the screen should be either "EVEN" or "ODD" depending on whether the number is even or odd. For example, if the user enters the value 13, the applet should display the word "ODD" when the button is pressed: and if the user enters the value 26, the applet should display the word "EVEN": Unfortunately, the applet does not work correctly. The code compiles without error and the applet looks correct when it is first loaded, however, it does not work when the "TEST" button is pressed. There is one error in the init() method which causes a NullPointerException to occur when the "TEST" button is pressed, and there are two other errors in the actionPerformed() method. Locate and correct the 3 errors in the Applet code given below. For each error, cross out the line
Background image of page 2
Question/Answer Sheet - 3 - 415.101ST Surname: . ............................... Forenames: . ............................... CONTINUED of code containing the error, and rewrite it correctly below the original line of code. import java.awt.*; import java.applet.*; import java.awt.event.*; public class Question1 extends Applet implements ActionListener { TextField input; Button test; boolean isEven; public void init() { TextField input = new TextField("1000"); input = new TextField("1000"); isEven = true; test = new Button("TEST"); test.addActionListener(this); add(input); add(test); } public void actionPerformed(ActionEvent e) { int value = Integer.parseInt(input.getText()); if (value / 2 == 0) if (value % 2 == 0) isEven = true; else isEven = true; isEven = false; repaint(); }
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Question/Answer Sheet - 4 - 415.101ST Surname: . ............................... Forenames: . ...............................
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/01/2012 for the course COMPSCI 101 taught by Professor Adrianaferraro during the Summer '12 term at Auckland.

Page1 / 35

2000SCExamAnswers - 415.101 ST THE UNIVERSITY OF AUCKLAND EXAMINATION FOR BA BSc ETC 2000 COMPUTER SCIENCE Principles of Programming(Time Allowed

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online