intellij idea使用教程 vim复制 pdo nhibernate arduino xampp vb6 Fries vue路由 sketch up教程 pr序列设置哪个好 mysql数据库驱动 json转object java上传图片 js控制台打印 python练习题 python实例 python网络编程 input函数python python命令 python入门例子 python获取字典的值 python的lambda函数 python读取字典 javadate java替换字符串 java实例变量 java数组 java文件读写 java类方法 python 教程 customerrors din字体下载 ad19 矩阵分析与应用 collect dnf武极刷图加点 git命令 扫微信二维码诈骗原理 ABViewer
当前位置: 首页 > 学习教程  > 编程语言

SCAU 随堂实验5 正n多边形类的定义与使用

2020/12/5 9:54:51 文章标签:

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.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;
    }
}

本文链接: http://www.dtmao.cc/news_show_450112.shtml

附件下载

相关教程

    暂无相关的数据...

共有条评论 网友评论

验证码: 看不清楚?