Object Oriented Programming Exam - CP 215

THE UNIVERSITY OF DODOMA
COLLEGE OF INFORMATICS AND VIRTUAL EDUCATION
SCHOOL OF INFORMATICS

UNDERGRADUATE UNIVERSITY EXAMINATION
FIRST SEMESTER 2016/2017

CS 213: OBJECT ORIENTED PROGRAMMING IN JAVA

Date: 21st February, 2017
Time Allocated: 3 Hours

Instructions:

  1. This examination consists of seven (7) questions.
  2. Answer any six (6) questions
  3. All University of Dodoma examination regulations apply.

Question One

a) Write a program that illustrates exception propagation. Define methods propagator1 and propagator2. Method propagator1 should initially throw an ArithmeticException exception. Method propagator2 should call propagator1, re-throw the exception. Call propagator2 from method main, and catch and handle the re-thrown exception by printing the stack trace of the exception. (15 Marks)

b) What will be the output of the following program? (5 Marks)

public class Arrays{
    public static void main(String args[]) {
        int a[] = {5, 1, 15, 20, 25};
        int i, j, m;
        i = ++a[1];
        j = a[1]++;
        m = a[i++];
        System.out.println(i + j + m);
    }
}

Question Two

a) Create a GUI application that will convert degree Celsius (°C) to degree Fahrenheit (°F). The application should have a JTextField labeled Celsius, another JTextField labeled Fahrenheit and a JButton named Convert. The application should allow a user to enter degree Celsius on its JTextField and whenever the user clicks Convert button, the application should convert degree Celsius to degree Fahrenheit and display it on Fahrenheit’s JTextField. Celsius’s JTextField should verify values entered to ensure that non-numeric values are not entered. In case the user enters a non-numeric values the application has to display an “Invalid Value” message on the output Dialog. Use the following formula: (°C × 9) ÷ 5 + 32 (15 Marks)

b) Why constructors and static methods cannot be declared abstract. (5 Marks)


Question Three

a) Create class SavingsAccount. Use a static variable annualInterestRate to store the annual interest rate for all account holders. Each object of the class contains a private instance variable savingsBalance indicating the amount the saver currently has on deposit. Provide method calculateMonthlyInterest to calculate the monthly interest by multiplying the savingsBalance by annualInterestRate divided by 12—and add this interest to savingsBalance. Provide a static method modifyInterestRate that sets the annualInterestRate to a new value. Write a Test class to test class SavingsAccount capability. Instantiate two savingsAccount objects, saver1 and saver2, with balances of TSh 20,000.00 and TSh 30,000.00, respectively. Let the user set annualInterestRate to arbitrary value, then calculate the monthly interest for each of 12 months and print the new balances for both savers. (15 Marks)

b) What is the source of error in this piece of code? (5 Marks)

class Calculation{
    void sum(int x, int y) {System.out.println(x + y);}
    void sum(int a, int b) {System.out.println(a + b);}
    public static void main(String args[]) {
        Calculation object = new Calculation();
        object.sum(20, 20);
    }
}

Question Four

a) What does the following piece of code print? (5 Marks)

new String("the university of dodoma").substring(0,1).toUpperCase() + "he university of" + 
new String("the university of dodoma").substring(8,9).toUpperCase() + "odoma"

b) Write a Java program that does the following: (15 Marks)

Prompts the user for a positive integer value (only values that are greater than zero), it should inform the user to enter a value of zero to exit and the program should terminate if the user enters the value of zero. Checks that the entered integer value is really greater than zero, unless otherwise informs the user and asks the user to enter an integer greater than zero. If the user enters a desired integer value (greater than zero), the program should:

i. Find and print to the screen the sum, average, product, largest and smallest value of all the entered values. ii. It should also count and print the number of even and odd numbers. iii. If the sum is greater than 100, it should print THE SUM IS GREATER THAN 100 if the sum is less than 100 it should print THE SUM IS LESS THAN 100 and if the sum is equal to 100 it should print THE SUM IS EQUAL TO 100. iv. If the product is greater than 10000, it should print THE PRODUCT IS GREATER THAN 10000 if the product is less than 10000 it should print THE PRODUCT IS LESS THAN 10000 and if the product is equal to 10000 it should print THE PRODUCT IS EQUAL TO 10000.


Question Five

Write a java program which contain an interface called ThreeDimensionalShape and three classes called Sphere, Cube and Test. An interface should contain the definition of two methods, calculateArea and calculateVolume. Both Sphere and Cube classes should implement calculateArea and calculateVolume methods to calculate the surface area and volume, respectively, of the three-dimensional shapes. Create a Test class which will contain main method, create all the required objects and call the methods for testing the capability of the program. Test class should use an array of ThreeDimensionalShape references to objects of each concrete class in the hierarchy. The program should print a text description of the object to which each array element refers. Also, in the loop that processes all the shapes in the array, determine whether each shape is a Sphere or a Cube. If it’s a Sphere, display its area and volume and if it’s a Cube, display its area and volume.

(20 Marks)

Use the following formula:

  • Area of a Cube = 6a²
  • Volume of a Cube = aÂł
  • Area of a Sphere = 4Ď€r²
  • Volume of Sphere = (4/3)Ď€rÂł

Given π = 3.14, a = 3cm, r = 5cm


Question Six

a) Assume that you have a 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 EmployeeInfo 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)

b) What will be the output of the following program? (5 Marks)

public class Animal{
}
public class Dog extends Animal{
    public static void main(String args[]) {
        Dog d = new Dog();
        System.out.println(d instanceof Animal);
    }
}

Question Seven

A company pays its employees on a weekly basis. The employees are of four types: Salaried employees are paid a fixed weekly salary regardless of the number of hours worked, hourly employees are paid by the hour and receive overtime pay (i.e., 1.5 times their hourly salary rate) for all hours worked in excess of 40 hours, commission employees are paid a percentage of their sales and base-salaried commission employees receive a base salary plus a percentage of their sales. For the current pay period, the company has decided to reward salaried-commission employees by adding 10% to their base salaries. The company wants you to write an application that performs its payroll calculations polymorphically. Use abstract class Employee to represent the general concept of an employee. The classes that extend Employee are SalariedEmployee, CommissionEmployee and HourlyEmployee. Class BasePlusCommissionEmployee—which extends CommissionEmployee—represents the last employee type.

(20 Marks)


END OF EXAMINATION PAPER