You are on page 1of 4

Assignment No.

GRN:12120127
Name of the Student: Waghule Shubham Kalyan
Roll No.: 94
Class: CS
Division: D
Batch: B3

Problem Statement

Calculate area of triangle, square & circle using function overloading. Function parameter
accept from user. Create Base Class Shape and Derived Classes Triangle, Square, Circle
respectively. Implement getInputs() Method for accepting inputs, and Overload setArea()
method for calculating area of respective shapes.

Use Class Tester for creating objects.

Sample Input and Output

Sample Input/Parameter for Triangle Values Expected Output


Height (H) 50 2500
Base (B) 100

Sample Input/Parameter for Circle Values Expected Output


π (Pie) 3.14 7853.98
Radius (R) 50

Sample Input/Parameter for Square Values Expected Output


Side (S) 15 225

import java.util.Scanner;
 
class Shape
{
    Scanner sc = new Scanner(System.in);
   
    float setArea(int h, int b)
    {
        return ((h*b)/2);
    }

    int setArea(int s)
    {
        return (s*s);
    }

    double setArea(double r)
    {
        return ((Math.PI)*r*r);
    }

}
class Triangle extends Shape
{
    public void getInputs()
    {
    System.out.print("Enter the height of triangle: ");
        int h = sc.nextInt();
    System.out.print("Enter the base of triangle: ");
        int b = sc.nextInt();
    System.out.println("Area of triangle is: "+setArea(h,b));
    System.out.println("-----------------------------------");
    }

}
class Square extends Shape
{
    public void getInputs()
    {
    System.out.print("Enter the side of square: ");
        int s = sc.nextInt();
    System.out.println("Area of square is: "+setArea(s));
    System.out.println("-----------------------------------");
    }

}
class Circle extends Shape
{
    public void getInputs()
    {
    System.out.print("Enter the radius of circle: ");
        double r = sc.nextDouble();
    System.out.println("Area of circle is: "+setArea(r));
    System.out.println("-----------------------------------");
    }

public class Tester


{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        int num=1;
    System.out.println("1.Triangle\n2.Circle\n3.Square\n4.Exit");
        while (num != 0)
        {
System.out.print("Choose from the above: ");
num = sc.nextInt();
            switch (num)
            {
                case 1:
                    Triangle obj1 = new Triangle();
                    obj1.getInputs();
                    break;
                case 2:
                    Circle obj2 = new Circle();
                    obj2.getInputs();
                    break;
                case 3:
                    Square obj3= new Square();
                    obj3.getInputs();
                    break;
                case 4:
                System.out.print("\nExit\n");
                System.exit(0);
                break;

                default:
                System.out.println("Invalid choice");
            }
        }

    }
}
Expected Output:

You might also like