JAVA Objective This lab will test your knowledge of equals
JAVA
Objective
This lab will test your knowledge of equals() , setter and getter methods. The program does not take any user input. We
will create a program that will allow us to create Shape objects that can be a RECTANGLE, CIRCLE, or TRIANGLE.
Your Task is: Create a project called ShapeChecker . It will contain 2 classes; the main class ShapeChecker, and another class, Shape. 1. Create the class called Shape
described below: It will contain 4 instance variables: private String type
- The type of shape. It can be “RECTANGLE”, “CIRCLE”, or “TRIANGLE” private double base
- The base of the shape (if any) private double height
- The height of the shape (if any) private double diameter
- The diameter of the shape (if any)
2. Setters and Getters: Create getters and setters for each instance variable –The setters for base and height should only
allow the respective variable to be set if the type was previously set to “RECTANGLE” or“TRIANGLE”. They should print
an error message otherwise. The setter for diameter should only allow the respective variable to be set if type was previously set to “CIRCLE”. It should print an error message otherwise.3. Create the following 2 methods: public double getArea() Return the area of the shape based on the type:•Rectangle: Base * Height • Triangle: 1/2 Base * Height • Circle: PI * (1/2 Diameter) 2 public boolean equals(Shape otherShape) Returns true if the areas of the shapes are within 50 units of each other
(for example, if shape1.getArea() returns 110, and shape2. getArea() returns 115, these should be considered equal, regardless of what the shape type is. It should use the getArea() methods of the shape objects when comparing their areas.
In the main method of
ShapeChecker
do the following:
1. Create 3 objects of type Shape, shape1, shape2, and shape3.
2. Set the following values for each shape object:
1. shape1:
type = “RECTANGLE”
base = 50
height = 20
2. shape2:
type = “TRIANGLE”
base
= 80
height = 24
3. shape3:
type = “CIRCLE”
diameter = 36
3. Using the
equals()
method of shape1, check if it is equal to shape2 and print a message saying
if it is or is not
4. Do the same for shape2 and shape3, and shape1 and shape3
Solution
//ShapeType.java
 public enum ShapeType
 {
    RECTANGLE,TRIANGLE,CIRCLE
 }
------------------------------------------------
 //Shape.java
 public class Shape
 {
   
    //instance variables of class
    private ShapeType type;
    private double base;
    private double height;
    private double diamter;
   
    //default constructor
    public Shape() {
        // TODO Auto-generated constructor stub
    }
   
    //Set type
    public void setType(ShapeType type)
    {
        this.type=type;
    }
   
    //Set base
    public void setBase(double base)
    {
        if(type==ShapeType.RECTANGLE ||type==ShapeType.TRIANGLE)
            this.base=base;
        else
            System.out.println(\"Invalid type\");
    }
   
    //Set height
    public void setHeight(double height)
    {
        if(type==ShapeType.RECTANGLE ||type==ShapeType.TRIANGLE)
            this.height=height;
        else
            System.out.println(\"Invalid type\");
    }
   
    //Set diameter
    public void setDiamter(double diameter)
    {
        if(type==ShapeType.CIRCLE)
            this.diamter=diameter;
        else
            System.out.println(\"Invalid type\");
    }
   
    //Return area
    public double getArea()
    {
        double area=0;
        if(type==ShapeType.RECTANGLE)
            area=base*height;
        else if(type==ShapeType.TRIANGLE)
            area=0.5*base*height;
        else if(type==ShapeType.CIRCLE)
            area=Math.PI*Math.pow(1/diamter,2);
       
        return area;
       
    }
    //return true if difference of areas of the shape objects within 50 units
    public boolean equals(Shape otherShape)
    {      
        return Math.abs(getArea()-otherShape.getArea())<50? true:false;  
    }
 }//end of the class
------------------------------------------------
//ShapeChecker.java
 public class ShapeChecker {
    public static void main(String[] args) {
      
        //Create an instance of Shape1 and set type as RECTANGLE
        Shape s1=new Shape();
        s1.setType(ShapeType.RECTANGLE);
        s1.setBase(50);
        s1.setHeight(20);
        //Create an instance of Shape2 and set type as TRIANGLE
        Shape s2=new Shape();
        s1.setType(ShapeType.TRIANGLE);
        s1.setBase(80);
        s1.setHeight(24);
        //Create an instance of Shape3 and set type as CIRCLE
        Shape s3=new Shape();
        s1.setType(ShapeType.CIRCLE);
        s1.setDiamter(36);
       
        //checking of s1 and s2 are equal
        if(s1.equals(s2))
            System.out.println(\"s1 and s2 are equal\");
        else
            System.out.println(\"s1 and s2 are not equal\");
       
        //checking of s2 and s3 are equal
        if(s2.equals(s3))
            System.out.println(\"s2 and s3 are equal\");
        else
            System.out.println(\"s2 and s3 are not equal\");
       
        //checking of s1 and s3 are equal
        if(s1.equals(s3))
            System.out.println(\"s1 and s3 are equal\");
        else
            System.out.println(\"s1 and s3 are not equal\");      
    }
 }
------------------------------------------------
Sample Ouptut:
s1 and s2 are equal
 s2 and s3 are equal
 s1 and s3 are equal




