Assignment No 3
class Shape {
public void calculateArea() {
System.out.println("Calculating area of a shape.");
class Circle extends Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
@Override
public void calculateArea() {
double area = Math.PI * radius * radius;
System.out.println("Area of the Circle: " + area);
}
class Rectangle extends Shape {
private double length, width;
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
@Override
public void calculateArea() {
double area = length * width;
System.out.println("Area of the Rectangle: " + area);
class Calculator {
public int add(int a, int b) {
return a + b;
public double add(double a, double b) {
return a + b;
public double add(int a, double b) {
return a + b;
public double add(double a, int b) {
return a + b;
public class Main1 {
public static void main(String[] args) {
Calculator calc = new Calculator();
System.out.println("Adding integers (5 + 10): " + calc.add(5, 10));
System.out.println("Adding doubles (3.5 + 2.2): " + calc.add(3.5,
2.2));
System.out.println("Adding int and double (7 + 4.3): " + calc.add(7,
4.3));
System.out.println("Adding double and int (6.1 + 3): " +
calc.add(6.1, 3));
Shape shape;
Circle circle = new Circle(5.0);
Rectangle rectangle = new Rectangle(4.0, 6.0);
shape = circle;
System.out.println("\nInvoking calculateArea() on Circle object:");
shape.calculateArea();
shape = rectangle;
System.out.println("\nInvoking calculateArea() on Rectangle
object:");
shape.calculateArea();