Cse 4102 - Object Oriented Programming
Cse 4102 - Object Oriented Programming
SCHOOL OF BUSINESS
DEPARTMENT OF MANAGEMENT SCIENCE
}
}
}
(b) Write a Java application that prompts for two integer values typed by a user at the
keyboard computes the sum of the values and displays the result. The program uses
java.util package. NB: Name of the class is Addition [4 Marks]
i. Polymorphism
ii. Coupling
iii. Inheritance
iv. Abstraction
(d) Explain the functional significance between operation of the keywords extends and
implements as used in Java. [2 Marks]
(e) Define a class named TaxWhiz that computes the sales tax for a purchase. It should
store the current tax rate as an instance variable.. This class should have one public
method, calcTax(double price), which returns a double, whose value is price times the
tax rate. For example, if the tax rate is 4 percent, 0.04, and the price is Ksh.100, the
calcTax() should return 4.0. [4 Marks]
(f) Identify and fix the errors in the following code: [4 Marks]
11111
33333
55555
77777
99999
(h) Distinguish between a do/while loop and a while loop using flow chart. [3 Marks]
2
(b) What is recursion in Java Classes? [2 Marks]
(c) Write a Java program that defines a function called "maxValue" that takes an array of
integers and its length as arguments and returns the maximum value in the array.
[5
Marks]
(d) How do you find the length of an array in Java. Use an example? [2 Mark]
(e) What is a two-dimensional array in Java? How is it declared and initialized?[2 Marks]
(g) Write a program that is able to compute the factorial of a number using a method
factorial( int x) [5 Marks]
(b) Define the class bankAccount to store a bank customer’s account number and balance.
Suppose that account number is of type int, and balance is of type double. Your class
should, at least, provide the following operations: set the account number, retrieve the
account number, retrieve the balance, deposit and withdraw money, and print
account information. Add appropriate constructors. (3 Marks)
(c) Every bank offers a checking account. Derive the class checkingAccount from the class
bankAccount (designed in part (b)). This class inherits members to store the account
number and the balance from the base class. A customer with a checking account
typically receives interest, maintains a minimum balance, and pays service charges if
the balance falls below the minimum balance. In addition to the operations inherited
from the base class, this class should provide the following operations: set interest rate,
retrieve interest rate, set minimum balance, retrieve minimum balance, set service
charges, retrieve service charges, post interest, verify if the balance is less than the
minimum balance, write a check, withdraw (override the method of the base class), and
print account information. NB: Add appropriate constructors. (5 Marks)
(d) Every bank offers a savings account. Derive the class savingsAccount from the class
bankAccount (designed in part (b)). This class inherits members to store the account
number and the balance from the base class. A customer with a savings account
typically receives interest, makes deposits, and withdraws money. In addition to the
operations inherited from the base class, this class should provide the following
operations: set interest rate, retrieve interest rate, post interest, withdraw(override the
method of the base class), and print account information. NB: Add appropriate
constructors. (4 Marks)
(e) Write a program to test your classes designed in parts (c) and (d). (4 Marks)
3
Question Four (20 Marks)
(i) Design a class named Account that contains:
i. A private int data field named id for the account (default 0).
ii. A private double data field named balance for the account (default 0).
iii. A private double data field named annualInterestRate that stores the current
interest rate (default 0). Assume all accounts have the same interest rate.
iv. A private Date data field named dateCreated that stores the date when the
account was created
v. A no-arg constructor that creates a default account.
vi. A constructor that creates an account with the specified id and initial balance
vii. The accessor and mutator methods for id, balance, and annualInterestRate.
viii. The accessor method for dateCreated.
ix. A method named getMonthlyInterestRate() that returns the monthly interest
rate.
x. A method named withdraw that withdraws a specified amount from the
account.
xi. A method named deposit that deposits a specified amount to the account.
Required:
Draw the UML diagram for the class. Implement the class. Write a test program that
creates an Account object with an account ID of 1122, a balance of $20,000, and an
annual interest rate of 4.5%. Use the withdraw method to withdraw $2,500, use the
deposit method to deposit $3,000, and print the balance, the monthly interest, and
the date when this account was created. [10 Marks]
(j) Explain how a developer can achieve the following while programming? [6 Marks]
i. Encapsulation
ii. Abstraction
iii. Polymorphism
(k) Explain the use of the following key words [4 Marks]
i. final
ii. extends
iii. super
iv. abstract
i. Draw the inheritance hierarchy showing the relationship between these two classes.
4
[2 Marks]
ii. Identify those methods which would be considered polymorphic. [3 marks]
(b) Consider the definition of the class C shown below.
public class C
{
int m;
int n;
public C(int mIn, int nIn)
{
m = mIn;
n = nIn;
}
public int m1()
{
return m+n;
}
}
Define a subclass of C named B that overrides method m1() so that it returns the difference
between m and n instead of their sum. Provide the appropriate access modifier for the
instance variables m and n. [5 Marks]
(c) Write a program that displays menu below, allows the user to chose any menu option
and displays message such that: if someone chooses 1, it displays the message “ You
have selected register student “. If option is 2, it displays the message “ You have
selected Search for student”. If option is 3, it displays the message “ You have selected
print student records” and if option is 4, it dispays the message “ Exit the application”.
Sample menu:
(d) Write a method squareOfAsterisks that displays a solid square ( the same number of rows and
columns) of asterisks, whose side is specified in integer parameter side. For example, if side is 4,
the method should display [3Marks]
* * * *
* * * *
* * * *
5
* * * *