Java: Write a test program to create three RegularPolygon objects using the parameterless construction method, RegularPolygon (6,4) and RegularPolygon (10,4,5.6,7.8). Show each object

(Geometry: regular n polygon) In a regular n polygon, all sides have the same length and all angles have the same degree (that is, the polygon is equilateral and equiangular).
Design a class called RegularPolygon, which includes:
● An int private data field named n defines the number of sides of the polygon, and the default value is 3.
● A double-type private data field named side stores the length of the side. The default value is 1.
● A double private data field named x defines the x coordinate of the midpoint of the polygon. The default value is 0.
● A double-type private data field named y defines the y coordinate of the midpoint of the polygon. The default value is 0.
● A parameterless construction method for creating regular polygons with default values.
● A construction method that can create regular polygons with a specified number of sides and lengths, centered at (0,0).
● A construction method that can create a regular polygon with the specified number of sides and length and center in (x, y).
● Accessors and modifiers for all data fields.
● A method to return the perimeter of the polygon getPerimeter (.
● A method to return the area of ​​the polygon getArea (). The formula for calculating the area of ​​the regular polygon is: (n s ^ 2) / (4 tan (PI / n))
Write a test program , Use the parameterless construction method, RegularPolygon (6,4) and RegularPolygon (10,4,5.6,7.8) to create three RegularPolygon objects. Show the perimeter and area of ​​each object.

Code:
class RegularPolygon {
private int n = 3;
private double side = 1;
private double x = 0;
private double y = 0;
// The following is the construction method (use method overloading)
RegularPolygon () {};
RegularPolygon ( int newn, int news) {
n = newn;
side = news;
x = 0;
y = 0;
};
RegularPolygon (int newn, int news, double newx, double newy) {
n = newn;
side = news;
x = newx;
y = newy;
};
// The following are the accessors and modifiers of all data fields
public void setn (int newn) {
n = newn;
};
public void setside (double news) {
side = news;
};
public void setx (double newx) {
x = newx;
};
public void sety(double newy){
y=newy;
};
public int getn(){
return n;
};
public double getside(){
return side;
};
public double getx(){
return x;
};
public double gety(){
return y;
};
public double getPerimeter(){
return nside;
};
public double getArea(){
double p=3.141592535;
return (n
sideside)/(4Math.tan(p/n));
};
}
public class test{
public static void main(String[] args){
RegularPolygon r1=new RegularPolygon();
System.out.println ("Perimeter of r1 =" + r1.getPerimeter () + "Area of ​​r1 =" + r1.getArea ());
RegularPolygon r2 = new RegularPolygon (6,4);
System.out.println ("Perimeter of r2 =" + r2.getPerimeter () + "Area of ​​r2 =" + r2.getArea ());
RegularPolygon r3 = new RegularPolygon (10,4,5.6,7.8);
System.out.println ( "Perimeter of r3 =" + r3.getPerimeter () + "Area of ​​r3 =" + r3.getArea ());
}
}

This article is original, please indicate the source.
If it helps you, please give me a thumbs up!

Published 9 original articles · won 9 · visited 2165

Guess you like

Origin blog.csdn.net/grandniu/article/details/105253736