You are on page 1of 11

NAME: SIDHANT KAUSHIK

REG. NO: 20MIS0184

ASSIGNMENT 1
QUESTION:
Create three shape classes - Rectangle, Square and Circle. Rectangle
has width and breadth, Square has side and Circle has radius as
member variables.
All the classes have getArea() method for implementation.
In the main method of FindSmallestShape, Create one object for
every class and compare their area's.
Print shape with smallest area and next smaller area.
Print shape with largest area and next larger area.
Print the areas of all these three shapes.
NAME: SIDHANT KAUSHIK
REG. NO: 20MIS0184
CODE:
import java.util.*;
class Rectangle
{
double breadth;
double width;
Rectangle(double breadth, double width)
{
this.breadth = breadth;
this.width = width;
}
double getArea()
{
return breadth * width;
}
}

class Square
{
double side;
Square(double side)
{
NAME: SIDHANT KAUSHIK
REG. NO: 20MIS0184
this.side = side;
}
double getArea()
{
return side * side;
}
}

class Circle
{
double radius;
Circle(double radius)
{
this.radius = radius;
}
double getArea()
{
return (22.0/7.0) * radius * radius;
}
}

public class FindSmallestShape


NAME: SIDHANT KAUSHIK
REG. NO: 20MIS0184
{
public static void main(String arg[])
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter the values...");
System.out.print("Enter breadth for rectangle- ");
double br= sc.nextDouble();
System.out.print("Enter width for rectangle- ");
double wi= sc.nextDouble();
System.out.print("Enter side for square- ");
double si= sc.nextDouble();
System.out.print("Enter radius for circle- ");
double ra= sc.nextDouble();

Rectangle r = new Rectangle(br,wi);


Square s = new Square(si);
Circle c = new Circle(ra);

if ((r.getArea() < c.getArea()) && (r.getArea() < s.getArea()))


{
System.out.println("\nRectangle has the smallest area.");
if(c.getArea() < s.getArea())
NAME: SIDHANT KAUSHIK
REG. NO: 20MIS0184
{
System.out.println("\nCircle has the next smaller area.");
}
else System.out.println("\nSquare has the next smaller area.");
}
else if( s.getArea() < c.getArea() )
{
System.out.println("\nSquare has the smallest area.");
if(r.getArea() < c.getArea())
{
System.out.println("\nRectangle has the next smaller area.");
}
else System.out.println("\nCircle has the next smaller area.");
}
else
{
System.out.println("\nCircle has the smallest area.");
if(r.getArea() < s.getArea())
{
System.out.println("\nRectangle has the next smaller area.");
}
else System.out.println("\nSquare has the next smaller area.");
NAME: SIDHANT KAUSHIK
REG. NO: 20MIS0184
}

System.out.println();

if ((r.getArea() > c.getArea()) && (r.getArea() > s.getArea()))


{
System.out.println("Rectangle has the largest area.");
if(c.getArea() > s.getArea())
{
System.out.println("\nCircle has the next larger area.");
}
else System.out.println("\nSquare has the next larger area.");
}
else if( s.getArea() > c.getArea() )
{
System.out.println("\nSquare has the largest area.");
if(r.getArea() > c.getArea())
{
System.out.println("\nRectangle has the next larger area.");
}
else System.out.println("\nCircle has the next larger area.");
}
NAME: SIDHANT KAUSHIK
REG. NO: 20MIS0184
else
{
System.out.println("\nCircle has the largest area.");
if(r.getArea() > s.getArea())
{
System.out.println("\nRectangle has the next largest area.");
}
else System.out.println("\nSquare has the next largest area.");
}

System.out.println();
System.out.println("Rectangle Area : " + r.getArea());
System.out.println("Square Area : " + s.getArea());
System.out.println("Circle Area : " + c.getArea());
System.out.println();
}
}
NAME: SIDHANT KAUSHIK
REG. NO: 20MIS0184
SNAPSHOT OF NOTEPAD:
NAME: SIDHANT KAUSHIK
REG. NO: 20MIS0184
NAME: SIDHANT KAUSHIK
REG. NO: 20MIS0184
NAME: SIDHANT KAUSHIK
REG. NO: 20MIS0184

SNAPSHOT OF OUTPUT:

You might also like