0% found this document useful (0 votes)
0 views6 pages

java revision questions (1)

The document contains a series of revision questions for BIT students focused on Object-Oriented Programming (OOP) in Java. It covers topics such as creating a modular calculator application, demonstrating OOP concepts like inheritance, polymorphism, and abstraction, as well as practical coding exercises involving class design, method overloading, and encapsulation. Additionally, it includes case studies and GUI design tasks to reinforce the understanding of OOP principles.

Uploaded by

treshobya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views6 pages

java revision questions (1)

The document contains a series of revision questions for BIT students focused on Object-Oriented Programming (OOP) in Java. It covers topics such as creating a modular calculator application, demonstrating OOP concepts like inheritance, polymorphism, and abstraction, as well as practical coding exercises involving class design, method overloading, and encapsulation. Additionally, it includes case studies and GUI design tasks to reinforce the understanding of OOP principles.

Uploaded by

treshobya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Object oriented programming –java 1- Revision Questions for BIT students

QN1: Modular Calculator Application Using Java OOP Concepts


You are tasked with designing a modular calculator application in Java for a tech company. The
system should be built using Object-Oriented Programming (OOP) principles, and must support
multiple types of calculators — starting with Basic and Scientific, with plans to add more in the
future. Your solution must demonstrate the following Java OOP concepts in the order listed: Class
Creation, Method Overloading, Inheritance, Method Overriding, Polymorphism, Abstract Classes
and Interfaces.
a) Program 1: Core Calculator with Inheritance, Overloading, and Overriding. (10
marks)
Begin by creating a class named BasicCalculator that includes overloaded methods for the basic
arithmetic operations: add(int a, int b) and add(double a, double b). Similarly, provide overloaded
versions of the subtract(), multiply(), and divide() methods to handle both integer and double
inputs.
Next, create a new class named ScientificCalculator that inherits from BasicCalculator. This class
should introduce two additional methods: power(double base, double exponent) to perform
exponentiation, and squareRoot(double number) to compute square roots. You must also override
at least one method from BasicCalculator—for example, multiply()—and modify its behavior to
include enhancements such as logging or custom result formatting.
Create a third class named Display to demonstrate polymorphism. In this class’s main() method,
declare a reference of type BasicCalculator, assign it to a ScientificCalculator object, and invoke
an overridden method. This should confirm that the method executed is the one in the actual object
(ScientificCalculator), illustrating runtime polymorphism.
b) Program 2: Applying Abstraction and Interface Implementation (10 marks)
Now, introduce abstraction by creating an abstract class called Calculator, which declares abstract
methods for the fundamental operations: add(), subtract(), multiply(), and divide(). Refactor your
BasicCalculator class so that it extends Calculator and provides concrete implementations of all
these abstract methods.
Finally, define an interface named Displayable with a single method: void displayResult(String
result). Both BasicCalculator and ScientificCalculator must implement this interface. In
BasicCalculator, implement displayResult() to print plain text output. In ScientificCalculator,
override this method to display results with a scientific label or formatted output.

C) : You are required to draw a UML class diagram that visually represents the inheritance
and interface relationships among the key components of your calculator application. Your
diagram should include the following elements: the abstract class Calculator, the concrete class
BasicCalculator, the subclass ScientificCalculator, and the interface Displayable. Clearly indicate
the type of each class (abstract or concrete), the implementation relationship between classes
and interfaces, and the inheritance hierarchy among the classes. In addition, your diagram should
identify which methods are overloaded, overridden, or declared abstract, to reflect how object-
oriented principles are applied in your design. (10 marks)

QN2: Write a Java program that allows the creation of at least three classes, each
demonstrating the following concepts:
1. Constructor Overloading – Use multiple constructors with different parameters in each
class.
2. Method Overloading – Implement multiple methods with the same name but different
parameter lists in each class.
Your program should include:
 A Student class to store student details.
 A Course class to represent course information.
 A Grade class to handle student grades.
In the main method:
 Create objects of each class using different constructors.
 Call the overloaded methods to demonstrate their functionality.

QN3. Write a program that calculates the staff net pay given the gross pay and the tax amount.
Hint: The program should prompt a user to enter the netpay. (4 marks)
d) Write short code snippets to explain upcasting, downcasting and typecasting as applied in object
oriented programming. (6 marks)
e) Identify errors in the code below and rewrite a fully debbuged program. Underline the errors
indefied. (4 marks)

package calculator;
public class Calculator {
Public double add(int a ,double b){
return a+b;
}
public double add(double xint y){
return x+y;
}
public static void Main(String[] args) {
// TODO code application logic here
Calculator calc = new Calculator();
Double sum1=calc.add(23, "Three");
System.println("The First Sum is "+sum1);
}}
QN4: This is a Case Study. It is intended to test the student’s knowledge about the major
principles that make any computer programing language Object Oriented.
Knowledge of computer programming is in high demand in today's technology-driven society.
Knowing object-oriented programming (OOP) can be valuable when developing and maintaining
software programs. You are required to define and also give an example for each of the concepts
below. (4 Marks@)
A. Inheritance
B. Encapsulation
C. Polymorphism
D. Abstraction
E. Constructor

QN5: Areas Examined: Visualizing a real-world problem into software (OOP) classes with
inheritance, overriding capabilities, Polymorphism and coding skills.

a). Draw a well-illustrated diagram of the following scenario in which inheritance is well
implemented between classes. Each class should have a name, instance variables (if applicable),
and method declarations. Draw lines to separate a class name from instance variables and also a
line that separates methods from instance variables.

“Every Animal has a picture, food, hunger, boundaries, and location. We also know that every
animal does the following; make noise, eat, sleep and roam (2marks). A Feline is an Animal that
roams uniquely. A Lion is a Feline, it can make noise uniquely and it can eat but also uniquely. A
Tiger is a Feline, it can make noise uniquely and it can eat but also uniquely. A Cat is a Feline, it
can make noise uniquely and it can eat but also uniquely (3marks). A Hippo is just an Animal. It
can make noise uniquely and it can eat but also uniquely (1marks). A Canine is an Animal that
can roam but uniquely. A Wolf is a Canine, it can make noise uniquely and it can eat but also
uniquely. A Dog is a Canine, it can make noise uniquely and it can eat but also uniquely
(1marks)”.

b). Write a java code that implements the scenario in Question a (remember inheritance is crucial
between those classes). For simplicity make all the identified methods just return a string reflecting
the would be actual action of that method in a given class i.e., the makeNoise () method in a Dog
class should return “dog noise” but makeNoise () method in Cat class should return “cat noise”.
(5 marks)

c) Another class is called PetOwner, it defines a public method called start which takes no
parameters and returns nothing but in the body of this method, creates an object v of the Vet class,
creates an object d of the Dog class, an object h of the Hippo class, then via v call the giveShot()
method and pass d as the parameter, also via v call the giveShot() method again and pass h as the
parameter. Finally, write down what the output will be when this code executes.
(5 marks)

QN6: This is a Case Study it is intended for planning an OOP application


Real Drinks is an upcoming business. It's set out to offer affordable drinks to its customers. It’s
located in Mbarara town, Plot 94 High Street, opposite Mayola just beside Petro fuel station.
The proprietor of Real Drinks wants the business to embrace IT usage in its day-to-day
transactions. He says he has employees. The employees are of different categories i.e., Managers,
Executive Managers, Secretaries, and Waitresses. The proprietor says that all employees must be
known in terms of their name, the salary they will earn, the date when they were hired (this can be
derived from the year, month and date) also the proprietor would like to be able to raise the salaries
of his employees.
At Real Drinks, only the managers and executive managers have a bonus but it's unique for each
of these two categories. It is only the secretaries who can set a date for a meeting. Also, no other
employee can serve a customer apart from a waitress.
According to your AOOP (with java) knowledge, do the following: -
(i) List and describe the classes that will be needed for Real Drinks. (2 marks)
(ii) List and describe the instance variables that will be needed in each class.
(2 marks)
(iii) List and describe the methods that will be in each class (note: have accessor methods,
modifier methods). (4 marks)
(iv) Draw a well-populated (with instance variables and methods) parent-child relationship
diagram that portrays (i) to (iii). (5 marks)
(v) Write a java program that implements (i) to (iv). (7 marks)
QN7 : Graphical User Interface Design and Implementation Using Java AWT and Swing
package.
A graphical user interface is a form of user interface that allows users to interact with electronic
devices through graphical icons and an audio indicator such as primary notation, instead of text-
based user interfaces, typed command labels or text navigation. Below is the Simple Calculator
GUI extracted from JTS Tech Solutions Ug Stock Management Portal. This interface is used by
the company to perform simple addition calculations.

A) You are required to write a well commented code that must have been used by the
organization’s software developer Mr Basaija Kenneth to design and come up with the
interface above. (10 marks)
B) Assume you are the newly appointed software developer of the organization. Based on the
code in PART A, write a short code snippet that implements the functionally of different
buttons as follow;
a. When the user clicks the ADD button, the application should be able to capture
what a user-entered as the first integer and second integer, sum them up and display
the result in the result input field. (5 marks)
b. Lastly, when the user clicks the RESET button, clear all the form input fields and
display the cursor in the first number input field. (5 marks)

QN8: java OOPs Question – Abstraction and Interfaces


Question:
Create a Java program that demonstrates the concepts of abstraction and interfaces.
Your task is to:
a) Create an abstract class Person with:
a. Abstract method displayInfo()
b. Concrete method greet() that prints a welcome message
b) Create a concrete class Student that extends Person and implements the displayInfo()
method.
c) Create an interface called Examinable with the method:
a. void takeExam(String subject);
d) Let the Student class implement the Examinable interface and define the takeExam
method.
e) In your main method:
a. Create a Student object using a reference of type Person.
b. Call both the displayInfo() and greet() methods.
c. Typecast the object (if necessary) and call the takeExam() method.

QN:9 Java OOPs Question – Encapsulation


Question:
Create a Java program that demonstrates the concept of encapsulation.
Your task is to:
a) Create a class called BankAccount with the following private data members:
a. accountNumber (String)
b. accountHolderName (String)
c. balance (double)
b) Provide public getter and setter methods for all fields, ensuring:
a. The balance cannot be set to a negative value.
b. The account holder’s name must not be empty.
c) Add two additional methods:
a. deposit(double amount) – adds to the balance if the amount is positive.
b. withdraw(double amount) – deducts from balance only if sufficient funds are
available.
d) In your main method, create a BankAccount object and:
a. Set account details using setters.
b. Perform deposit and withdrawal operations.
c. Print out the final account details using getters.

End

You might also like