You are on page 1of 7
BN. THE UNIVERSITY OF DODOMA : COLLEGE OF INFORMATICS AND VIRTUAL EDUCATION t SCHOOL OF INFORMATICS FIRS i CS 213: OBJECT ORIENTED PROGRAMMING IN JAVA EMESTER 2016/2017 Date: 21° February, 2017 ait, Time Allocated: 3 Hours Instructions: i 1. This omit as of se til Aa _ 2. Answer any six (6) * — Question One } »/ LAB8 2) Write a program that illustrates exee methods propagator! and propagat should initially throw an ArithmeticException exception, | Method propagator2 should call Propagatori, 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 programs? [5 Marks} ; Ption propagation, Define tor2. Method propagator! . ] y public class Arravs{ . public static void main(String argsip) ¢ int al] = {5, 1, 15, 20, 25}, ; intij,m; 2 EA a ¢ pe i= +tafL), / + i j= aif ings \ m = afi +]; vem.out.printin(i + j + m); } at Kg 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: (2C x 9) + 5 + 32 [15 Marks] ae Why constructors and static methods cannot be declared abstract [5 Marks] Question Three a) Create class SavingsAccount. Use a ic variable 1A iz &. annuallnteresiRat to store the annual interest rate for all account holders. Bach object of the class contains a private instance variable savin, Balance. cating the amou saver currently has on deposit. Provide method calculateMonthlyInterest» to;calculate the monthly inter t by multiplying the savingsBalance by annuallnterestRate divided by 12—and add this interest to savings-Balance. Provide a static method modifyInterestRate that sets the annuallnterestRate to a new value. Write a Test class to test class SavingsAccount capability. Instantiate two savingsAccount objects, saver! and saver2, with balances of TSh 20,000.00 and TSh 30,000.00, respectively. Let the user set annuallnterestRate to arbitrary value, then calculate the monthly interest for each of 12 months and print the new balances for both savers. {15 Marks] ) What is the source of error in this piece of code? [S Marks] Page 3 of 7 class Calculation{ void sum(int x, int y){System.out printings + y); void sum(int a, int b){System.out.printin(a + public static void main(String args[))f Calculation object = new Calculation@, object.sum(20, 20); i ‘ mate? Jum wih 2 pam clepinsd 19 clorr Callarehn Question Four a) What does the following piece of code print? String(“the university of dodoma”).substring(0, 1).toUpperCase() + “he uni new String("the university of dodoma").substring(8,9).toUpperCase() + "odoma’ Prompts the user for a are greater than zero) “a verage, product i. Find and print to the screen the sum, 2 rage, | ntered values. largest and smallest value of ail ¢ i sr of even and odd ii. It should also count and print the number of ever numbers. -HE SUM IS iii, If the sum is greater than 100, it should print THE SUM 4 it GREATER THAN 100 if the sum is less than 100 i jr f the hould 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 1S THAN 10000 and if the product is equal to 10000 it should print THE PRODUCT IS EQUAL T ‘© 10000. 2) Bikien Five Write a java program which contain an interface called ThreeDimentionalShape 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 ihe capability of the program. Test class should use an array of. ThreeDimentionalShape references to objects of each concrete class in the hierarchy. The program should print a text description of the Page 5 of 7 object to which cach 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 ae Use the following formula: * Area of a Cube = 6a" ‘ © Volume of a Cube = a? © Area of a Sphere = 4ur” * Volume of Sphere = (4/3)ar* Given = 3.14, a = 3m, r= Sem Question Six a) Assume that you have a MySQL database named running on localhost server and it hi Public class Animal{ t public class Dog extends Animal{ public static void main(String args[, De Dog d = new Dog(); System.out.printIn(d instanceof : ‘Animal); } j } uA 2 7 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 f the number of hours worked, hourly employees ate paid regardless 0} 1.5 times their hourly by the hour and receive overtime pay (ie., salary rate) for all hours worked in excess of 40 hours, commission employees are paid a percentage of their sales and base: commission employees receive a base salary plus a perce

You might also like