Section A: Structured Questions (80 Marks) INSTRUCTION: Answer ALL Questions in The Space Provided
Section A: Structured Questions (80 Marks) INSTRUCTION: Answer ALL Questions in The Space Provided
1.
(2 marks)
Answer:
Procedural programming tries to solve the problem by implementing
procedures (methods) to solve the main problem. [1 mark]
Object-oriented programming is a collection of objects that interact with
each other to solve the main problem. [1 mark]
1.
What is the relationship between classes and objects? Give ONE (1) example.
2.
(2 marks)
Answer:
An object is created from class. [1 mark]
For example, a class of Dog with a name attribute can have Spike (a dog
object) and Brufus (another dog object). [1 mark]
1.
What is a method? Do all objects of the same class have the same methods?
2.
(2 marks)
Answer:
Method is the action that an object can take, also called behaviour. [1 mark]
Yes, all objects of the same class have the same methods. [1 mark]
a.
State the package that provides the following classes and give ONE (1)
example of a method from each class.
b.
Predefined Classes Java Packages Methods
Scanner
String
Random
Math
(4 marks)
Answer: java.util e.g: nextInt(), nextDouble()
java.lang e.g: charAt(), length()
java.util e.g: nextInt(), nextDouble()
java.lang e.g: pow(), sqrt()
[½ mark for each ]
b) Write a Java statement to import ALL classes from one of the package.
(1 mark)
Answer: import java.util.*;
a.
(2 marks)
public PressureCooker () {
}
[1M for public PressureCooker, 1M for bracket & curly braces]
a.
(2 marks)
PressureCooker pc1 = new PressureCooker();
[1M] [1M]
a.
Write another constructor for this class by including ALL data/variables
stated.
b.
(7 marks)
a.
Write a Java statement to create an object using the constructor you wrote in
Question 5 (c).
b.
(3 marks)
a.
(3 marks)
a.
Write a complete method call for the method you wrote in Question 5 (e).
b.
(1 mark)
double totalPrice = PressureCooker.calculatePrice(3);
[½ M] [½ M]
6. Explain the ways in which inheritance promotes software reuse and saves time during
program development.
(3 marks)
Answer:
Software reuse lets us write less code when developing the new subclass and
avoid duplication of common functionality between several classes by
building a class inheritance hierarchy. [1.5 marks]
With inheritance, the developer can save time during program development
by basing new classes on existing proven and debugged high-quality
software. [1.5 marks]
package accessmodifier
package accessmodifier;
Answer:
Protected access modifier can only be accessible through
inheritance. [2 marks]
a) Draw a UML class diagram for A, B and C classes, showing the inheritance
relationship among them.
(3 marks)
Answer:
b) What will be the output if we run the main method of Test class?
(3 marks)
Answer: ABC 1 mark for each correct output, -1 for incorrect output
a) What are the methods that should be implemented in the Rectangle class?
(3 marks)
Answer:
draw() , calculateArea(), calculatePerimeter()
1 mark for each correct method
10. What exception will be thrown when the following code fragment is executed?
(4 marks)
Answer: ACFG minus 1 for each wrong answer (letter)
12. Given the following incomplete fragment of code, fill in the blanks with TWO (2) if
statements. Each can throw an Exception object if the value of a variable named m
is
When the exception is caught either of the following message will be displayed.
(4 marks)
Answer:
if (m < 0)
throw new Exception (m + " is a negative value");
if (m > 100)
throw new Exception(m + " is too large");
[2 marks for each correct if statement]
Answer:JFrame
(1 mark)
c) What is the width and height of the frame?
Answer:Width = 400 height = 300
(1 mark)
d) What is the effect to the output of this program if the frame.setVisible(true)
statement is removed?
Answer:The frame will not be shown or will not be visible.
(1 mark)
14. The following incomplete program is supposed to write "Hello world" to a file
named "myFile.txt". Fill in the blanks to complete the code.
(6 marks)
Answers:
a.
java.io.*
b.
c.
IOException
d.
e.
new File("myFile.txt")
f.
g.
FileWriter outFileStream
h.
i.
outStream.println("Hello world")
l.
15. Complete the code to read the "Hello world" string from the file in Question
14.
Answers:
a.
bufReader.readLine()[1M]
d.
1.
The GUI, together with the names of its GUI components, are shown below:
Based on the provided information above, answer all of the following questions:
a.
// TODO: add calcBusinessCustomer() method code here:
public double calcBusinessCustomer() { //1M
double charge = 0.0; //0.5M
if (numOfConnections <= 10) { //0.5M
charge = 75.00 + (20.00 * numOfPremChannels); //1M
} else { //0.5M
charge = 75.00 +(numOfConnections - 10)*5.00 +(20.00 *
numOfPremChannels); //1.5M
}
return charge; //0.5M
}
public String toString() {
String billInfo = "";
double charge = 0.0;
if (custType.equals("Residential")) {
charge = calcResidentialCustomer();
billInfo = "Account Number: " + acctNum + "\n"
+ "Number of premium channels =
"+numOfPremChannels+ "\n"
+ "Amount Due = RM"+charge;
} else {
charge = calcBusinessCustomer();
billInfo = "Account Number: " + acctNum + "\n"
+ "Number of service connections =
"+numOfConnections+ "\n"
+ "Number of premium channels = "+numOfPremChannels+
"\n"
+ "Amount Due = RM"+charge;
}
return billInfo;
}
}
a.
(8 marks)
private void
displayBTNActionPerformed(java.awt.event.ActionEvent evt) {
// TODO: add your code here:
String acctNum = acctNumTF.getText(); //0.5M
int numOfPremChannels = Integer.parseInt(numPremTF.getText());
//1M
int numOfConnections = 1; //0.5M
String custType = "Residential"; //0.5M
if (businessRB.isSelected()) { //1M
custType = "Business"; //0.5M
numOfConnections = Integer.parseInt(numConnTF.getText());
//1M
}
CustomerBill bill =
new CustomerBill(custType, acctNum, numOfConnections,
numOfPremChannels); //2M
outputTA.setText(bill.toString()); //1M
}
END OF QUESTIONS