2020-11-28

	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 getPerimeter(){
        return (width+height)*2;
    }

    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;
    }
}
package shape;

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

    public static void sort(Rectangle[] rectangles) {
        for(int i=1;i<rectangles.length;i++)
            for (int j=0;j<rectangles.length-1;j++){
                if(rectangles[j].getArea()<rectangles[j+1].getArea()){
                    double t1;
                    double t2;
                    t1 = rectangles[j].getHeight();
                    t2 = rectangles[j].getWidth();
                    rectangles[j].setWidth(rectangles[j+1].getWidth());
                    rectangles[j].setHeight(rectangles[j+1].getHeight());
                    rectangles[j+1].setHeight(t1);
                    rectangles[j+1].setWidth(t2);
                }
            }
    }

    public static Rectangle getMaxRectangle(Rectangle[] rectangles){
        double area = rectangles[0].getArea();
        int j = 0;
        for(int i=1;i<rectangles.length;i++){
            if(rectangles[i].getArea()>area){
                double t1;
                double t2;
                t1 = rectangles[i].getHeight();
                t2 = rectangles[i].getWidth();
                rectangles[i].setWidth(rectangles[j].getWidth());
                rectangles[i].setHeight(rectangles[j].getHeight());
                rectangles[j].setHeight(t1);
                rectangles[j].setWidth(t2);
                j=i;
            }
        }
        return rectangles[j];
    }

    public static void output(Rectangle[] rectangles){
        System.out.printf("共有%d个矩形对象,颜色是%s\n",rectangles.length,Rectangle.getColor());
        for(int i=0; i<rectangles.length;i++){
            System.out.printf("[%.2f,%.2f] - [%.2f]\n",rectangles[i].getWidth(),rectangles[i].getHeight(),rectangles[i].getArea());
        }

    }
}


package com.company;

import shape.Rectangle;
import shape.Utility;

public class Main {

    public static void main(String[] args) {
        Rectangle[] rec = new Rectangle[10];
        for(int i=0;i<10;i++){
            rec[i] = new Rectangle();
            rec[i].setWidth(Math.random()*100);
            rec[i].setHeight(Math.random()*100);
        }
        Utility u = new Utility();
        u.output(rec);
        u.sort(rec);
        Rectangle.setColor("RED");
        u.output(rec);
    }
}
上一篇:POJ 2559 Largest Rectangle in a Histogram


下一篇:C++友元和继承