THE UNIVERSITY OF DODOMA
COLLEGE OF INFORMATICS AND VIRTUAL EDUCATION
SCHOOL OF INFORMATICS
UNDERGRADUATE UNIVERSITY EXAMINATIONS
FIRST SEMESTER 2018/2019
CS 213: OBJECT ORIENTED PROGRAMMING IN JAVA
Date: 19" February, 2019ION A (40 Marks)
Question One
a) Considering a piece of code given bellow, create a
initializing properties of class B.
[class Af
| int y;
public A(int y)f
i this.y = y,
i
lara
da
| class B extends Af
UL
v >) In the following program, called ComputeResult, what is
of result after executing each numbered line?
public class ComputeResult a
public static void main(String[] args) {
String original ="
resilt.setCharA
result. insert
result.~ Question Two
Read carefully the following program then answer the questions that
follow: -comments indicate where missing components of the program
are to be placed,
public class MainClass {
public static void main(String[] args){
//(2) print the greeting
//(3) construct a MyClass object called myObject
/(A) update()
i
class MyClass{
// (1) definition of MyClass constructor
public static void greetings(){
// definition of greets
y i
public void update(int num, String title) {
// definition of update
}
public void print) {
// definition of print
iy
¥
4
private int numOfltems:
private String reportTitle;
Bea u wish to call the method
2 you wis
suppose
>, Write a statement(s)
) above
line (2,
otly.
orrec
ou wish to call the update
t
se Y
supp¢
correct java statement that
Write
Question Three
and describe the error in the foll O
ort java.io. *,
lass Parent {
void message(){
System. out.printin("Parent");
public c
oid message() throws SOL,
System. out. printin("Child"
public static void main(Sh
Parent p = new Childg;
| P-message(); » aa) Indicate the output of the following program {7 Marks}
public class Cive{
int x = 0;
int f(int x) {
return x * x;
}
int z = f(£(x+1));
public static Void main(String args[]) {
| Cive c = new Cive();
}
iy secs deccohs Mii canal Me es gaeaatieay
SECTION B (60 Marks)
Question Four
a) Assume that you have 2 MySQL database named EmployeeDB
running on localhost server and it has a table called EmployeeInfo
with four columns: p_id, first_name, last_name and phone_number.
Write a Java program that will establish JDBC connection to the
database and query the database’s Employeelnfo table to display
first name, last name and phone number of the employees.
Exception handling is inevitable for database connectivity, therefore
provide try-catch-finally block for exception handling where
appropriate. Given the driver class is com.mysql.jdbc.Driver, the
port number is 3306. Use root for both username and password.
{15 Marks}4) Write a java app that contains classes Polygon, Rectangle, Trlangle
1 myMain. Class Polygon declares instance variables height and
width and defines a method serValues for setting height and width
values supplied by user. It should also define abstract method area.
Class Rectangle extends class Polygon and implements method
area for calculating and returning area of a rectangle. Class Triangle
extends class Polygon and implements method area for calculating’
and returning area of a triangle. Class myMain defines
method, creates Rectangle and Triangle’s objects and calls
methods for demonstrating their capabilities.
Question Five
¥ a) Thoroughly read the following problem statement then a
following questionsFahrenheit (°F): F = (°C * 9/5) + 32
1 Use anonymous class in the implementation of event
handling. [15 Marks}
ii Use inner class in the implementation of event handling.
[15 Marks]
Question Six
v a) Create a Java program for registering electorates. The program has
to use the method called age Validator for validating the age of the
electorates. In case the age supplied is below 18 years, the method
has to throw (using throw keyword) the custom exception called
CiveException. [15 Marks}
» b) Using Swing graphical user interface components, write a java
program that accepts radius of a circle from the user and outputs
area of the circle to the screen. User has to input the radius through
input dialog window and the program has to output the area through
the output dialog window. Use the fo! owing formula to calculate
the area of a circle: area = oe \
A ;
ae