December 1999
JP219 : JAVA PROGRAMMING

QUESTION 4

Total Marks: 15 Marks

Click here to access other questions

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

 

A Frame is a derived class of the AWT component Window .Frames inherit from the Component class,and therefore components can be added using the add method.
A Button is a basic AWT component,which has a basic push to activate interface,with a title that informs the user of its use.A button can be placed in a frame.
A CheckBox is also a basic AWT component and is a simple on/off push to activate switch, is a member of a CheckBoxGroup ,has a title and is placed in a frame.

 

 
(a)

 Define a class,called MyFrame which implements a frame by extending the Frame superclass.Your class should contain a constructor which creates the frame with a named title bar.In addition to this,include in your declaration a protected button b a protected checkbox group cg and a protected checkbox c .
A sample definition of MyFrame follows:
public class MyFrame extends Frame {

protected Button b;
protected CheckboxGroup cg;
protected CheckBox c;

public MyFrame(String title){

super(title);

}

}

And the following marking scheme should be used:
•A correct class definition which extends Frame .(1 mark)
•A correct Button declaration;(1 mark)
•A correct CheckboxGroup and CheckBox declaration;(1 mark)
•A correct constructor signature,with a String parameter;(1 mark)
•Calling the superclass constructor with the String parameter.(1 mark)

 

[5]
(b)

 Write a method,called MyButton ,the signature of which is given below,which is a member of your MyFrame class.The method should take two String parameters x and y ,and allocate the button b ,such that it is added to your frame,and has title x and the action command y .Your method should also ensure that the button is set up with the frames action listener component.
void MyButton(String x,String y);
A sample definition of MyButton follows:

void MyButton(String x,String y)
{

b =new Button(x);
b.setActionCommand(y);
b.addActionListener(this);
this.add(b);

}

And the following marking scheme should be used:
•Allocating the button with the new operator;(1 mark)
•Constructing the button with the Button constructor and the correct string•Setting the action of the button to the string y ;(1 mark)
•Setting the listener of the button to the frame components listener;(1 mark)
•Adding the button to the frame.(1 mark)

 

[5]

(c)

 Write a method,called MyCheckBox ,the signature of which is given below,which is a member of your MyFrame class.The method should take a single String parameter x , and add to your frame f the check box c ,such that it has title x ,initial value true and is a member of the check box group cg .
void MyCheckBox(String x);
A sample definition of MyCheckBox follows:
void MyCheckBox(String x)
{

c =new Checkbox(x,true,cg);
this.add(c);

}

And the following marking scheme should be used:
•Allocating the checkbox with the new operator;(1 mark)
•Using the string x as the title;(1 mark)
•Setting initial value to true ;(1 mark)
•Adding the checkbox to the group cg ;(1 mark)
•Adding the checkbox to the frame.(1 mark)

[5]