April 2000
JP219 : JAVA PROGRAMMING

QUESTION 3

Total Marks: 15 Marks

Click here to access other questions

SUGGESTED SOLUTIONS
Solutions and allocated marks are indicated in green.
Return to
Question 3

Do not award half marks. Do not deduct marks for trivial syntactic errors. Alternative correct answers should be given credit, unless otherwise indicated in the marking scheme.

(a)State what is meant in Java programming by an event [2marks ]
An event is
•An object (1 mark)
•Which describes something which has happened.(1 mark)
[2 marks ]

(b)State what is meant by an event source [1mark ]
An event source is the generator of an event.[1 mark ]

(c)State what is meant by an event handler [2marks ]
An event handler is
•A method which receives an event object;(1 mark)
•Anddeciphersandprocessesit.(1 mark)
[2 marks ]

(d)Assume the following de .nition of the class MyApplet
public class MyApplet extends Applet {
   private int last_x, last_y;
   // Display my applet
   public void paint(Grpahics g) {
      g.drawString(‘‘Hello World’’);
   }
}
(i)Write a basic HTML file,which could be used to display this applet,with
appropriate dimensions.[4 marks ]
(ii)Add to the class MyApplet a method which will be called automatically upon the initialisation ofthe applet,and will set the co-ordinates ofthe points last x and last y to zero.[3 marks ]
(iii)The Applet class contains the abstract method mouseDown When the user
clicks a mouse button,the system calls this method with a reference to the event
object generated,and the x and y co-ordinates ofthe mouse when the event
happened.Implement an overloading of this method in the class MyApplet
which will simply set the values of last x and last y to the values when the
event occurs,and print out the new values.[3 marks ]
(i)A sample definition ofthe HTML follows:
<APPLET code=’’MyApplet.class’’ width=100 height=100>
</APPLET>
And the following marking scheme should be used:
•Correct use of the <APPLET> and </APPLET> tags;(1 mark)
•Correctly using the code speci .er;(1 mark)
•With the file MyApplet.class (1 mark)
•Including some appropriate speci .ers for the dimensions for the applet to
be displayed.(1 mark)
[4 marks ]
(ii)A sample definition of init follows:
public void init() {
x= 0; y= 0;
}
And the following marking scheme should be used:
•Correctly naming the method init (1 mark)
•Correctly declaring it as public void (1 mark)
•Initialising the values of x and y to zero in the body of the method.(1 mark)
[3 marks ]

(iii)A sample definition of mouseDown follows:
public void mouseDown(Event e, int x, int y) {
   last_x= x;
   last_y= y;
   system.out.println(last_x + last_y);
}
And the following marking scheme should be used:
•Correctly specifying the function signature for mouseDown (1 mark)
•Correctly updating the values of last x and last y (1 mark)
•Correctly printing out the new values.(1 mark)
[3 marks ]