Java: this is all one question Question #1 Geometry: n-sided regular polygon) An
ID: 3882142 • Letter: J
Question
Java: this is all one question Question #1 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 class named Regularpolygon that contains: 50 points . A private int data field named that defines the number of sides in the polygon with default value 3 . A private double data field naned side that stores the length of the side with default value 1 e A private double data field naned x that defines the x-coordinate of the center of the polygon with default value 0 * A private double data field naned y that defines the y-coordinate of the center of the polygon .A no-arg constructor that creates a regular polygon with default values. . A constructor that creates a regular polygon with the specified number of sides and the length with default value of side, and centered at (2, . A constructor that creates a regular polygon with the specified number of sides, the length of side, and x-and y-coordinates · The accessor and mutator methods tor an data fields. The method getPerimter() that returns the perimeter of the polygon. The perimeter is the addition ot the length of all the sides of the polygon. * The method getAreal) that returns the area of the polygon. The formula for computing the area of a regular polygon is Area , use the Math, tan (double) method which also returns a double 25 points Create a testing class which wil1 create two Regularpolygon objects and if the two objects are the same display then display a message stating both objects contain the same information. If the two objects are not the same then copy all the intormation from the second object into the first one Do not assign the objects. You need to copy all the intormation from second object into the tirst 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 Create 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 25 points Perimeter . Area . Type of Polygon Type Polygon is based on the number of sides Sides Type Triangle Quadrilateral Pentagon Hexagon Heptagon OctagonExplanation / 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
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.