Exam DIS&JAN 2014

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

diakiimt..

UNIVERSITI
crU* KEBANGSAAN
06 MALAYSIA
4
The National University
of Malaysia

PEPERIKSAAN AKHIR
SEMESTER I SESI AKADEMIK 2013-2014
IJAZAH SARJANAMUDA DENGAN ICEPUJIAN

DISEMBER 2013 / JAN. 2014 MASA : 2 JAM 30 MINIT

KOD KURSUS TTTK2934

TAJUK KURSUS : PENGATURCARAAN BERORENTASIKAN OBJEK


(OBJECT ORIENTED PROGRAMMING)

ARAHAN 1. Jawab SEMUA soalan dalam buku jawapan yang diedarkan.


Answer ALL questions in the answer booklet provided.

2. Jumlah markah keseluruhan kertas ini 100 markah.


The total marks for this question paper in 100 mark.

3. Kertas soalan tidak dibenarkan dibawa keluar daripada Dewan


Peperiksaan.
This question paper is not allowed to be taken out of the
Examination Hall.

Kertas soalan ini mengandungi 9 muka surat bercetak, tidak termasuk muka surat ini

MS ISO 9001 REG NO. AR 2845


TTTK2934

1. a. Define each of the following terms:


i. object
ii. class
iii. method overloading
(6 marks)

b. Below is a description of a vault:


A vault is either locked or unlocked. Content can be put into or taken
out of a vault only if it is unlocked. The correct combination for a vault
must be given to unlock it.
A class definition representing vaults is given below:
class Vault {
protected String combination;
protected boolean unlocked;
protected String content;
public Vault(String comb) {
combination = comb;
unlocked = false;
content = null;
}
public Vault() {
combination = "0000";
unlocked = false;
content = null;
}
public boolean unlock(String comb) {
//
/ / Method intentionally not complete. Refer question 1. b. iii
//
}
public void lock() {
unlocked = false;
}
public void putln(String str) {
if (unlocked && content == null)
content = str;
}
public String takeOut() {
if (unlocked)
return content;
return null;
}

i. Identify the following in the given class definition.


• two examples of object references
• two examples of constructors
• one example of method overloading

(5 marks)

1
TTTK2934

ii. Explain how the method vault ( string ) relates to object creation.
(2 marks)

iii. The behaviour of a vault object upon receiving an unlock (comb )


message is described as follows:
The object unlocks unless it is already unlocked or the value
of comb is not the same as its combination. Returns true if
unlocking is done, otherwise it returns false.
Based on the above description, write the body of the unlock method.
(5 marks)

iv. Consider the pseudocode in Figure 1:


read strings strl and str2
form a combination string based on strl and str2
create a Vault object using the created combination
put the string "secret" into the Vault object

Figure 1

The second step in the pseudocode forms a combination string from


strings s trl and str2 inputted from the previous step. A combination
string is an 8-digit string with the following properties:
• its first four characters are the last four characters of s trl
• its last four characters are the first four characters of s t r2.
As an example, the combination string created from "01234" and
"567890" is "12345678".
a. Identify the class(es) and method(s) from the Java class library you
could use for generating combination strings as described above.
(3 marks)
b. Based on (a), write an algorithm for generating a combination
string from strings s trl and s tr2.
(2 marks)
c. Translate the pseudocode in Figure 1 into Java statements.
(7 marks)

2
TTTK2934

2. a. Briefly explain each of the following terms:


i. inheritance
ii. method overriding
(4 marks)

b. Consider the following class definition:


class MyApplet extends JApplet {
private Vehicle vl, v2, current;
public void init() {
vl = new Car("WMB9438"); // Line 4
v2 = new Bus("WKK1417");
current = v1;
}
public void start() {
if (current == v1)
current = v2;
else
current = v1;
super. start();
}
public void paint(Graphics g) {
current.display(g); // Line 16
}

}
Referring to the class definition, state whether each of the following
statements is true or false. Give an explanation if it is false.
i. An inheritance relationship exists between classes MyApplet and
JApplet.
ii. The statement at Line 4 is valid if the Car class is a descendant of the
Vehicle class.
iii. The instance variable current at Line 16 contains a reference to a
C ar object.
iv. After a MyApplet object executes the init method, its instance
variable current contains a reference to a Vehicle object.
v. The start method in the class definition overrides one of the methods
inherited from JApplet.
vi. The MyApplet class has no constructors.
(8 marks)

3
TTTK2934

c. A photo journal is one way of documenting chronological memories of an


individual's events. Supposing you are developing a personal photo journal
software. A photo journal is described as follows:
•A photo journal is a collection pages.
•Every page has a background colour.
•There are three types of pages:
- A photo page contains an image, a caption and a
description about that image.
- A divider page contains a title and is used to divide the
pages of a photo journal into sections.
- A list of contents page lists the sections and pages in a
photo journal.
i. Besides add new page, suggest three other possible operations of a
photo journal.
(3 marks)
ii. Consider the skeletal class definitions given below:
class Page f
// Represents a page in a photo journal

class PhotoPage extends Page {


// Represents a photo page

class Divider extends Page f


// Represents a divider page

class List_of_Contents extends Page {


// Represents a list of contents page

Define one relevant attribute for every class listed above based on the
description of a photo journal given earlier.
(2 marks)

4
TTTK2934

iii. An extract of the PhotoJournal class definition is given below:

class PhotoJournal f
private static int maxPages = 20;
private Page[] pages = new Page[maxPages];

public void list content() f


r
for (int i=0; i < maxPages; i++)
if (pages[i] != null)
pages[i].display();

The body of the l is tcontent method displays a thumbnail for


every page of a photo journal. For flexibility, Java's polymorphism
feature is used when displaying a thumbnail based on the type of page.
Describe how display methods should be defined in the inheritance
hierarchy given in (ii).

(3 marks)

3. a. Name a Java Swing component that best fits each of the following
description:

i. A component that looks like a button and that, when pressed, brings up
a menu of items for the user to choose from.

ii. A component that displays text, but that does not react to user clicks.

iii. A component that allow user to select a value from a range of values.

(3 marks)

b. Name the most suitable layout manager to display the following interfaces.

i.
r
t451 L. ,K;zZi

2 3

1 3

5
TTTK2934

} Layout

2 3

ggioreigeweelerearseenetrassasemexamenerammore

(2 marks)

c. Write Java statement(s) to create the following GUI components.

i. A group of three radio buttons with the label rain, sunshine and
rainbow.

ii. A JList component with items PCC, Imagine Cup, Robot Soccer,
Media Creative and CESMED.

(5 marks)

d. Given the diagram in Figure 2:

TITLE
Subtitle

Boxl Box2 Box3

Box4 Box5 Box6

Box7 Box8 Box9

Large Box

Figure 2

i. Using Figure 2, illustrate the JPanel(s) and respective layout


manager needed to display the interface.

(5 marks)

6
TTTK2934

ii. Using the following component declaration, write a program to display


the interface in Figure 3.

JLabel title, subtitle;


JButton btn = new JButton [9];
String [] btnStr = {
"TK1114 Computer Programming",
"TR1333 Discrete Mathematics",
"TR1713 Statistics and Probability",
"TT1964 Database",
"TK2324 Object Oriented Programming",
"TT2013 System Thinking and
Philosophy",
"TT3013 Computer, Ethics and Social",
"TK3043 Algorithm Design and
Analysis",
"TK4086 Project"};
JTextArea textArea;
JPanel pl, p2, p3;

Course Registration
Millillialli - t MEMO'
Fakulti Teknologi dan Sains Maklumat
Bachelor of Computer Science Programme

TK1114 Computer Programming TR1333 Discrete Mathematics TR1713 Statistics and Probability

TT1064 Database TK2324 Object Oriented Programming TT2013 System Thinking and Philoso...

TT3013 Computer, Ethics and Social TK3043 Algonthm Design and Analysis TK4086 Project

Figure 3

(10 marks)

4. a. Name suitable Java Swing components that can be used with each of the
following Listener class:

i. ActionListener (Name one component)

ii.I temListener (Name two components)

iii.ChangeListener (Name only one component)

(4 marks)

7
TTTK2934

b. Event handling has three main components namely events, event source and
listeners. Describe how events are handled in Java programs.

(4 marks)

c. Draw Something is a mobile app developed by Omgpop. It won a Flurry App


Spotlight Award in 2012. Fifty days after its release, it had been downloaded
50 million times. In March 2012, both Draw Something and Omgpop were
bought by the game giant Zynga for USD180 million.

Draw Something consists of two section which is the drawing section and the
word guessing section. Figure 4 below shows the word guessing section. The
picture to guess is shown on the upper part of the interface (Box A). At the
lower part of the interface (Box B), the guesser is given a number of blank
spaces representing the number of letters in the word, and a selection of
scrambled letters, which include all the letters of the word plus some more
letters which are not in the word. The guesser gets unlimited tries and time to
use these letters and attempt enter the guess word.

W DrawSomething,

B
1353121
El NI1311113
Figure 4

i. Define all the Java Swing components most suitable used in Box B.

ii. Write a program to display the components in Box B.

(7 marks)

8
TTTK2934

d. Extend the program in (c) to handle the user interaction. When the
guesser choosed a correct letter, the letter will be deleted and
displayed in the correct blank space. When all letters are correctly
guessed, your program should display the message "CORRECT"
as in Figure 5.

(10 marks)

Figure 5

"SELAMAT MAJU JAYA"

You might also like