You are on page 1of 7

FACULTY OF SCIENCE AND TECHNOLOGY

DEPARTMENT OF COMPUTER SCIENCE


& MATHEMATICS
LEVEL FOUR
ACADEMIC YEAR 2013/14
JUNE ASSESSMENT PERIOD

Module CO4025
Introduction to Programming

Date 2014

Time

Length 1 HOURS

INSTRUCTIONS TO CANDIDATES:

Answer any FOUR from FIVE questions

Answer ANY FOUR from FIVE questions


Start the answer to each question on a separate page on your examination
book
Question 1

a) Java is an object-oriented programming language. Explain the meaning of the


following key features used in object oriented programming:
a. Class
b. Attribute
c. Constructor
d. Method
e. Object

[10 Marks]

b) The following fragment of code attempts to convert a String, value, into a

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);

c) Using examples to illustrate, explain why we use import statements at the


head of our Java source code files.

[5 Marks]

d) Comment on the differences between using swing components to build a GUI


and using the NetBeans Form facility in Design mode.

[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

Use either a do or a while statement to output the numbers 1 to 10


to the screen.
[5 Marks]

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]

b It is common to handle input data as a string in Java. If necessary, the data


can then be converted into numerical format. Explain the effects of the
following fragment of code:

[5 Marks]
String number = "4";
System.out.println("Number = " + number + " and its value
trebled = " + (Integer.parseInt(number) * 3));

e) Explain what happens when the following fragment of graphics code is


executed:

[10 Marks]

Graphics2D g2d = (Graphics2D) g;//create graphics object g2d


int x = 200;
int y = 150;
Point2D.Double start = new Point2D.Double(x, y);
x -= 50;
Point2D.Double end = new Point2D.Double(x, y);
Line2D line = new Line2D.Double(start, end);
BasicStroke lineWidth = new BasicStroke(4);
g2d.setStroke(lineWidth);
g2d.setColor(Color.GREEN);
g2d.draw(line);

Page 2 of 5

Question 3

a Discuss the importance of testing in developing computer programs and


explain how you would test a completed program.

[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

g) Explain how the following fragment of code behaves:

[10 Marks]

int[] myArray = new int[10];


for(int i = 0; i < 10; i++) {
myArray[i] = (i + 1);
}
for(int i = 0; i < 10; i++) {
System.out.println("Contents of myArray[" + i + "] = "
+ myArray[i]);
}

Page 3 of 5

Question 4

a Variable names and comments can be used to document programs. Give

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

a The String objects contain a range of string-handling methods. Explain the


meaning of each of the following operations:

[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

You might also like