A private int data field named n that defines the number of sides in the polygon
ID: 3624768 • Letter: A
Question
A private 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 named x that denes the x-coordinate of the center of the polygon with default value 0.
A private double data field named y that defines the y-coordinate of the center of the polygon with default value 0.
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 length of side, centered at (0,0).
A constructor that creates a regular polygon with the specified number of sides, length of side, and x- and y-coordinates.
The accessor and mutator methods for all data fields.
The method getPerimeter() that returns the perimeter of the polygon.
Explanation / Answer
public class RegularPolygon
{
// A private int data field named n that defines the number of sides in the polygon with default value 3.
private int n;
// A private double data field named side that stores the length of the side with default value 1.
private double side;
// A private double data field named x that denes the x-coordinate of the center of the polygon with default value 0.
private double x;
// A private double data field named y that defines the y-coordinate of the center of the polygon with default value 0.
private double y;
// A no-arg constructor that creates a regular polygon with default values.
public RegularPolygon()
{
n = 3;
side = 1;
x = y = 0;
}
// A constructor that creates a regular polygon with the specified number of sides and length of side, centered at (0,0).
public RegularPolygon(int n, double side)
{
this.n = n;
this.side = side;
x = y = 0;
}
// A constructor that creates a regular polygon with the specified number of sides, length of side, and x- and y-coordinates.
public RegularPolygon(int n, double side, double x, double y)
{
this.n = n;
this.side = side;
this.x = x;
this.y = y;
}
// The accessor and mutator methods for all data fields.
public void setN(int n)
{
this.n = n;
}
public int getN()
{
return n;
}
public void setSide(double side)
{
this.side = side;
}
public double getSide()
{
return side;
}
public void setX(double x)
{
this.x = x;
}
public double getX()
{
return x;
}
public void setY()
{
this.y = y;
}
public double getY()
{
return y;
}
// The method getPerimeter() that returns the perimeter of the polygon.
public double getPerimeter()
{
return n*side;
}
// The method getArea() that returns the area of the polygon.
public double getArea()
{
return n*s*s/ (4*Math.tan(Math.PI));
}
}
// Write a test program (i.e. main method) that creates three RegularPolygon objects, created using the no-arg constructor, using RegularPolygon( 6, 4 ), and using RegularPolygon(10, 4, 5.6, 7.8 ). For each object, display its perimeter and area.
public static void main(String[] args)
{
RegularPolygon test1 = new RegularPolygon();
RegularPolygon test2 = new RegularPolygon(6, 4);
RegularPolygon test3 = new RegularPolygon(10, 4, 5.6, 7.8);
System.out.println(test1.getPerimeter()+" "+test1.getArea();
System.out.println(test2.getPerimeter()+" "+test2.getArea();
System.out.println(test3.getPerimeter()+" "+test3.getArea();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.