BAHRIA UNIVERSITY (Karachi Campus) : Object-Oriented Programming (Csc-210)
BAHRIA UNIVERSITY (Karachi Campus) : Object-Oriented Programming (Csc-210)
QUESTION NO. 01: (UML BASED DEVELOPMENT SKILL - ABSTRACTION) [2+6+7 = 15 Marks]
Consider the UML class diagram given at last of the question to code following in JAVA:
Page 2 of 5
QUESTION NO. 02 (SHORT CASES TO CODE) [5x3 = 15 Marks]
Write down the JAVA code for the following given scenarios:
1. Write down the code for creation of two threads A and B with the single print statement for
thread A: “Thread A will win” and for thread B: “Thread B will win”, and also write down Driver
class for this multi-threaded program contains one part to run thread A and B concurrently.
2. A text file “original.txt” is already saved in hard disk consisting following paragraph about OOP
features.
“There are four major features in object-oriented programming that makes them
different than non-OOP languages: encapsulation, inheritance, polymorphism and
abstraction.”
Copy this file into another file “copied.txt”. Write the statements required in try block only with
proper exception handling in catch block(s).
a. fileExists(), that takes as a parameter a string and returns a boolean value that istrue, if
the file whose name is passed as parameter exists, and false otherwise.
b. isInt(), that takes as parameter a string and returns a boolean value that is true if the
string represents an integer, and false otherwise.
c. isDouble(), that takes as parameter a string and returns a boolean value that istrue if the
string represents a real number, and false otherwise.
System.out.println(Integer.parseInt(br.readLine());
Page 3 of 5
3. Following diagram depicts IS-A and HAS-A relationship. Write JAVA class structures for given
relationship.
try
{
for (int i=0; i < numbers.length-1; i++)
{
ratio = numbers[i] / numbers[i+1];
System.out.println(numbers[i]+"/"+numbers[i+1]+
"="+ratio);
}
}
catch (ArithmeticException ae)
{
System.out.println("Couldn't calculate "+
numbers[i]+"/"+numbers[i+1]);
}
}
}
Page 4 of 5
5. Consider the following code:
6. Consider UML given in Question No. 01 and analyze the following statements. Perform
downcasting to the statements (1 to 6) where requires.
a) c.setCurrentYear(2019); //Statement 1
b) c.setModelYear(2020); //Statement 2
c) c.setBaseTax(7); //Statement 3
d) c.setSpeed(100); //Statement 4
e) c.setFuelLevel(20); //Statement 5
f) System.out.println(c); //Statement 6
7. “instanceOf is a binary operator in Java, which can resolve DownCasting”. Defend this
statement.
9. Constructor chaining is the process of calling one constructor from another constructor with
respect to current object. Constructor chaining can be done in two ways:
I. Within same class: It can be done using this() keyword for constructors in same class
II. From base class: by using super() keyword to call constructor from the base class.
Construct a chain of follow constructors within a class Temp using this() keyword.
Temp()
Temp(int a)
Temp(int a, int b)
10. A file is having stored numbers from 10 to 59 under the name “numbers.txt”. What value
does read() returns when it has reached the end of a file while working with transfer of
control or conditional statements. Also name the built-in class of read() method.