Cohort: BCA/07B/PT - BCA/06/PT - BCNS/06/FT - BCNS/05/FT - BIS/06/FT - BIS/05/FT - BSE/05/FT - BSE/04/PT-BSE/06/FT

Download (0)

Full text

(1)

BSc (Hons) in Computer Applications,

BSc (Hons) Computer Science with Network Security,

BSc (Hons) Business Information Systems &

BSc (Hons) Software Engineering

Cohort: BCA/07B/PT - BCA/06/PT - BCNS/06/FT - BCNS/05/FT -

BIS/06/FT - BIS/05/FT - BSE/05/FT - BSE/04/PT-BSE/06/FT

Examinations for 2007 - 2008 / Semester 1

Resit Examinations for 2006/2007 - Semester 1

Resit Examinations for 2005/2006 - Semester 2

MODULE: OBJECT ORIENTED PROGRAMMING

MODULE CODE: PROG1102

Duration: 2 Hours 30 Minutes

Instructions to Candidates:

1. Answer all questions using Java as programming language.

2. Questions may be answered in any order but your answers must show the question number clearly.

3. Always start a new question on a fresh page. 4. All questions carry equal marks.

5. Total marks 100.

(2)

ANSWER ALL QUESTIONS

QUESTION 1: (25 MARKS)

(a) Examine the following piece of Java code and write down the outputs. Justify fully all your answers.

class C{

public int x = 1; public C(int i){ x += i;

} }

class D extends C{ public D(int i){ super(i); x += i; } }

class E{

public static void main(String args[ ]){ C ar[ ] = new C[2]; ar[0] = new C(1); ar[1] = new D(2); System.out.println(ar[0].x + ar[1].x); } } (5 marks) (Continued)…

(3)

Question 1 : (continued)

(b) An abstract class Person in Java has two subclasses named Employee and Student.

Write down the abstract class Person which contains the following:- (i) attributes name, address, phone number and e-mail (ii) a no-arg constructor

(iii) an abstract String method ‘toString’

(iv) set methods to assign values to the attributes

(v) get methods to return the values assigned to the attributes

(5 marks)

Write down the subclass Employee which contains the following:- (i) attributes office, salary and date-hired

(ii) a no-arg constructor

(iii) set methods to assign values for the office, salary and date-hired (iv) get methods to return the values for the office, salary and date-hired

(v) a String method which overrides the abstract method ‘toString’ to return the class name and person’s name

(5 marks)

Write down the subclass Student which contains the following:- (i) attribute type

(ii) a no-arg constructor

(iii) a set method to assign value for the attribute type (iv) a get method to return the value for the type

(v) a String method which overrides the abstract method ‘toString’ to return the class name and person’s name

(5 marks)

Now write an application to perform the following:- (i) to instantiate Employee and Student objects

(ii) to assign values to all of the Employee object’s attributes (iii) to assign values to all of the Student object’s attributes

(iv) to output the values assigned to both the Employee and Student objects

(4)

QUESTION 2: (25 MARKS)

(a) Explain with suitable examples the meaning of the following terms:- (i) method call

(ii) method overloading (iii) method overriding

(6 marks)

(b) What is the output of the following programs? Explain fully all your answers. (i) class A {

public static void main(String args[ ]){ int a[ ] = new int[5];

for(int i=0; i<5; i++) a[i] = i+1;

for(int i=0; i<5; i++)

System.out.println(a[i]); } }

(3 marks)

(ii) class B {

public static void main(String args[ ]){ int a[ ] = new int[5];

for(int i=0; i<5; i+=2) a[i] = i+1;

for(int i=0; i<5; i++)

System.out.println(a[i] % 2); } }

(3 marks)

(iii) class C {

public static void main(String args[ ]){

int t=0;

for(int i=0; i<5; i++){ for(int j=0; j<5; j++) t = t + i; } System.out.println(t); } } (3 marks) (Continued)…

(5)

Question 2 : (continued)

(c) Answer the following questions regarding an array called ‘fractions’. (i) Define a constant variable ARRAY_SIZE initialized to ten.

(ii) Declare the array with ARRAY_SIZE elements of type double and initialize the elements to 0.

(iii) Print the numbers to verify if they have been properly initialized using System.out.println.

(iv) Name the fourth element of the array.

(v) Refer to array element 4.

(vi) Assign the value 1.667 to array element 9.

(vii) Assign the value 3.333 to the seventh element of the array.

(viii) Sum all the elements of the array using a for repetition structure. Define the integer variable x as a control variable for the loop.

(ix) Print the sum of the elements of the array.

(6)

QUESTION 3: (25 MARKS)

(a) Using a class hierarchy of your own choosing, with at least TWO subclass levels, show

(i) the use of abstract and concrete forms of Java class and method in your hierarchy.

(5 marks)

(ii) the role and purpose of constructors in each level of your hierarchy.

(5 marks)

(iii) the meaning of 'polymorphism'.

(5 marks)

In your answer, it should be clearly and fully explained how the proposed class structure is reflected in Java language and constructs.

(b) (i) Consider a class ‘Employee’. Given the knowledge we have of some of the different categories of employees like permanent salaried employees, commission employees, etc, write the sample code in Java to represent the class ‘Employee’ such that it forms part of a hierarchy with subclasses.

Note: The class Employee should have different other classes inheriting from it at more than one level.

(5 marks)

(ii) Use this same example to explain how we can simulate the concept of multiple inheritance in Java by using interfaces. Here we should make the ‘Employee’ our interface.

(5 marks)

QUESTION 4: (25 MARKS)

(a) Discuss the Java approach to event processing by explaining how event handling works in Java. Make use of a suitable example to support your

answer. (5 marks)

(b) Explain what is a ‘Listener’ in Java using an appropriate example? (10 marks)

(c) Discuss the purpose and use of the Java Adapter classes. Use appropriate examples to support your answer.

Figure

Updating...

References

Related subjects :