SCAU 随堂实验5 正n多边形类的定义与使用
作者:互联网
Main
package exp03.main;
import exp03.shape.RegularPolygon;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
System.out.println("输入需要创建的多边形对象的个数n:");
int n = cin.nextInt();
System.out.println("依次输入每个多边形的:边数、边长度、中心坐标,并创建多边形对象:");
RegularPolygon[] Re = new RegularPolygon[n];
for(int i=0;i<n;i++){
int NumberOfSides = cin.nextInt();
double LengthOfSide = cin.nextDouble();
double x = cin.nextDouble();
double y = cin.nextDouble();
Re[i] = new RegularPolygon(NumberOfSides,LengthOfSide,x,y);
System.out.println(Re[i].toString());
}
double maxArea = Re[0].getArea();
int result = 0;
for(int i=1;i<n;i++){
if(Re[i].getArea()>maxArea){
result = i;
maxArea = Re[i].getArea();
}
}
System.out.println(Re[result].toString()+" 并且最大面积为:"+Re[result].getArea());
double miniDistance = Re[0].getDistance();
result = 0;
for(int i=1;i<n;i++){
if(Re[i].getDistance()<miniDistance){
result = i;
miniDistance = Re[i].getDistance();
}
}
System.out.println(Re[result].toString()+" 并最小距离为:"+Re[result].getDistance());
}
}
RegularPolygon
package exp03.shape;
public class RegularPolygon {
private int numberOfSides = 3;
private double lengthOfSide = 1.0;
private double x = 0.0;
private double y = 0.0;
public int getNumberOfSides() {
return numberOfSides;
}
public void setNumberOfSides(int numberOfSides) {
this.numberOfSides = numberOfSides;
}
public double getLengthOfSide() {
return lengthOfSide;
}
public void setLengthOfSide(double lengthOfSide) {
this.lengthOfSide = lengthOfSide;
}
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 RegularPolygon(){
}
public RegularPolygon(int numberOfSides, int lengthOfSide){
this.lengthOfSide = lengthOfSide;
this.numberOfSides = numberOfSides;
}
public RegularPolygon(int numberOfSides,double lengthOfSide, double x, double y){
this.numberOfSides = numberOfSides;
this.lengthOfSide = lengthOfSide;
this.x = x;
this.y = y;
}
public double getArea(){
return (numberOfSides*lengthOfSide*lengthOfSide)/4.0*Math.tan(Math.PI/numberOfSides);
}
public double getDistance(){
return Math.sqrt(x*x+y*y);
}
@Override
public String toString(){
return "["+numberOfSides+","+lengthOfSide+"]@("+x+","+y+")";
}
@Override
public boolean equals(Object O){
if(O == null) return false;
if(this.getClass()!= O.getClass()) return false;
RegularPolygon r = (RegularPolygon) O;
if(this.x!=r.x || this.y!=r.y||this.lengthOfSide!=r.lengthOfSide||this.numberOfSides!=r.numberOfSides)
return false;
return true;
}
@Override
public int hashCode(){
int result = hashCode();
if(result == 0){
result = 17;
result = 31*result + (int)this.x;
result = 31*result + (int)this.y;
result = 31*result + (int)this.lengthOfSide;
result = 31*result + this.numberOfSides;
}
return result;
}
}
标签:多边形,int,double,随堂,lengthOfSide,numberOfSides,result,SCAU,public 来源: https://blog.csdn.net/Maoxim/article/details/110677244