August 2000
JP219 : JAVA PROGRAMMING

QUESTION 2

Total Marks: 15 Marks

Click here to access other questions

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

(a) Explain what is meant by a Java applet, and how it differs from a Java
application. [3 marks]
(a) • An applet is a program which runs in a web browser or similar; (1 mark)
• An application runs stand-alone; (1 mark)
[2 marks]

(b) Declare a class Scribble, such that it can be run as an applet. Your class should include the protected int members X1 and Y1. [3 marks]
(b) public class Scribble extends Applet {
protected int X1;
protected int X2;
}
• Class structure; (1 mark)
• Deriving from Applet; (1 mark)
• Declaring the protected members; (1 mark)
[3 marks]

(c) A Graphics object is the simplest way to do drawing in Java. It contains the method drawString, which takes three parameters, a string, an x co-ordinate and a y coordinate.
Write a method for your Scribble class which initialises the applet, and causes the string “Hello World” to be written at coordinates (20, 30). [4 marks]
(c) public void init() {
Graphics g= new Graphics;
g.drawString("Hello World", 20, 30);
}
• Naming the method init; (1 mark)
• Creating a graphics object; (1 mark)
• Invoking the drawString method; (1 mark)
• With the correct parameters; (1 mark)
[4 marks]

(d) Write the HTML needed for your Scribble applet to be displayed in a viewer, with dimensions 100 x 100. [3 marks]
(d) <APPLET code="Scribble.class" width= 100 height= 100>
</APPLET>
• Opening and closing the APPLET tag; (1 mark)
• Declaring the code as Scribble.class; (1 mark)
• Including the width and height; (1 mark)
[3 marks]

(e) Give the import statements necessary to complete your applet. [2 marks]
(e) • import java.lang.applet.*; (1 mark)
• import java.awt.*; (1 mark)