Professional Documents
Culture Documents
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.
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("-----------------------------------");
}
default:
System.out.println("Invalid choice");
}
}
}
}
Expected Output: