Final Oop 2023
Final Oop 2023
println("Result: " +
Employee that has fields for employee's called Calculator that involves methods for calculator.subtract(a, b));
name, ID, and salary. The program must four basic operations.
accept all values of the mentioned fields and break;
print them in a single line of code. It must also import java.util.Scanner;
calculate a new salary after a 10% increment case '*':
of the existing salary. class Calculator {
System.out.println("Result: " +
import java.util.Scanner; public int add(int a, int b) { calculator.multiply(a, b));
int number = rand.nextInt(10); o Closes the for loop. public Rectangle(double length, double
width) {
if (number % 2 == 0) { 9. System.out.println("Count of
Even = " + count); this.length = length;
count++;
this.width = width;
o Prints the total
} count of even
}
numbers generated
}
in the loop. @Override
System.out.println("Count of Even = " +
(b) Suggest the correct output of the program double area() {
count);
The output of the program will be a count of return length * width;
}
even numbers generated from 20 random
integers between 0 and 9 (inclusive). The }
(a) Explain the meaning of each line of code.
exact count can vary since it depends on the
1. public void generateNumbers() random number generation, but the output }
{ format will be: Count of Even = <number>.
public class Main {
QN 2. Write a program to implement three
o Declares a public
public static void main(String[] args) {
Classes called Circle, Triangle, and
method named
Rectangle.
generateNumbers Circle circle = new Circle(5);
that returns no
abstract class Shape {
value (void). Triangle triangle = new Triangle(10, 5);
abstract double area();
2. int count = 0; Rectangle rectangle = new Rectangle(8,
} 4);
o Initializes a counter
variable count to class Circle extends Shape { System.out.println("Area of Circle: " +
zero. circle.area());
private double radius;
3. Random rand = new Random(); System.out.println("Area of Triangle: "
public Circle(double radius) { + triangle.area());
o Creates an instance
of the Random this.radius = radius; System.out.println("Area of Rectangle:
class to generate " + rectangle.area());
}
random numbers.
}
@Override
4. for (int i = 0; i < 20; i++) {
}
double area() {
o Starts a for loop
QN 1. Part (i): Describe the following terms
that will iterate 20 return Math.PI * radius * radius; as used in Object-Oriented Programming
times, with the loop
counter i starting } (a) Class
from 0 and ending
at 19. } A class is a blueprint or prototype that defines
the attributes (fields) and behaviors
5. int number = rand.nextInt(10); class Triangle extends Shape { (methods) that the objects created from the
class can use. It serves as a template for
o Generates a private double base;
creating objects.
random integer
between 0 private double height;
(b) Objects
(inclusive) and 10
public Triangle(double base, double
(exclusive) and Objects are instances of a class. They
height) {
assigns it to the represent real-world entities and encapsulate
variable number. both the state (attributes) and behavior
this.base = base;
(methods) defined by the class. Each object
6. if (number % 2 == 0) { can have different values for its attributes.
this.height = height;
(c) Method } }
Part (ii): Concepts of Object-Oriented Shape shape = new Circle(); System.out.println("Dog barks.");
Programming
shape.draw(); // Abstraction in action }
(a) Inheritance
} }
Inheritance is a mechanism in object-oriented
} public class Main {
programming where a new class (subclass)
inherits attributes and methods from an
(c) Data Encapsulation public static void main(String[] args) {
existing class (superclass). This promotes
code reuse and establishes a hierarchical Data encapsulation is the technique of Animal animal = new Dog();
relationship between classes. restricting access to certain details of an
object and only exposing certain parts. This is animal.sound(); // Polymorphism in
Example: action
achieved using access modifiers like private,
protected, and public.
class Animal { }
Example:
void eat() { }
class Person {
System.out.println("This animal eats Part (iii): Differences
food.");
private String name;
(a) Method Overriding and Method
} Overloading
private int age;
} Method Overriding:
public void setName(String name) {
class Dog extends Animal { Definition: Occurs when a subclass provides
this.name = name;
a specific implementation of a method
void bark() {
} already defined in its superclass.
System.out.println("The dog barks.");
public String getName() { Purpose: To allow a subclass to provide a
specific implementation for a method that is
}
return name; already defined in its superclass.
}
} Example:
public class Main {
public void setAge(int age) { class Animal {
public static void main(String[] args) {
this.age = age; void sound() {
Dog dog = new Dog();
} System.out.println("Animal makes a
dog.eat(); // Inherited method sound.");
public int getAge() {
dog.bark(); // Method of Dog class }
return age;
} }
}
} class Dog extends Animal {
}
(b) Data Abstraction @Override
(d) Polymorphism
Data abstraction is the concept of hiding the void sound() {
Polymorphism is the ability of an object to
complex implementation details of an object
take on many forms. It allows a single System.out.println("Dog barks.");
and exposing only the essential features. It
interface to represent different underlying
helps in reducing programming complexity
forms (data types). It is of two types: compile- }
and effort.
time polymorphism (method overloading)
and runtime polymorphism (method }
Example:
overriding).
Method Overloading:
abstract class Shape {
Example:
Definition: Occurs when multiple methods
abstract void draw();
class Animal { with the same name exist in the same class but
with different parameters.
}
void sound() {
Purpose: To allow a class to have more than
class Circle extends Shape {
System.out.println("Animal makes a one method having the same name if their
void draw() { sound."); parameter lists are different.
Scope: The member is accessible from any public void pupAge() { System.out.println("Enter the time of the
other class. meeting (HH:MM):");
int age = 0;
Example: meeting.setTime(scanner.nextLine());
age = age + 7;
class Example { System.out.println("Enter the location
System.out.println("Puppy age is: " + of the meeting:");
public int data; age);
} } meeting.setLocation(scanner.nextLine());
Default (Package-Private): public static void main(String[] args) { System.out.println("Enter the subject of
the meeting:");
Scope: The member is accessible only within Test test = new Test();
the same package. meeting.setSubject(scanner.nextLine());
test.pupAge();
Example: System.out.println("Meeting Details:");
}
class Example { System.out.println("Time: " +
} meeting.getTime());
int data; // No modifier means default
access Output: System.out.println("Location: " +
meeting.getLocation());
} Puppy age is: 7
System.out.println("Subject: " +
(d) Interfaces and Packages Part (v): Design a Class Called Meeting meeting.getSubject());
class Meeting { }