COMP S203F – Test 1
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit
COMP S203F – Test 1 (Sample)
This sample is aimed primarily to illustrate the overall format and some types of questions. The mark allocations and level of difficulties may be varied in the test. |
You should attempt ALL questions of the paper. The total mark of the paper is 50. You must include
all your answers in the provided answer book. All submitted Java programs should be compiled successfully, otherwise, you will be penalized at 10% (absolute marks).
Question 1 [10 marks]
Circle "True" if the following statement about Java is true and circle "False" otherwise.
[10]
(a)
Define an interface by using reserved word interface instead of using class.
True False
(b)
Two methods of a class having the same name must have different number of parameters.
True False
(c)
An abstract class cannot have an abstract method.
True False
(d)
Package javax.swing contains most useful GUI items like frame, panel, button, menu.
True False
(e)
JFrame use BorderLayout by default.
True False
Question 2 [20 marks]
(a)
(i)
Write a class Product2 with an attribute price (type double).
(ii)
Write the getter and setter methods of the attribute. The names of getter and setter methods should be getWord() and setWord() respectively if the attribute name is word.
(iii)
Write a constructor with a single parameter price and a suitable statement to initialize the attribute using the value of the parameter.
(iv)
In Product2, add a class method averagePrice(Product2[] products) which returns the average price of the products in the parameter. You can assume each element of the array refers to a properly initialized Product2 object.
(b)
(i)
After the end of the class Product2, add a non-public class DiscountedProduct, which is a subclass of Product2 with a doubletype attribute discountPercentage. In the non-public class, there is no "public" before the word "class".
(ii)
Write the getter/setter method of the attribute and the constructor DiscountedProduct(double price, double discountPercentage) which suitably initializes the attributes.
(iii)
In DiscountedProduct, override the getPrice() method so that the discounted price (= price * (1- discountPercentage/100)) is returned.
(c)
(i)
After the end of the class DiscountedProduct, add another non-public class TenDollarProduct, which is a subclass of Product2 without any attribute.
(ii)
Override the getPrice() method to return 10, ignoring the attribute price of its superclass. Notice the class method averagePrice() should return a correct value without any change after a new subclass is added.
(d)
Illustrate with a diagram for the class hierarchy corresponding to the classes specified in this question. Attributes and methods should be included appropriately.
Hint: you may like to write the main() method to test the correctness of your class before submission.
[20]
Question 3 [20 marks]
Write a class Conversion containing the following methods:
(a)
constructor : which builds the frame shown below. The frame consists of a text field for inputting a SGD amount, a label with 10 spaces for an equivalent SGD amount in HKD, and a button to start the calculation. Declare any necessary attributes in the class and add appropriate action listeners for future use. Copy the class, including import statement(s), as the answers to this part.
(b)
actionPerformed() : which performs the calculation and puts the result on the label when the button is pressed. You can assume one SGD is equivalent to 5.7 HKD. You can assume a valid real number is entered in the textfield. Copy the method as the answers to this part.
(c)
main() : which creates a Conversion object and sets it visible for testing. Copy the method as the answers to this part.
[20]
Partial Method list:
JButton |
JButton(s), setText(s) |
JCheckBox |
isSelected(), JCheckBox(s), setSelected() |
JFrame |
getContentPane(), JFrame(s), pack(), setJMenuBar(mb), setLayout(lm), setSize(i,j), setVisible(bo), show() |
JLabel |
JLabel(s) |
JMenuBar |
add(m), JMenuBar() |
JMenu |
add(mi), addSeparator(), JMenu(s) |
JMenuItem |
JMenuItem(s) |
JOptionPane |
showMessageDialog(o,s), showInputDialog(o,s), showConfirmDialog(o,s) |
JPanel |
add(co), add(co, i) |
JRadioButton |
isSelected(), JRadioButton(s), setSelected() |
JTextField |
JTextField(i) |
JTextArea |
JTextArea(i,j) |
where b: byte, b[]: byte array, bo: boolean, c:char, co: GUI component, f: File, i,j: int, k: key(Object),
lm: layout manager, m: menu, mb: menu bar, mi: menu item, o: Object, s: String
2022-03-31