Question

Write a simple class representing a Polygon up to 6 sides: the Polygon class has 5...

Write a simple class representing a Polygon up to 6 sides: the Polygon class has 5 fields: Regular, sides, sideLength, angle, apothem

Polygon:
Regular: boolean
sides: int
sideLength: double
angle : double
apothem : double

  • Provide a no-arg constructor for this class initializes the object and sets the instance variables to:
    Regular: true
    sides: 3
    sideLength: 1
    angle : 60
    apothem : SquareRoot(3)/2
  • Provide the following methods:
  • Set number of sides
  • set the length of the sides
  • set the angle of the polygon
  • set the length of the apothem
  • find the apothem
  • determine the type of polygon by evaluating the number of sides
  • Calculate the area of the Polygon based on the number of sides
  • calculate the perimeter
  • Equal to : To compare the Polygon to another ( returning true or false)
  • display Polygon information as a string of data
  • Provide few cases where a class is initialized, using a constructor
  • Provide few cases where these methods are called.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Polygon class


public class Polygon {
private boolean Regular;
private int sides;
private double sideLength,angle,apothem;

public Polygon()
{
this.Regular=true;
this.sides= 3;
this.sideLength= 1;
this.angle = 60;
this.apothem = Math.sqrt(3)/2;
}

public void setSides(int sides) {
this.sides = sides;
}

public void setSideLength(double sideLength) {
this.sideLength = sideLength;
}

public void setAngle(double angle) {
this.angle = angle;
}

public void setApothem(double apothem) {
this.apothem = apothem;
}
public void findApothem()
{
this.apothem = Math.sqrt(sides)/2;
}
public String getType()
{
if(sides==3)
return "Triangle";
else if(sides==4)
return "Recangle";
else if(sides==5)
return "Pentagon";
else
return "Hexagon";
  
}
public double getPerimeter()
{
return sides*sideLength;
}

@Override
public int hashCode() {
int hash = 7;
return hash;
}

@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Polygon other = (Polygon) obj;
if (this.Regular != other.Regular) {
return false;
}
if (this.sides != other.sides) {
return false;
}
if (Double.doubleToLongBits(this.sideLength) != Double.doubleToLongBits(other.sideLength)) {
return false;
}
if (Double.doubleToLongBits(this.angle) != Double.doubleToLongBits(other.angle)) {
return false;
}
if (Double.doubleToLongBits(this.apothem) != Double.doubleToLongBits(other.apothem)) {
return false;
}
return true;
}
  
@Override
public String toString() {
return "\nThe Polygon is : "+getType()+ "\nsides=" + sides + ",\nSide Length = " + sideLength + ",\nAngle = " + angle + "\nApothem=" + apothem + "\nPerimeter: "+getPerimeter();
}
  
}

Main..java driver


public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Polygon p1=new Polygon();
Polygon p2=new Polygon();
p2.setAngle(60);
p2.setSideLength(20);
p2.setSides(6);
p2.findApothem();
System.out.println(p1);
System.out.println(p2);
System.out.println("\nIf p1 and p2 are equals: "+p1.equals(p2));
}
  
}

output

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.

Add a comment
Know the answer?
Add Answer to:
Write a simple class representing a Polygon up to 6 sides: the Polygon class has 5...
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
  • //include comments Opts (Regular polygon) An n-sided regular polygon has n sides of the same length...

    //include comments Opts (Regular polygon) An n-sided regular polygon has n sides of 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: Aprivate int data field named n that defines the number of sides in the polygon with default value 3. A private double data field named side that stores the length of the side with default value 1. A private double data field...

  • 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...

  • (Geometry: n-sided regular polygon) An n-sided regular polygon’s sides all have the same length and all...

    (Geometry: n-sided regular polygon) An n-sided regular polygon’s sides all have the same length and all of its 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. ■ A private float data field named side that stores the length of the side. ■ A private float data field named x that defines...

  • Java Lab In this lab, you will be implementing the following class hierarchy. Your concrete classes...

    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...

  • Study smarter, not harder.

    this program is in java (eclipse). CSIT 210 n/bbcswebdav/pid-8544435-dt-content-rid-37523491_1 /courses/CSI T2 10.2 1 662.20192 1/Lab%206.pdf bat. Import bookmarks now Lab 6: Module 5- Classes Account Class (15 pts) Design a class Account and the driver class AccountDemo. The class is based on a bank. 1. The attributes of Account class are as follows: (1 pt) a. long id;I should be a default of -1 b. double balance; I/ should be a default of $5 c. double interestRate; W should be...

  • 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...

  • Programming Language: Java Write a class named ColoredRectangle that extends the Rectangle class (given below). The...

    Programming Language: Java Write a class named ColoredRectangle that extends the Rectangle class (given below). The ColoredRectangle class will have an additional private String field named Color. The ColoredRectangle class should have four constructors: a no-arg constructor; a three-arg constructor; a two-arg constructor that accepts a Rectangle object and a color; and a copy constructor. The ColoredRectangle class should have an Equals and toString methods. The ColoredRectangle class should have mutators and accessors for all three fields. public class Rectangle...

  • Using separate files, write the definition for a class called Point and a class called Circle. Th...

    Using separate files, write the definition for a class called Point and a class called Circle. The class point has the data members x (double) and y (double) for the x and y coordinates. The class has the following member functions: a) void set_x(double) to set the x data member b) void set_y(double) to set the y data member c) double get_x() to return the the x data member d) double get_y() to return the the y data member e)...

  • ****Here is the assignment **** Purpose: To write an Object-Oriented application that creates a Java class...

    ****Here is the assignment **** Purpose: To write an Object-Oriented application that creates a Java class with several instance variables, a constructor to initialize the instance variables, several methods to access and update the instance variables’ values, along with other methods to perform calculations. Also, write a test class that instantiates the first class and tests the class’s constructor and methods. Details: Create a class called Rectangle containing the following: Two instance variables, An instance variable of type double used...

  • U8ing separate files, write th definition for a clas called Poit and a class called Cirele...

    U8ing separate files, write th definition for a clas called Poit and a class called Cirele 1. The class point has the data members (double) and y (double) for the x and y coordinates The class has the following ember functions a) void set x (double) to set the x data member b) void set y(double) to set the y data member c) double get-x() to return th the data member d) double get yO to zeturn the the y...

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