Sei sulla pagina 1di 9

UTM

UNtVERSm TEKK0L0G1 MALAYSIA

RESEARCH UNIVERSITY

UNIVERSITITEKNOLOGI MALAYSIA
FACULTY OF SCIENCE
FINAL EXAMINATION
SEMESTER I SESSION 2011/2012

CODE

SSP 2333

COURCE

COMPUTER PROGRAMMING

LECTURER

EN. KHALID KASMIN

PROGRAM

2 SSZ & 2 SSF

DATE

18 JANUARY 2012

DURATION

3 HOURS

INSTRUCTION : ANSWER ALL QUESTIONS

(THIS QUESTION PAPER CONSISTS OF 9 PRINTED PAGES INCLUDING THIS PAGE)

SSP 2333

Name
Course

Instructions:
a) Answer all questions.

Question 1.
a) Write on the following blank lines two Java keywords for doing
repetitive or iterative computation.
[ 2 marks ]
i) _________________________ ii)
b) Write on the following blank line a Java declaration statement for
creating an array of real numbers that can contain 11 elements.
[ 2 marks ]

c) Write on the following blank line a Java statements for declaring


and assigning elements of the following matrix to an array.
[ 3 marks ]
2.3 3.1 0.5
0.7 1.4 1.1

d) Write down the Java print statement for printing to screen the
values of the following three variables:
[ 3 marks ]
int i=0;
double x;
String a="yes"

[-1-]

SSP 2333

Question 2.
a)

Which of the following code represent the declaration and


assignment of 2 dimensional array shown in Table 1?

a
[0]
[1]

[0]
5
3

Table 1
[1]
4
-5

[2]
6
7

[3]
10
9

(i) int[][] a = new int[][] { {5,4,6,10},{3,-5, 7,9}}j


(ii) int[][] a = {{5,3},{4,-5},{6,7},{10,9}>;
(iii) int[][] a = {{5,4 ,6,10},{3,-5,7,9}>;
(iv) int[][] a = new int[2][4];
a[0][0]=5; a[0][l]=4; a[0][2]=6; a[0][3]=10;
a[1][0]=3; a[1][1]=-5; a[l][2]=7; a[l][3]=9;
(v) int[][] a = {5,4,6,10,3,-5,7,9};

A)
B)
C)
D)
b)

(i), (ii), and (iv)


(i), (iii), and (iv)
(iii), and (iv)
All of the above

Write a java program


to find the maximum and minimum integer values
stored in the two dimensional array shown in Table 1.
(ii) Determine the location in the array of both numbers.

(i)

The output of the program should be as follows:


The maximum number is 10 located at row 0 column 3
The minimum number is -5 located at row 1 column 1

[-2-]

SSP 2333

Question 3.
a)

Write the rule for multiplying the following two matrices, A


and B in terms of the elements of the matrices, a,y and b,y.

.B=

[2 Marks]
b)

Write a Java program that calculates the multiplication of the


two matrices of part 3(a).
Your program should have
(i)

declaration and values assignment,

(ii)

the multiplication codes,

(iii) result display codes, and


(iv)

a 2x2 matrix display Java method.

Make a Java method in your matrix multiplication class that


print properly a 2x2 single matrix. Call that matrix display
method from the main() class to display matrix A, matrix B,
and the matrix result of the multiplication of A and B.
[8 Marks]

Question 4.
a)

When you just login to a linux server as a user, what is the


working directory know as?

b)

[2 marks]

You want to organize all your programming work in a single


directory called "javaprog", so, what is the command line
command

for

creating that directory, and

what is the

command to get into that directory?


[3 Marks]

[-3-]

SSP 2333

c)

Assume you are already in the directory "javaprog" and you


want to change its name to "jprog", write down steps and the
commands to achieve that.
[5 Marks]

Question 5.
There are 5 syntax errors in the following java program. Find
those errors and suggest the corrections.
public class maxmin {
public static void main(String args[]) {
doublet] num = new double[10];
int maxnum, minnum;
int j;
num[l]=-2.5; num[2]=0.0;num[3]=1.5; num[4]=7.5;
num[5]=9.2;
num[6]=6.6; num[7]=-10.5; num[8]=20.1; num[9]=3.4;
num[10]=8.3;
maxnum = 0.0;
minnum = 0.0;
fr(j=l; j<=10; j++); {
if (num[j] > maxnum) {
maxnum = num[j];

}
if (num[j] < minnum) {
minnum = num[j];

>
System.out.printf("Number + j + " is " + num[j]);
System.out.printf("Biggest number is %4.If\n",maxnum);
System.out.printf("Smallest number is %4.If\n",minnum);

}
>

[-4-]

SSP 2333

Question 6.
a)

Write a Java program for converting a binary number


"11011010" to hexadecimal form. Use the appropriate data
type and variables to store the result of the conversion.

b)

What will be stored in a, b, c and d after the following Java


code is executed?

int a = 1 | 2
int

= ((1

| 2)

3 i 5;
* 3) & 5;

int c = l | ( 2 * (3 & 5) ) ;
int

c)

d = 202;

Write the output of the following java program;


public class BitMask {
public static void main(String[] args) {
int bitmask = OxOOOF;
int val = 0x2222;
System.out.println(val & bitmask);
}
}

[-5-]

SSP 2333

Question 7.
a)

Consider the following code snippet

String StrMassOfObject;
double mO;
StrMassOfObject = jTextField2.getText();

Write down the assignment statement for converting the


String value of variable StrMassOfObject to double value
in variable mO.
[5 Marks]

b)

Assume that you have a jLabel Swing control named jLabell of


your Java GUI program made using NetBeans. Which of the
following code do you need to type into jLabell text property
in order to display Xi miS2 ?
A) <html>x<sub>l</sub> ms<sup>-2</sup>
B) sub<xl> sup<ms-2>
C) <html>x<sup>l</sup> ms<sub>-2</sub>
D) <htmlxsub>xl</sub> <sup>ms-2</sup>

[-6-]

SSP 2333

Question 8.
items

1.5

2.2

3.0

3.9

5.3

6.1

6.8

2.5

4.2

15.9

8.2

9.9

12.5

13.7

Table 2

a)

Using if () and for () , write a Java code to calculate the summation,


!'=7

(x.f

S = ^ for yf<10

=i y.-

for ordered pairs given in Table 2. Note that the term (x j ) 2 ly j is


included in the summation only if y, <io .
[6 Marks]

b)

Using if () and While () , rewrite your Java code for calculation of


summation of Question 8 (a) above.
[4 Marks]

Question 9.
The position of an object, m is described by the following equation
of motion:
y(t)=y0+vt+jat2 ,

where yo is the initial position, Vo is the initial velocity and a is the


acceleration. Assume that a = -9.8 ms'2, y0=0.5 m and v0=2.0 ms1.
Write a Java program that prints values, fit to plotted by Gnuplot, of
a) y and t of object m starting from t = 0.1 s until y reaches zero
using incremental time step of At = 0.0001 s,
b) the maximum value of y (yma*) and the time (t m a x ) it happened .
[10 Marks]
[-7-]

SSP 2333

Question 10.
import j ava.awt.*;
import javax.swing.*;
import java.applet.Applet;
public class ContohApplet extends JApplet {
JButton btnForce, btnMass, btnAcce, btnClear;
JTextField txtForce, txtMass;
JLabel

lblForce, lblMass;

JPanel

northPanel, centerPanel,southPanel;

public void init() {


JButton btnForce = new JButton("Force");
JButton btnMass = new JButton("Mass M);
JButton btnAccel = new JButton("Accel");
JButton btnClear = new JButton(" Clear ");
JTextField txtForce = new JTextField(15);
JTextField txtMass = new JTextField(15);
JLabel

lblForce = new JLabel( Force (in N) " );

JLabel

lblMass = new JLabel(" Mass (in kg) );

northPanel = new JPanel();


northPanel.setLayout (new FlowLayout());
northPanel.add(btnForce);
northPanel.add(btnMass);
northPanel.add(btnAccel);
centerPanel = new JPanel();
centerPanel.setLayout (new GridLayout(2, 2));
centerPanel.add(lbl_Force); centerPanel.add(txt_Force);
centerPanel.add(lbl_Mass); centerPanel.add(txt_Mass);
southPanel = new JPanel();
southPanel.setLayout (new FlowLayout());
southPanel.add(btn_clear);
Container

content

content.setLayout

getContentPane();

(new

BorderLayout());

add(northPanel,

BorderLayout.NORTH);

add(centerPanel,

BorderLayout.CENTER);

add(southPanel, BorderLayout.SOUTH);

}
>______________

a)

List the layout manager used in this applet code.


[3 marks]

b)

Write the code for attaching event listener to btnClear.


[3 marks]

c)

Sketch how would the components be layed out on the applet.


[4 Marks]
[-8-]

Potrebbero piacerti anche