0% found this document useful (0 votes)
12 views6 pages

Exercise 02

The document outlines an exam paper for the course DCT1094: Object Oriented Programming, covering various programming tasks and questions related to Java. It includes questions on assignment statements, logic errors, method definitions, and class design, as well as practical programming tasks such as calculating averages, areas, and volumes. The exam consists of multiple questions with specific marks allocated for each part.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views6 pages

Exercise 02

The document outlines an exam paper for the course DCT1094: Object Oriented Programming, covering various programming tasks and questions related to Java. It includes questions on assignment statements, logic errors, method definitions, and class design, as well as practical programming tasks such as calculating averages, areas, and volumes. The exam consists of multiple questions with specific marks allocated for each part.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

ANSWER SCHEME

COURSE CODE : DCT1094

COURSE TITLE : OBJECT ORIENTED


PROGRAMMING

SEMESTER/SESSION : 2 (2021/2022)

DURATION :3 HOURS
_____________________________________________________________________________

QUESTION 1

a) Write assignment statements that multiply the values of variables x (2 marks)


and y and store the result in x.

b) Assume that the variables x, y, z, and result are all integers. x =


1, y = 3, and z = 5. What value will be stored in result in each
of the following statements?

i) result = x + y; (1 mark)

ii) result = x + y * z; (1 mark)

iii) result =z + (y * x); (1 mark)

c) Explain the term logic error. Identify and correct any logic errors in the
following Java program in Figure 1.

import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter score for DCT1094 ");
int score = input.nextInt();
if (score >=40)
System.out.println("Fails");
}
}
(6 marks)
Figure 1- Java Program Containing Logic Errors

d) Write a Java program to input a thousand (1000) integers and print the (11 marks)
average. Do not use an array.

2
_____________________________________________________________________________

QUESTION 2

a) Identify and correct an error in the following method definition.


i)
public static void info();{
System.out.println("Programmed by Abu");
} (3 marks)

ii)
public static void average(int N1, int N2) {
return ((N1+N2)/2.0);
}
(3 marks)

b) Write a method named inchToCM that receives a value in inches and


returns the equivalent measurement in centimeters. 1 inch is equal to
2.54 centimeters. (5 marks)

QUESTION 3

Write a Java program that prompts the user to input the length and width of a
rectangle. The program should then display the area of the rectangle.

The calculation of the area is done in a method called getArea that takes
two integers (length and width) as parameters and returns the area of the
rectangle (area = length x width).

Here is a sample run (Figure 2):

Figure 2- Sample Run (15 marks)

3
_____________________________________________________________________________

QUESTION 4
The following Java program (Figure 3) is designed to calculate the perimeter
for a rectangle and a cuboid. Please answer the following questions based on
the provided code.

//program to calculate perimeter of a rectangle and a


cuboid
import java.util.Scanner;
class rectangle{
protected int width, length;

rectangle(){
width=1;
length=1;
}

rectangle(int newWidth, int newLength){


width=newWidth;
length=newLength;
}

public int getPerimeter(){


return 2*(width+length);
}

public void setWidth(int newWidth){


width=newWidth;
}

public void setLength(int newLength){


length=newLength;
}

public int getWidth(){


return width;
}

public int getLength(){


return length;
}
}

class cuboid extends rectangle{


private int height;

cuboid(){
width=3;
length=2;
height=5;
}

4
_____________________________________________________________________________

cuboid(int newWidth, int newLength, int newHeight){


width=newWidth;
length=newLength;
height=newHeight;
}

public int getPerimeter(){


return 4*(width+length+height);
}

public void setHeight(int newHeight){


height=newHeight;
}

public int getHeight(){


return height;
}
}

public class Main


{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
rectangle rectangle1 = new rectangle();
cuboid cuboid1= new cuboid();
}
}

Figure 3- Program to Calculate Perimeter of a Rectangle and a Cuboid

a) Identify and list TWO (2) classes, other than the main class, that are
defined in the program.
(2 marks)
b) Identify and list all objects instantiated in the program.
(2 marks)
c) Identify the parent class and its corresponding child class in the
program. (2 marks)

d) Create a UML Class Diagram to illustrate the classes in the program. (6 marks)

e) Draw a UML Object Diagram to illustrate objects in the program. (6 marks)

f) Provide an explanation of how the object-oriented concepts of (8 marks)


Encapsulation and Abstraction were implemented in the program.

5
_____________________________________________________________________________

QUESTION 5

a) Design a class named Pyramid that extends Rectangle as shown in


Figure 3 in Question 4. Figure 4 depicts the shape of a pyramid.

Figure 4- Pyramid Shape

The class should contains:

i) An Additional private integer data fields named height. (2 marks)

ii) A no-arg constructor that creates a default Pyramid. Set the default (3 marks)
values 5 for width, length and height.

iii) A constructor that creates a Pyramid with the specified width, (3 marks)
height, and length.

iv) A method named getVolume() that returns the area of this (3 marks)
Pyramid.
Volume =(width * length * height)/3;

vi) A appropriate accessor and mutator methods for the class. (6 marks)

b) Write a test program that instantiates (create) TWO (2) Pyramid objects
namely pyramid1 and pyramid2 to hold the following data. (9 marks)

Table 1 – Pyramid Objects and Data


Object width length height
Pyramid1 5 5 5
Pyramid2 7 7 15

-------------------------------------------End of question----------------------------------------
6

You might also like