Semester 1 Marking Scheme

BSc Software Engineering & BSc Business Information System MSc Information Systems(Conversion) Resit - Examinations for 2004 – 2005 / Semester 1 Mark...
0 downloads 1 Views 132KB Size
BSc Software Engineering & BSc Business Information System

MSc Information Systems(Conversion) Resit - Examinations for 2004 – 2005 / Semester 1 Marking Scheme MODULE: OBJECT ORIENTED PROGRAMMING MODULE CODE: BISE050 Duration: 2 Hours Instructions to Candidates: 1.

Answer all questions

2.

This paper carries a total of 60 marks.

This question paper contains 3 questions and 4 pages.

QUESTION 1

(20 Marks)

a) With appropriate examples, describe the following i) Single and multiple inheritance

(4 marks)

The ability to use the data and methods of a parent class within a sub-class and the objects created from it is called inheritance. A sub-class is said to inherit specific member data and methods from its parent class. The parent class is also referred to as the base class or super class. Apart from the inherited properties, the sub-class can have other data members and methods. In single inheritance a class or sub-class is created from only one parent class. Example 1 Consider the class shape. Sub-classes circle, triangle, rectangle … could be created from the class shape.

shape circle

triangle

rectangle

In multiple inheritance a class or sub-class can be created from more than one parent class. Example 1 Consider the class father and mother. Sub-class child cane be created from both classes father and mother.

father

mother child

ii)

Interface

(2 marks)

An interface is a contract in the form of a collection of method and constant declarations. When a class implements an interface, it promises to implement all of the methods declared in that interface

Page 2 of 9

The variables declared in an interface body are available as constants to classes that implement the interface. This enables pools of related constants to be defined and made available to classes that require use of these constants. Common constant pools are color constants, mathematical constants, and formatting constants. Variables declared in an interface are implicitly static and final and must be initialized in their declaration. The methods are public abstract by default An interface is allowed to extend more than one interface, but a class can extend only a single class The extending interface inherits all the methods and constants defined in the interfaces that it extends. When a class implements an interface X, it must implement all methods of all interfaces that X extends iii) Private and public modifiers

(2 marks)

Classes may not be private. A private method or variable is only visible within its own class. A public class, method or variable is visible anywhere its class is visible (1 + 1 Marks) iv) Static variable

(2 marks)

Class static variables are associated with the class. There is only one copy of a class variable regardless of the number of instances of a class. Static variables can be accessed through the class rather than through an instance. v) Polymorphism (2 marks) Polymorphism means ‘many forms’for example we can have a methods in a class with the same name but performing different task for eg.in one class we have the following 3 methods add(int,int);// to add 2 integer numbers add(float,float); // to add 2 float numbers add(string,string);// to concatenate 2 strings When the method is invoked, the appropriate method depending on parameters is executed. Polymorphism is also the ability for objects of different classes related by inheritance to respond differently to the same message. The methods differ in the following way No of arguments Type of arguments Order of arguments vi) Class and Object

(4 marks)

A class is a blueprint or prototype that defines the variables and the methods common to all objects of a certain kind.

Page 3 of 9

Object- An object is a software bundle of related variables and methods. Software objects are often used to model real-world objects you find in everyday life b) Explain with the help of a diagram, explain the compilation process of a Java program. (4 marks) Java Compiler(Macintosh) Java Program

Java Compiler(Win

Java ByteCodes Platform Independent

Java Compiler(Solaris)

QUESTION 2

Binary File(Run on Mac)

Binary File(Run on Win) Binary File(Run on Solaris)

(20 Marks)

a) Correct the following codes i) class Human { int eyeColor; . . . } class myclass { public static void main(String arg[]) { Human m = new Human(); m.eyeColor= 1; } ii)

int j=0; j = j + 1;

iii) class Child { int noOfChild; Child(int c) // Constructor { noOfChild = c; System.out.println(“Child =“+noOfChild); } } class testMain { public static void main(String arg[]) { Child c2 = new Child(10); } }

Page 4 of 9

QUESTION 2 (Continued) iv) class Feline { boolean hungry = true; void speak(); { …..; } void call() { if (hungry) speak(); } } class testmain { public static void main(String arg[]) { Feline theCat = new Feline(); } } class Feline { boolean hungry = true; abstract void speak(); void call() { if (hungry) speak(); } } OR class Fel extends Feline { void speak() { ….; } } class testmain { public static void main(String arg[]) { Fel theCat = new Feline(); }

Page 5 of 9

}

(5 marks) b) Implement the class Time which consists of i) three private integer data members (hours, minutes and seconds). ii) a constructor to initialise the data members to zero iii) another constructor that takes three integer arguments(hours, minutes and seconds) and assigns them to the data members. iv) an accessor method setHour that takes one integer argument hours and assigns it to data member hours. v) an accessor method setMinute that takes one integer argument minutes and assigns it to data member minutes. vi) an accessor method setSecond that takes one integer argument seconds and assigns it to data member seconds. vii) a method to convert and return a given time to seconds. viii) a method to convert and return a given time to minutes. ix) a method to display time in the format “HH/MM/SS”. (15 marks) class Time { private int hours; private int minutes; private int seconds; ( 2 marks) public void Time() { hours=0; minutes = 0; seconds = 0; }

(2 marks)

public void Time(int hours, int minutes, int seconds) { this.hours = hours; this.minutes = minutes; this.seconds = seconds; } (2 marks) public void setHour(int hours) { this.hours = hours; } (1 mark) public void setMinute(int minutes) { this.minutes = minutes; } (1 mark) public void setSecond(int seconds) { this.seconds = seconds; } (1 mark)

Page 6 of 9

public int timeToSecond() { return seconds + 60 * minutes + 3600 * hours; } (2 marks) public double timeToMinute() { return (seconds/60) + minutes + hours * 60; } (2 marks) public void displayTime() { System.out.println(hours + “/” + minutes + “/’ + seconds); } (2 marks) }

QUESTION 3

(20 Marks)

class Shape { int noOfShape; Shape() //Constructor { System.out.println(“Shape constructor”); } Shape(int s) //Constructor { noOfShape = s; System.out.println(“Shape=“+noOfShape); } } class Rectangle extends Shape { int noOfRectangle; Rectangle() // Constructor { super(99); System.out.println(“Rectangleconstructor”); } Rectangle(int r) //Constructor { noOfRectangle = r; System.out.println(“Rectangle =“+noOfRectangle); } } class Box extends Rectangle { int noOfBox; Box(int b) // Constructor { noOfBox = b; System.out.println(“Box =“+noOfBox); } } class testMain { public static void main(String arg[])

Page 7 of 9

{ Box b1 = new Box(10); Rectangle r1 = new Rectangle(20); Shape s1 = new Shape(30); Rectangle r2 = new Rectangle(); Shape s2 = new Shape(); } } a) Define a constructor. Using the above Java program describe constructor chaining. (3+4 marks) A constructor is a special method that is called when an object is created. It is commonly used to perform any further initialisation that is needed. Constructors are defined as methods that have the same name as their class. A constructor method has no return value. A default constructor is automatically created by the Java compiler if we do not explicitly create one. The default constructor does not have any arguments. Once a class has at least one constructor, the default constructor is ignored. (3 marks) When a class is defined, Java guarantees that the class’s constructor method is called whenever an instance of that class is created. It also guarantees that the constructor is called when an instance of any subclass is created. Constructor calls are “chained”. Any time an object is created, a sequence of constructor methods are invoked from subclass to superclass. The body of the superclass constructor is always run FIRST, followed by the body of its subclass, and on down the class hierarchy to the class that is being instantiated. (4 marks)

b) Explain the statement: Rectangle r1 = new Rectangle(20). (4 marks) This is an example of instantiation where object r1 is created from class rectangle. When object r1 is created, the constructor with one argument is invoked and the value 20 is passed to it. Memory is allocated for the instance variable. c) What is the meaning of the keywords super and extends? (4 marks) Sub-classing is the process by which one class can be derived from another. The keyword extends is used for sub-classing. The creation of a class from an existing class(inheritance). (2 marks) Methods and variables can be accessed from the immediate parent class by using “super” keyword.(2 marks)

Page 8 of 9

d) What is the output of the above Java program?

(5 marks)

Shape=99 Rectangleconstructor Box =10 Shape constructor Rectangle =20 Shape=30 Shape=99 Rectangleconstructor Shape constructor

Page 9 of 9