Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java: Write a set of classes to represent shapes in Java. You should have five c

ID: 660776 • Letter: J

Question

Java:

Write a set of classes to represent shapes in Java. You should have five classes and a tester program as described below.

1) PolyShape:

- This class represents polygons with an instance data variable int[] sideLengths

- This class should have a method getPerimeter() that takes no parameters and returns an integer.

2) Quadrilateral

-This class represents polygons with four sides

3) Rectangle

-This class should have a method getArea() that takes no parameters and returns an integer.

4)Square

5)Triangle

-This class represents polygons with three sides

-This class should have methods isIsoceles and isEquilateral that returns a boolean if two or three (respectively) sides are of equal length

You can assume integer-length sides.

When designing your classes:

-Think about what the parent-child relationship should be between these five classes.

-Think about what information should be sent as parameters and what information can be hard-coded (using constants) into a class.

-Move common code as high up in the hierarchy as possible.

-Use super whenever possible.

-Write appropriate constructors and getters and setters for each of the five classes.

-Write a toString method should return the number of sides, the side lengths, and all possible names for a shape.

For example, a square might print "I have four sides of length 3, 3, 3, and 3. I am a polygon. I am a quadrilateral. I am a rectangle. I am a square."

-Write an interactive driver program that allows the user to repeatedly create a shape by entering its dimensions. The driver program then prints the object created and invokes the perimeter, area (if applicable), and isIsoceles/isEquilateral (if applicable) methods.

Print out a text-based graphic of the shape created when the user creates a square or a rectangle. For example, a 4x5 rectangle might print:

*****
* *
* *
*****

Explanation / Answer

Program code:

//PolyShape.java

public class PolyShape

{

     int[] sideLengths;

     // constructor

     public PolyShape(int[] sides)

     {

          sideLengths = sides;

     }

     // toString method

     public String toString()

     {

          return "I am a polygon";

     }

     // getPerimeter method

     public int getPerimeter()

     {

          return 0;

     }

     // setter and getter methods of a class

     public int[] getSideLengths()

     {

          return sideLengths;

     }

     public void setSideLengths(int[] sideLengths)

     {

          this.sideLengths = sideLengths;

     }

}

//Quadrilateral.java

public class Quadrilateral extends PolyShape

{

     int side[];

     // constructor

     public Quadrilateral(int sides[])

     {

          super(sides);

          side = new int[4];

          side = sides;

     }

     // getPerimeter method

     public int getPerimeter()

     {

          int total = 0;

          for (int i = 0; i < side.length; i++)

          {

              total += side[i];

          }

          return total;

     }

     // toString method

     public String toString()

     {

          String s = "";

          s += super.toString();

          s += " I am a Quadrilateral";

          return s;

     }

     // setter and getter methods of a class

     public int[] getSide()

     {

          return side;

     }

     public void setSide(int[] side)

     {

          this.side = side;

     }

}

//Rectangle.java

public class Rectangle extends Quadrilateral

{

     int side[];

     // constructor

     Rectangle(int sides[])

     {

          super(sides);

          side = new int[4];

          side = sides;

     }

     // getPerimeter method

     public int getPerimeter()

     {

          int total = 0;

          for (int i = 0; i < side.length; i++)

          {

              total += side[i];

          }

          return total;

     }

     // getArea method

     public int getArea()

     {

          int area = 0;

          if (side[0] == side[1] && side[2] == side[3])

          {

              area = side[0] * side[2];

          }

          else if (side[0] == side[2] && side[1] == side[3])

              area = side[0] * side[1];

          else if (side[0] == side[3] && side[1] == side[2])

              area = side[0] * side[1];

          else

          {

              area = 0;

          }

          return area;

     }

     // toString method

     public String toString()

     {

          String s = "";

          s += super.toString();

          s += " I am a Rectangle";

          return s;

     }

     // display method

     public void display()

     {

          for (int i = 0; i < side[0]; i++)

          {

              for (int j = 0; j < side[1]; j++)

              {

                   if (i == 0 || i == side[0] - 1 || j == 0

                             || j == side[1] - 1)

                   {

                        System.out.print("*");

                   }

                   else

                   {

                        System.out.print(" ");

                   }

              }

              System.out.println();

          }

     }

     // setter and getter methods of a class

     public int[] getSide()

     {

          return side;

     }

     public void setSide(int[] side)

     {

          this.side = side;

     }

}

//Square.java

public class Square extends Rectangle

{

     int side[];

     // constructor

     Square(int sides[])

     {

          super(sides);

          side = new int[4];

          side = sides;

     }

     // getPerimeter method

     public int getPerimeter()

     {

          int total = 0;

          for (int i = 0; i < side.length; i++)

          {

              total += side[i];

          }

          return total;

     }

     // getArea method

     public int getArea()

     {

          int area = 0;

          area = side[0] * side[0];

          return area;

     }

     // toString method

     public String toString()

     {

          String s = "";

          s += super.toString();

          s += " I am a Square";

          return s;

     }

     // display method

     public void display()

     {

          for (int i = 0; i < side[0]; i++)

          {

              for (int j = 0; j < side[0]; j++)

              {

                   if (i == 0 || i == side[0] - 1 || j == 0

                             || j == side[0] - 1)

                   {

                        System.out.print("*");

                   }

                   else

                   {

                        System.out.print(" ");

                   }

              }

              System.out.println();

          }

     }

     // setter and getter methods of a class

     public int[] getSide()

     {

          return side;

     }

     public void setSide(int[] side)

     {

          this.side = side;

     }

}

//Triangle.java

public class Triangle extends PolyShape

{

     int side[];

     // constructor

     public Triangle(int sides[])

     {

          super(sides);

          side = new int[3];

          side = sides;

     }

     // getPerimeter method

     public int getPerimeter()

     {

          int total = 0;

          for (int i = 0; i < side.length; i++)

          {

              total += side[i];

          }

          return total;

     }

     // toString method

     public String toString()

     {

          String s = "";

          s += super.toString();

          s += " I am a Triangle";

          return s;

     }

     // setter and getter methods of a class

     public int[] getSide()

     {

          return side;

     }

     public void setSide(int[] side)

     {

          this.side = side;

     }

     // isIsosceles method

     public boolean isIsosceles()

     {

          if (side[0] == side[1] || side[1] == side[2] || side[2] == side[0])

              return true;

          else

              return false;

     }

     // isEquilateral method

     public boolean isEquilateral()

     {

          if (side[0] == side[1] && side[1] == side[2] && side[2] == side[0])

              return true;

          else

              return false;

     }

}

//ShapeImplementationClass.java driver class

//import the required packages

import java.util.*;

//define the class that contains the main method and two static methods

public class ShapeImplementationClass

{

     // static Scanner object to read input from the user

     static Scanner scan = new Scanner(System.in);

     // main method

     public static void main(String args[])

     {

          // declare the required variables

          int choice;

          int sides[];

          do

          {

              // display a menu

              System.out.println(" ************Menu************");

              System.out.println("1. Square");

              System.out.println("2. Rectangle");

              System.out.println("3. Quadrilateral");

              System.out.println("4. Triangle");

              System.out.println("5. Exit");

              // prompt the user to enter his choice

              System.out.println(" Select an option: ");

              choice = scan.nextInt();

              // depending on the user choice make the switch statement

              // to go to respective case block

              switch (choice)

              {

                   // square code

                   case 1:

                        sides = new int[4];

                        sides = getValues(sides, 4);

                        Square s = new Square(sides);

                        System.out.println("The area of square is "+ s.getArea());

                        System.out.println("The perimeter of square is " + s.getPerimeter());

                        printDetails(sides, sides.length);

                        System.out.println();

                        System.out.println(s.toString());

                        s.display();

                        break;

                   // rectangle code

                   case 2:

                        sides = new int[4];

                        sides = getValues(sides, 4);

                        Rectangle r = new Rectangle(sides);

                        System.out.println("The area of rectangle is "+ r.getArea());

                        System.out.println("The perimeter of rectangle is "+ r.getPerimeter());

                        printDetails(sides, sides.length);

                        System.out.println();

                        System.out.println(r.toString());

                        r.display();

                        break;

                   // quadrilateral code

                   case 3:

                        sides = new int[4];

                        sides = getValues(sides, 4);

                        Quadrilateral q = new Quadrilateral(sides);

                        System.out.println("The perimeter of quadrilateral is "+ q.getPerimeter());

                        printDetails(sides, sides.length);

                        System.out.println();

                        System.out.println(q.toString());

                        break;

                   // triangle code

                   case 4:

                        sides = new int[3];

                        sides = getValues(sides, 3);

                        Triangle t = new Triangle(sides);

                        System.out.println("The perimeter of triangle is "+ t.getPerimeter());

                        System.out.println("Triangle is a equilateral triangle:(0-true/1-false) "+ t.isEquilateral());

                        System.out.println("Triangle is a isosceles triangle:(0-true/1-false) " + t.isIsosceles());

                        printDetails(sides, sides.length);

                        System.out.println();

                        System.out.println(t.toString());

                        break;

                   // exit code

                   case 5:

                        System.exit(0);

                        break;

                   default:

                        System.out.println("You have entered wrong option! Try again!");

              }

          } while (true);

     }

     // getValues method gets the input from the user and returns the array

     public static int[] getValues(int arr[], int size)

     {

          System.out.println("Enter the value of sides of the shape: ");

          for (int i = 0; i < size; i++)

          {

              System.out.print("Enter value of side " + (i + 1) + ": ");

              arr[i] = scan.nextInt();

          }

          return arr;

     }

     // printDetails method prints the respective print statement

     public static void printDetails(int a[], int size)

     {

          if (size == 4)

          {

              System.out.print("I have four sides of length ");

          }

          else

          {

              System.out.print("I have three sides of length ");

          }

          for (int i = 0; i < size; i++)

          {

              if (i < size - 1)

                   System.out.print(a[i] + ", ");

              else

                   System.out.println("and " + a[i]+ ". ");

          }

     }

}

Sample Output:

************Menu************

1. Square

2. Rectangle

3. Quadrilateral

4. Triangle

5. Exit

Select an option:

1

Enter the value of sides of the shape:

Enter value of side 1: 2

Enter value of side 2: 2

Enter value of side 3: 2

Enter value of side 4: 2

The area of square is 4

The perimeter of square is 8

I have four sides of length 2, 2, 2, and 2

I am a polygon

I am a Quadrilateral

I am a Rectangle

I am a Square

**

**

************Menu************

1. Square

2. Rectangle

3. Quadrilateral

4. Triangle

5. Exit

Select an option:

2

Enter the value of sides of the shape:

Enter value of side 1: 3

Enter value of side 2: 4

Enter value of side 3: 3

Enter value of side 4: 4

The area of rectangle is 12

The perimeter of rectangle is 14

I have four sides of length 3, 4, 3, and 4

I am a polygon

I am a Quadrilateral

I am a Rectangle

****

* *

****

************Menu************

1. Square

2. Rectangle

3. Quadrilateral

4. Triangle

5. Exit

Select an option:

3

Enter the value of sides of the shape:

Enter value of side 1: 3

Enter value of side 2: 4

Enter value of side 3: 5

Enter value of side 4: 6

The perimeter of quadrilateral is 18

I have four sides of length 3, 4, 5, and 6.

I am a polygon

I am a Quadrilateral

************Menu************

1. Square

2. Rectangle

3. Quadrilateral

4. Triangle

5. Exit

Select an option:

4

Enter the value of sides of the shape:

Enter value of side 1: 3

Enter value of side 2: 3

Enter value of side 3: 1

The perimeter of triangle is 7

Triangle is a equilateral triangle:(0-true/1-false) false

Triangle is a isosceles triangle:(0-true/1-false) true

I have three sides of length 3, 3, and 1.

I am a polygon

I am a Triangle

************Menu************

1. Square

2. Rectangle

3. Quadrilateral

4. Triangle

5. Exit

Select an option:

5

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote