Cosc 226 (Oop Final-Exam)
Cosc 226 (Oop Final-Exam)
I. WRITE YOUR:-
NAME________________________________
IDNO_________________________________
DEPARTMENT________________________
II. INSTRUCTION:
1. DO NOT TURN THE PAGE UNTIL YOU ARE TOLD TO DO SO
2. SWITCH OFF YOUR CELL PHONES
3. MAKE SURE THAT THE EXAM BOOKLET CONTAINS 4 PARTS
4. WRITE YOUR ANSWERS ON THE SPACES PROVIDED AFTER EACH
QUESTION.
Part One: Read the questions 1 to 7 carefully and give short and precise answer for each questions in
the boxes provided under each questions (20 points).
3. Write the difference between classes and interfaces in java programming language(3 point)
Classes Interface
4. Write down the syntax or general form to define a subclass, abstract class and to define an
interface (3 point).
6. List and explain the three access modifiers or specifiers used by java programming language to
access members of the class by giving examples (3 point).
8. Observe the following simple java program and identify the syntax errors and explain the
reason that makes error in this program and write only the correct code to make the program
error free(4 points)
Syntax Errors
final class Father {
final void getFather() {
System.out.println("This is a final method.");
}
}
class Child extends Father {
void getFather() {
System.out.println("This is from sub class B");
}
}
9. Identify the syntax errors you observed for the following java program and write your answer
inside the box provided below (6 points).
package inheritpoly;
import javax.swing.JOptionPane;
class Employee{ Syntax Errors
double salary;
double allwance;
double tax(){
return salary*.35;
double deduction(){
return tax()+ pension();
}
double growthSalary(){
return (salary+allwance);
}
double netPay(){
return (growthSalary()-deduction());
}
}
class TestSalary{
public static void main(String args[]){
//==========================================================
====
JOptionPane.showMessageDialog(null, "WELCOME TO EMPLOYEES SALARY
CALCULATION"+ " ", "", JOptionPane.PLAIN_MESSAGE);
}
}
class Case{
protected int i;
this.i = i;
} Syntax Errors
super(i);
child.i--;
class TestTwo{
b1 = new Reason(11);
b2 = new Reason(22);
a1 = new Case(110);
a1.Increment(a1);
b1.Decrement(b2);
b1.Decrement(b1);
Part Three:-Read the java code that is described in each of the following questions (11-13) carefully
and determine the output of the program you expect and write your answers inside the
box provided under each questions (15 points)
11. Determine the output of the following java program and identify that what object oriented
programming features are implemented by this program (6 points).
package inheritpoly;
class Figure {
double dim1;
double dim2;
Figure(double a, double b) {
dim1 = a;
dim2 = b;
} Output
double area() {
System.out.println("Area for Figure is undefined.");
return 0;
}
}
class Rectangle extends Figure {
Rectangle(double a, double b) {
super(a, b);
}
// override area for rectangle
double area() {
System.out.println("Inside Area for Rectangle.");
return dim1 * dim2;
}
}
class Trig extends Figure {
Trig(double a, double b) {
super(a, b);
}
// override area for right triangle
double area() {
System.out.println("Inside Area for Triangle.");
return dim1 * dim2 / 2;
}
package inheritpoly;
class A {
int i, j;
void showij() {
System.out.println("i and j: " + i + " " + j);
Output
}
}
// Create a subclass by extending class A.
class B extends A {
int k;
void showk() {
System.out.println("k: " + k);
}
void sum() {
System.out.println("i+j+k: " + (i+j+k));
}
}
class SimpleInheritance {
public static void main(String args[]) {
A superOb = new A();
B subOb = new B();
// The superclass may be used by itself.
superOb.i = 10;
superOb.j = 20;
//System.out.println("Contents of superOb: ");
superOb.showij();
// The subclass has access to all public members of its superclass
subOb.i = 7;
subOb.j = 8;
13. Observe the following Java code and determine the output of this program and indicates the
features of java implemented by this program(5 points)
class Hide{
protected float x; Output
protected int y;
public Hide (float x, int y){
this.x = x * x;
this.y = y;
}
}
class Member extends Hide {
private int x;
private float z;
public Member (int x, int y, float z){
super(x,y);
this.x =x;
this.z = z;
}
public void M1(){
z = (super.x + y)/x;
System.out.println("Superclasses version of x: " +super.x);
System.out.println("Subclasses version of x: " +x);
System.out.println(" Z = " +z);
}
}
classHideSuperClassField{
public static void main(String args[]){
Member o = new Member (8, 4, 10);
o.M1();
System.out.println("Y = " +o.y);
}
}
14. Write simple java program to implement both method overloading and overriding based on the
following information. Use Class A as a super class and class B and C as a subclass, where B
inherit properties and methods of class A and C inherits both the properties and methods of class
B and A. You can use any kind instance variables and methods which does not violate the java
convention about method and variable declaration to implement this activity (7 points).
15. Define abstract class by the name Service and abstract method charge() and define a
subclasses by the name Vat, ServiceCharge, TotalPrice, TotalDeduction and TotalCost
that all these subclasses inherits the super class Service and the abstract method charge()
is left to all these subclass . Define the necessary attributes constructors, methods, declare
reference variables of a super class and assign the reference variable of a super class to all
subclasses to implement your work. Consider the following information for your work(8 points)
TotalPrice=(UnitPrice * Quantity)
Vat is 15% of TotalPrice
ServiceCharge is 5% of TotalPrice
TotalDeduction=(Vat + ServiceCharge)
TotalCost=(Vat + ServiceCharge + TotalDeduction)
16. Write simple java program to implement both method overloading and overriding based
on the following information. Use Class Example1 as a super class and class Example2
and Example3 as a subclass, where Example2 inherit properties and methods of class
Example1 and Example3 inherits both the properties and methods of class Example2 and
Example1. You can use any kind instance variables and methods which does not violate
the java(5 points)
17. Define abstract class by the name Bank and abstract method calculate () to perform
different kinds of bank operations such as register customer, deposit, withdrawals and net
balance and declare all the necessary instance variables extended to subclasses. Define a
subclass by the name Register, Deposit, Withdrwals and NetBalance by extending the
super class Bank. The implementation of abstract method calculate () is left to all these
subclasses. Use the following information for your work( 5 points)