SCAU Java-随堂实验4 矩形类的定义与封装

Main类
package main;

import shape.Rectangle;
import shape.Utility;

/**
 //    编写一个名为Main的主类,放在main包中。在主类中调用Utility类定义的方法实现如下功能:
 //            (1) 创建一个由10个矩形对象组成的数组,每个矩形的大小随机产生,颜色是默认值,矩形的宽度和高度的范围是[0,100)。
 //            (2) 输出这10个矩形;
 //            (3) 输出面积最大的矩形,输出格式是:[宽, 高] – 面积,均保留2位小数。
 //            (4) 对这10个矩形对象按面积进行降序排序;
 //            (5) 修改矩形的颜色为"RED";
 //            (6) 输出排序之后10个矩形。
 */

public class Main {
    public static void main(String[] args) {
        //创建一个由10个矩形对象组成的数组
        Rectangle[] rectangles = new Rectangle[10];
        for(int count = 0; count < rectangles.length; count++) rectangles[count] = new Rectangle(Math.random()*100,Math.random()*100);
        //输出这10个矩形;
        Utility.output(rectangles);
        //输出面积最大的矩形
        System.out.println();
        System.out.println("The rectangle with the largest area:");
        System.out.printf("[%.2f, %.2f] – %.2f\n\n", Utility.getMaxRectangle(rectangles).getWidth(),
                                                      Utility.getMaxRectangle(rectangles).getHeight(),
                                                      Utility.getMaxRectangle(rectangles).getArea());

        //对这10个矩形对象按面积进行降序排序
        Utility.sort(rectangles);
        //修改矩形的颜色为"RED"
        Rectangle.setColor("RED");
        //输出排序之后10个矩形
        Utility.output(rectangles);
    }
}

Rectangle类
package shape;

public class Rectangle {
    private double width;
    private double height;
    private static String color = "BLACK";

    public Rectangle(){}
    public Rectangle(double width, double height){
        this.width = width;
        this.height = height;
    }

    public double getArea(){
        return width*height;
    }

    public double getPermeter(){
        return 2*(width+height);
    }

    public double getWidth(){
        return width;
    }

    public void setWidth(double width){
        this.width = width;
    }

    public double getHeight(){
        return height;
    }

    public void setHeight(double height){
        this.height = height;
    }

    public static String getColor(){
        return color;
    }

    public static void setColor(String color){
        Rectangle.color = color;
    }
}

Utility类
package shape;

public class Utility {
    public static int compare(Rectangle rect1, Rectangle rect2){
        if(rect1.getArea() == rect2.getArea()) return 0;
        else return rect1.getArea()>rect2.getArea()?1:-1;
    }

    public static void sort(Rectangle[] rectangles){
        for (int i = 0; i < rectangles.length - 1; i++) {
            //依次的比较相邻两个数的大小,遍历一次后,把数组中第i小的数放在第i个位置上
            for (int j = 0; j < rectangles.length - 1 - i; j++) {
                // 比较相邻的元素,如果前面的数小于后面的数,就交换
                if (rectangles[j].getArea() < rectangles[j + 1].getArea()) {
                    Rectangle temp = rectangles[j + 1];
                    rectangles[j + 1] = rectangles[j];
                    rectangles[j] = temp;
                }
            }
        }
    }

    public static Rectangle getMaxRectangle(Rectangle[] rectangles){
        int max = 0;
        for(int i = 1; i < rectangles.length; i++){
            if(rectangles[max].getArea() < rectangles[i].getArea()) max = i;
        }
        return rectangles[max];
    }

    public static void output(Rectangle[] rectangles){
        System.out.println("共有"+rectangles.length+"个矩形对象,颜色是:"+ Rectangle.getColor());
        for (Rectangle rectangle : rectangles) {
            System.out.printf("[%.2f, %.2f] – %.2f\n", rectangle.getWidth(), rectangle.getHeight(), rectangle.getArea());
        }
    }



}

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值