Professional Documents
Culture Documents
Module CO4025
Introduction to Programming
Date 2014
Time
Length 1 HOURS
INSTRUCTIONS TO CANDIDATES:
[10 Marks]
double and store the result in yValue. Explain what the try.. catch block
does and say why it has been included.
[5 Marks]
try{
yValue = Double.parseDouble(value);
} catch(NumberFormatException e1) {
JOptionPane.showMessageDialog(null, "Wrong Number Format
for Y Value\n " + e1, "Input Error", JOptionPane.ERROR_MESSAGE);
[5 Marks]
[5 Marks]
Page 1 of 5
Question 2
a Conditional and loop statements are used to control program execution. Write
short Java statements to meet the following requirements:
a
Check the value held in the integer variable age and output a
suitable message indicating classification: under-fives are classes as
infant; under-eighteens are classed as child and the remainder are
classed as adult.
[5 Marks]
[5 Marks]
String number = "4";
System.out.println("Number = " + number + " and its value
trebled = " + (Integer.parseInt(number) * 3));
[10 Marks]
Page 2 of 5
Question 3
[10 Marks]
f) The following swing components are commonly used to build graphical user
interfaces (GUI). Explain briefly what the components are and say how they
are typically used.
[5 Marks]
a. JFrame
b. JPanel
c. JLabel
d. JTextField
e. JButton
[10 Marks]
Page 3 of 5
Question 4
examples of how you document and structure your programs and explain the
significance of this.
[10 Marks]
h) Java uses a number of primitive data types. Explain what kind of data type
the following keywords define and say when they might typically be used: [5
Marks]
a. int
b. char
c. double
d. boolean
i) We often want to collect or organise data in our programs. Explain what each
of the following data structures is and give an example of when it might be
used:
a. A one-dimensional array
b. An ArrayList
c. A two-dimensional array
[10 Marks]
Page 4 of 5
Question 5
[10 Marks]
d. if(choice.equals(Quit))
e. if(string1.compareToIgnoreCase(string2) > 0)
f. if(string.charAt(i) != 0)
j) Explain how the following fragment of code behaves and explain when it
might be useful to use the switch statement:
[5 marks]
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days = 31;
break;
case 2:
days = 28;
break;
default:
days = 30;
break;
}
k) We build classes that implement the ActionListener class with the following
overall structure. Why do we build these classes and how do we use them in
GUI environments?
[10 marks]
private class OptionButtonListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
// handler code goes here
}
}
END OF PAPER
Page 5 of 5