Professional Documents
Culture Documents
IN ASSOCIATION WITH
NAME:
PROGRAM:
COURSE:
DUE DATE:
LECTURER:
TASK 1 : Classes and Objects
this.width = width;
this.height = height;
In this class:
We have private attributes width and height to represent the dimensions of the rectangle.
The constructor Rectangle(double width, double height) initializes these attributes when a Rectangle
object is created.
The calculateArea() function calculates the area of the rectangle by multiplying width and height.
The calculatePerimeter() function calculates the perimeter of the rectangle using the formula 2 * (width
+ height).
In the main() method, we create a Rectangle object with a width of 5 and a height of 10, and then
calculate and print the area and perimeter.
this.width = width;
this.height = height;
}
// Calculate the area of the rectangle
We have added a new method add(Rectangle otherRectangle) that overloads the + operator. This
method takes another Rectangle as a parameter, calculates the sum of their dimensions, and returns a
new Rectangle with those dimensions.
In the main() method, we create two Rectangle objects, rectangle1 and rectangle2, and then use the
add() method to add them together to get a new sumRectangle. Finally, we calculate and print the area
and perimeter of the sumRectangle.
TASK 3: : Inheritance
A)
class Shape {
return 0;
System.out.println("int to double:");
System.out.println("Original int value: " + intValue);
System.out.println("\ndouble to int:");
This program first demonstrates casting from int to double by explicitly casting the intValue to a double.
Then, it demonstrates casting from double to int by explicitly casting the doubleValue2 to an int. When
you run this program, you'll see how the values change after typecasting.
try {
} catch (ArithmeticException e) {
if (denominator == 0) {
In this program, the divideNumber() function takes two integers as input, checks if the denominator is
zero, and if it is, it throws an ArithmeticException with an error message. In the main() method, we call
this function with an example where the denominator is zero and catch the exception to handle the
division by zero error.
try {
} catch (ArithmeticException e) {
System.err.println("Error: " + e.getMessage());
if (denominator == 0) {
In this program, We first call divideNumber(10, 2) with a valid division, which will print the result.
Then, we call divideNumber(10, 0) with division by zero, which will throw an ArithmeticException. We
catch this exception and print the error message.
This program demonstrates the use of the divideNumber() function and how it handles exceptions when
division by zero occurs.
B)
System.out.println("\ndouble to int:");
This program first demonstrates casting from int to double by explicitly casting the intValue to a double.
Then, it demonstrates casting from double to int by explicitly casting the doubleValue2 to an int. When
you run this program, you'll see how the values change after typecasting.
try {
} catch (ArithmeticException e) {
System.err.println("Error: Division by zero is not allowed.");
if (denominator == 0) {
In this program, the divideNumber() function takes two integers as input, checks if the denominator is
zero, and if it is, it throws an ArithmeticException with an error message. In the main() method, we call
this function with an example where the denominator is zero and catch the exception to handle the
division by zero error.
try {
if (denominator == 0) {
In this program, We first call divideNumber(10, 2) with a valid division, which will print the result.
Then, we call divideNumber(10, 0) with division by zero, which will throw an ArithmeticException. We
catch this exception and print the error message.
This program demonstrates the use of the divideNumber() function and how it handles exceptions when
division by zero occurs.
"OOP_Templates_Assignment.zip"}
this.radius = radius;
}
@Override
this.base = base;
this.height = height;
@Override
}
}
In this program:
We define a base class Shape with a default implementation of the area() function that returns 0.
We derive two classes, Circle and Triangle, from the base class.
In the Circle class, we override the area() function to calculate the area of a circle using the formula πr².
In the Triangle class, we override the area() function to calculate the area of a triangle using the formula
0.5 * base * height.
In the main() method, we create instances of Circle and Triangle, calculate and print their respective
areas.
A)
return 0;
this.radius = radius;
@Override
this.base = base;
this.height = height;
@Override
this.side = side;
}
// Override the area() function to calculate the area of a square
@Override
In this expanded program:We add a new class Square derived from the base class Shape.
In the Square class, we override the area() function to calculate the area of a square using the formula
side * side.
In the main() method, we create instances of Circle, Triangle, and Square, calculate and print their
respective areas.
B)
class Shape {
return 0;
this.radius = radius;
@Override
this.base = base;
this.height = height;
@Override
this.side = side;
@Override
}
public class ShapeDemo {
double totalArea = 0;
totalArea += shape.area();
return totalArea;
In this program:
We accumulate the areas of all the shapes to calculate the total area.
In the main() method, we create instances of Circle, Triangle, and Square, put them in an array of Shape,
and then call calculateTotalArea() to compute and print the total area of all shapes using polymorphism.
Task 5: Encapsulation
this.name = name;
this.age = age;
this.rollNumber = rollNumber;
return name;
this.name = name;
return age;
this.age = age;
return rollNumber;
this.rollNumber = rollNumber;
student.setName("Alice");
student.setAge(22);
student.setRollNumber(102);
System.out.println("\nUpdated Details:");
In this class:
We have private attributes name, age, and rollNumber to represent the student's information.
We provide public getter methods (getName(), getAge(), getRollNumber()) to access the attribute
values.
We provide public setter methods (setName(), setAge(), setRollNumber()) to update the attribute
values.
In the main() method, we create a Student object, get and print its initial details, update the details
using setter methods, and then get and print the updated details.
UNIT 3
A)
if (value1.compareTo(value2) > 0) {
return value1;
} else {
return value2;
In this example, we've created a generic class MaximumFinder that uses a type parameter T which must
extend the Comparable interface. The findMaximum method compares the two values using compareTo
and returns the maximum value.
You can create instances of MaximumFinder with different types and find the maximum value for those
types.
B)
private T firstValue;
private U secondValue;
this.firstValue = firstValue;
this.secondValue = secondValue;
public T getFirstValue() {
return firstValue;
public U getSecondValue() {
return secondValue;
this.secondValue = secondValue;
pair.setFirstValue(20);
pair.setSecondValue("World");
In this example, we've created a generic class Pair that takes two type parameters T and U to represent
the types of the first and second values in the pair. The class includes member functions getFirstValue
and getSecondValue to retrieve the values, as well as setFirstValue and setSecondValue to set new
values for the pair. The main method demonstrates how to use this Pair class with different types.
A)
#include <iostream>
namespace Geometry {
class Rectangle {
private:
double width;
double height;
public:
double calculateArea() {
}
// Calculate the perimeter of the rectangle
double calculatePerimeter() {
};
int main() {
return 0;
Inside the Geometry namespace, we include the Rectangle class with its attributes, constructor, and
member functions as in the previous task.
In the main() function, we create a Rectangle object inside the Geometry namespace and use it to
calculate and print the area and perimeter.
By placing the Rectangle class inside the Geometry namespace, you can ensure that it is encapsulated
within that namespace and can be accessed using the scope resolution operator :: as shown in the
main() function.
B)
System.out.println("int to double:");
System.out.println("\ndouble to int:");
In this program:
We demonstrate type casting from int to double by explicitly casting the intValue to a double.
We demonstrate type casting from double to int by explicitly casting the doubleValue2 to an int.
We also demonstrate type casting from double to int with rounding. When you explicitly cast a double
to an int, it truncates the decimal part, effectively rounding towards zero.
Running this program will show how the values change after type casting.
try {
} catch (ArithmeticException e) {
if (denominator == 0) {
In this program, the divideNumber() function takes two integers as input, checks if the denominator is
zero, and if it is, it throws an ArithmeticException with an error message. In the main() method, we call
this function with an example where the denominator is zero and catch the exception to handle the
division by zero error.
B)
try {
} catch (ArithmeticException e) {
if (denominator == 0) {
}
return numerator / denominator;
In this program, We first call divideNumber(10, 2) with a valid division, which will print the result.
Then, we call divideNumber(10, 0) with division by zero, which will throw an ArithmeticException. We
catch this exception and print the error message.
This program demonstrates the use of the divideNumber() function and how it handles exceptions when
division by zero occurs.