Question

Java Lab

In this lab, you will be implementing the following class hierarchy. Your concrete classes must call the superclass constructor with the proper number of sides (which is constant for each concrete shape). The perimeter method is implemented in the superclass as it does not change based on the number of sides. The area method must be overridden in each subclass as the area is dependent on the type of polygon. The areas of an equilateral triangle, square, and a regular hexagon given side length a are 3 2 a and 3 3 2 a respectively. The superclass area method should just return 0 as a default value. You are also responsible for writing a small test driver that instantiates each of the concrete subclasses with test values (not necessarily user input), and prints the perimeter and area of each object to the console Regular Polygon num Sides int side Length int Regular Polygon (numSides int, sidelength: int) get NumSides(): int get Side Length(): int perimeter double area double Triangle Hexagon quare Triangle sideLength: int) Square sideLength: int) Hexagon side Length: int) area O: double area double area O: double

0 0
Add a comment Improve this question Transcribed image text
Answer #1

HI, Please find my implementation.

Please let me know in case of any issue.

public abstract class RegularPolygon {

   private int numSides;

   private int sideLength;

   public RegularPolygon(int numSides, int sideLength) {

       this.numSides = numSides;

       this.sideLength = sideLength;

   }

   public void setNumSides(int numSides) {

       this.numSides = numSides;

   }

   public int getNumSides() {

       return numSides;

   }

   public void setSideLengths(int sideLength) {

       this.sideLength = sideLength;

   }

   public int getSideLengths() {

       return sideLength;

   }

   public int perimeter() {

       return numSides*sideLength;

   }

  

   public abstract double area();

   public String toString() {

       String s = "I am a shape with " + numSides + " sides of length: ";

       s += "\nI am a polygon.";

       return s;

   }

}

class Hexagon extends RegularPolygon{

   public Hexagon(int sideLength) {

       super(6, sideLength);

   }

  

   public double area(){

       return 3*Math.pow(3, 1.0/3.0)*getSideLengths()*getSideLengths()/2.0;

   }

  

   public String toString() {

       String s = "I am a shape with " + getNumSides()+ " sides of length: ";

       s += "\nI am a Quadrilateral.";

       return s;

   }

}

///////////////////////////////////////////////////////////////////////////////

class Square extends RegularPolygon{

   public Square(int sideLength) {

       super(4, sideLength);

   }

   public double area(){

       return getSideLengths()*getSideLengths();

   }

   public String toString() {

       String s = "I am a shape with " + getNumSides()+ " sides of length: ";

       s += "\nI am a Square.";

       return s;

   }

}

////////////////////////////////////////////////////////////////////////////////////

// Triangle class

class Triangle extends RegularPolygon{

   public Triangle( int sideLength) {

       super(3, sideLength);

   }

   public double area(){

       return Math.pow(3, 1.0/3.0)*getSideLengths()*getSideLengths()/4.0;

   }

   public String toString() {

       String s = "I am a shape with " + getNumSides()+ " sides of length: ";

       s += "\nI am a Triangle.";

       return s;

   }

}

public class RegularPolygonTest{

   public static void main(String[] args) {

       RegularPolygon[] shapes = {

               new Square(3),

               new Triangle(6),

               new Triangle(7)

       };

       for(RegularPolygon p : shapes){

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

           System.out.println("Perimeter: "+ p.perimeter());

           System.out.println("Area: "+p.area());

           System.out.println();

       }

   }

}

/*

Sample run:

I am a shape with 4 sides of length:

I am a Square.

Perimeter: 12

Area: 9.0

I am a shape with 3 sides of length:

I am a Triangle.

Perimeter: 18

Area: 12.980246132766673

I am a shape with 3 sides of length:

I am a Triangle.

Perimeter: 21

Area: 17.66755723626575

*/

Add a comment
Know the answer?
Add Answer to:
Java Lab In this lab, you will be implementing the following class hierarchy. Your concrete classes...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • java language Problem 3: Shape (10 points) (Software Design) Create an abstract class that represents a Shape and contains abstract method: area and perimeter. Create concrete classes: Circle, Rectang...

    java language Problem 3: Shape (10 points) (Software Design) Create an abstract class that represents a Shape and contains abstract method: area and perimeter. Create concrete classes: Circle, Rectangle, Triangle which extend the Shape class and implements the abstract methods. A circle has a radius, a rectangle has width and height, and a triangle has three sides. Software Architecture: The Shape class is the abstract super class and must be instantiated by one of its concrete subclasses: Circle, Rectangle, or...

  • Java code for the following inheritance hierarchy figure.. 1. Create class Point, with two private instance...

    Java code for the following inheritance hierarchy figure.. 1. Create class Point, with two private instance variables x and y that represented for the coordinates for a point. Provide constructor for initialising two instance variables. Provide set and get methods for each instance variable, Provide toString method to return formatted string for a point coordinates. 2. Create class Circle, its inheritance from Point. Provide a integer private radius instance variable. Provide constructor to initialise the center coordinates and radius for...

  • JAVA: Quadrilateral Inheritance Hierarchy Write an inheritance hierarchy for classes Quadrilateral, Parallelogram, Rectangle, and Square. Use...

    JAVA: Quadrilateral Inheritance Hierarchy Write an inheritance hierarchy for classes Quadrilateral, Parallelogram, Rectangle, and Square. Use Quadrilateral as the superclass of the hierarchy. Create and use a Point class to represent the points in each shape. Make the hierarchy as deep as possible, i.e. more than two levels. Specify the instance variables and methods for each class. The private instance variables of Quadrilateral should be the x-y coordinate pairs for the four endpoints of the Quadrilateral. A program that instantiates...

  • In Java Which of the following statements declares Salaried as a subclass of payType? Public class...

    In Java Which of the following statements declares Salaried as a subclass of payType? Public class Salaried implements PayType Public class Salaried derivedFrom(payType) Public class PayType derives Salaried Public class Salaried extends PayType If a method in a subclass has the same signature as a method in the superclass, the subclass method overrides the superclass method. False True When a subclass overloads a superclass method………. Only the subclass method may be called with a subclass object Only the superclass method...

  • java. Question 3: The payment transaction scenario could be represented by the following hierarchy: Payments VISA...

    java. Question 3: The payment transaction scenario could be represented by the following hierarchy: Payments VISA MASTERCARD Paypal Create Payments as a superclass and VISA, MASTERCARD, PAYPAL as subclasses Create an interface PaymentsInterface with one method called payment Info. Create classes VISA, MASTERCARD, PAYPAL that implement Payments For each of the three classes (VISA, MASTERCARD, PAYPAL), create one constructor Constructors are used to create objects with initial balance with US dollars (USD). The interface class method "paymentinfo" should be overridden...

  • Purpose: The purpose of this lab is for you to design and implement several classes that...

    Purpose: The purpose of this lab is for you to design and implement several classes that use Inheritance. The problem: The program must handle a collection of different 2-dimensional shapes: triangles, rectangles and circles. All shapes have a color (use a Stringl and are either filled or not (use a boolean). All shapes must calculate and return their perimeter, and area. toStrina) for all shapes must implement the standardized formatting for inheritance. You are required to implement each of the...

  • N-Sided Polygon An n-sided polygon is a planed figure whose sides have the same length and...

    N-Sided Polygon An n-sided polygon is a planed figure whose sides have the same length and whose angles have the same degree. Write a class called NSidedPolygon that contains the following components: Private members to store the name of a polygon, the number of sides, and the length of each side. (You are expected to select the appropriate data types for each member.) A constructor that accepts the number of sides and the length of each side. A private method...

  • Problem 2 9.9 Geometry: n-sided regular polygon pg. 362 (25 points) Follow instructions as provided on...

    Problem 2 9.9 Geometry: n-sided regular polygon pg. 362 (25 points) Follow instructions as provided on page 362, reprinted below 9.9 (Geometry: n-sided regular polygon) In an n-sided regular polygon, all sides have the same length and all angles have the same degree (i.e., the polygon is both equilateral and equiangular) Design a class named RegularPolygon that contains A private int data field named n that defines the number of sides in the polygon with default value of 3 A...

  • We have introduced Abstract Classes and Interfaces in the lectures. In this lab, we would like...

    We have introduced Abstract Classes and Interfaces in the lectures. In this lab, we would like to provide few questions to review these topics and hands-on to solve them. Write a class named Octagon that extends GeometricObject and implements the Comparable and Cloneable interfaces. Assume that all eight sides of the octagon are of equal length. The area can be computed using the following formula: area = (2 + 4 Squareroot 2) * side * side Write a test program...

  • Java Please - Design and implement a class Triangle. A constructor should accept the lengths of...

    Java Please - Design and implement a class Triangle. A constructor should accept the lengths of a triangle’s 3 sides (as integers) and verify that the sum of any 2 sides is greater than the 3rd(i.e., that the 3 sides satisfy the triangle inequality). The constructor should mark the triangle as valid or invalid; do not throw an exception. Provide get and set methods for the 3 sides, and recheck for validity in the set methods. Provide a toString method...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT