uestion #1 50 points (Geometry: n-sided regular polygon) An n-sided regular poly
ID: 3882123 • Letter: U
Question
uestion #1 50 points (Geometry: n-sided 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 . A private int data field named n that defines the number of sides in the polygon vith default A private double data tield named x that defines the x-coordinate of the center of the polygon A private double data tield named y that defines the y-coordinate of the center of the polygon .A constructor that creates a regular polygon with the specified number of sides and the length class named Regularpolygon that contains: value 3 . A private double data field named side that stores the length of the side with default value 1. with default value o with default value 0 .A no-arg constructor that creates a regular polygon with default values. of side, and centered at (2. side, and x-and y-coordinates The method getPerinter) that returns the perimeter of the polygon. The perineter is the . A conatructor that creates a regular polygon with the specified number of sides, the length o The accessor and mutator methods for all data fields. addition of the length of all the sides of the polygon The method getAreal) that returns the area of the polygon. The formula for comput ing the area of a regular polygon is Area- . Use the Math. tan (double) method hieh also returns a 4×tan( ) double 25 points Question #2 Create a testing class which will create two Regularpolygon objects and if the two objects are the same display then display a nessage stating both objects contain the same information. It the two objects are not the same then copy all the information from the second object into the first one Do not assign the objects. You need to copy all the information from second object into the tirst one. You will need to create two more methods in the Regularpolygon class equals checks if the information of two objects are the same copy - copies the information of one object into another Question #3 25 points a testing class which will create 5 RegularPolygon objects. Set the side with a random number from 2 to 10 and set n with a random number from 3 to 8. Display the following for each polygon: e Perimeter Area Type of Polygon Type Polygon is based on the number of sides Sides Type Triangle Quadrilateral Pentagon Hexagorn Heptagon ctagonExplanation / Answer
Given below is the code for the question. Please do rate the answer if helpful. Thank you.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Answer 1:
RegularPolygon.java
public class RegularPolygon {
private int n;
private double side;
private double x, y;
RegularPolygon()
{
x = 0;
y = 0;
side = 1;
n = 3;
}
RegularPolygon(int numSides, double sideLen)
{
n = numSides;
side = sideLen;
x = 0;
y = 0;
}
RegularPolygon(int numSides, double sideLen, double xCor, double yCor)
{
n = numSides;
side = sideLen;
x =xCor;
y = yCor;
}
public int getN() {
return n;
}
public void setN(int n) {
this.n = n;
}
public double getSide() {
return side;
}
public void setSide(double side) {
this.side = side;
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public double getPerimeter()
{
return n * side;
}
public double getArea()
{
return (n*side *side) / (4 * Math.tan(Math.PI/n));
}
public boolean equals(RegularPolygon other)
{
if(n == other.n && side == other.side && x == other.x && y == other.y)
return true;
else
return false;
}
//will copy the values from passed parameter into this object
public void copy(RegularPolygon source)
{
n = source.n;
side = source.side;
x = source.x;
y = source.y;
}
}
------------------------------------------------------------------------------------------------
Answer 2:
=========
PolyTest1.java
public class PolyTest1 {
public static void main(String[] args) {
RegularPolygon p1 = new RegularPolygon(3, 5);
RegularPolygon p2 = new RegularPolygon(4, 10);
System.out.println("polygon p1: n = "+ p1.getN() + ", side = " + p1.getSide()
+ ", area = " + p1.getArea() + ", perimeter = " + p1.getPerimeter());
System.out.println("polygon p2: n = "+ p2.getN() + ", side = " + p2.getSide()
+ ", area = " + p2.getArea() + ", perimeter = " + p2.getPerimeter());
if(p1.equals(p2))
System.out.println("Both the polygons are same");
else
{
System.out.println("The polygons are not same. Copying second polygon into first");
p1.copy(p2);
System.out.println("polygon p1: n = "+ p1.getN() + ", side = " + p1.getSide()
+ ", area = " + p1.getArea() + ", perimeter = " + p1.getPerimeter());
System.out.println("polygon p2: n = "+ p2.getN() + ", side = " + p2.getSide()
+ ", area = " + p2.getArea() + ", perimeter = " + p2.getPerimeter());
}
}
}
output
======
polygon p1: n = 3, side = 5.0, area = 10.825317547305486, perimeter = 15.0
polygon p2: n = 4, side = 10.0, area = 100.00000000000001, perimeter = 40.0
The polygons are not same. Copying second polygon into first
polygon p1: n = 4, side = 10.0, area = 100.00000000000001, perimeter = 40.0
polygon p2: n = 4, side = 10.0, area = 100.00000000000001, perimeter = 40.0
------------------------------------------------------------------------------------------------
Answer 3: Using arrays to hold the 5 polygon objects.
=================
PolyTest2.java
=======
public class PolyTest2
{
public static void main(String[] args) {
RegularPolygon[] p = new RegularPolygon[5];
for(int i = 0; i < 5; i++)
{
p[i] = new RegularPolygon((int)(3 + Math.random() * 6), (int)(2 + Math.random() * 9));
}
for(int i = 0; i < 5; i++)
{
System.out.println("Polygon " + (i+1));
System.out.println("N: " + p[i].getN());
System.out.println("Side: " + p[i].getSide());
System.out.println("Area: " + p[i].getArea());
System.out.println("Perimeter: " + p[i].getPerimeter());
int n = p[i].getN();
if(n == 3)
System.out.println("Type: Triangle");
else if(n == 4)
System.out.println("Type: Quadrilateral");
else if(n == 5)
System.out.println("Type: Pentagon");
else if(n == 6)
System.out.println("Type: Hexagon");
else if(n == 7)
System.out.println("Type: Heptagon");
else if(n == 8)
System.out.println("Type: Octagon");
System.out.println();
}
}
}
output
========
Polygon 1
N: 8
Side: 5.0
Area: 120.71067811865476
Perimeter: 40.0
Type: Octagon
Polygon 2
N: 5
Side: 2.0
Area: 6.881909602355868
Perimeter: 10.0
Type: Pentagon
Polygon 3
N: 5
Side: 4.0
Area: 27.52763840942347
Perimeter: 20.0
Type: Pentagon
Polygon 4
N: 4
Side: 8.0
Area: 64.00000000000001
Perimeter: 32.0
Type: Quadrilateral
Polygon 5
N: 6
Side: 2.0
Area: 10.392304845413264
Perimeter: 12.0
Type: Hexagon
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.