一、实验目的
(1)理解对象和类,掌握用类创建对象模型。
(2)理解和掌握数据域封装,可见性修饰符的使用
(3)学习如何定义类和创建对象,理解对象引用变量的概念。
(4)理解构造方法的作用,并使用构造方法创建类的对象。
(5)初步了解UML类图
二、实验内容
按照如下步骤完成实验:
第1步,编写一个名为Rectangle的类表示矩形,类放在shape包中。类的编写请按照下图所示的类图。
本题目中,假设所有的矩形对象的颜色是一致的,因此颜色使用静态成员。
静态数据域color的默认值是 “BLACK”;方法getArea和getPerimeter分别计算
矩形的面积和周长。

类图的简要解释说明:
上面是使用类图描述类的结构,类图是包含自上到下3个部分的矩形。最上面
是“类名”,第2部分是“数据域”,第3部分是构造方法和方法。
数据域和方法前面的符号表示可见性修饰符。+ 表示 public , - 表示 private.
有下划线的数据域和方法是静态数据域和静态方法,使用static修饰。
第2步,编写一个名为Utility的类,放在shape包中。其中按如下要求定义如下3个static方法:
(1) public static int compare(Rectangle rect1, Rectangle rect2)
功能:
如果rect1的面积比rect2的面积大,返回值为1
如果rect1的面积比rect2的面积小,返回值为-1
如果rect1的面积与rect2的面积相同,返回值为0
(2) public static void sort(Rectangle[] rectangles)
功能:
按照矩形的面积从大到小,对数组rectangles进行排序。排序算法可以是任何
你学过的排序算法,如: 冒泡、选择、希尔、快排等。
(3) public static Rectangle getMaxRectangle(Rectangle[] rectangles)
功能:
在数组rectangles中找到并返回面积最大的矩形,如果有多个面积相同的最大
矩形,返回数组中下标最小的矩形。
(4) public static void output(Rectangle[] rectangles)
功能:
按下标顺序依次输出数组rectangles中的所有矩形。
第一行输出:共有X个矩形对象,颜色是:XXXXX
以下每行输出一个矩形,矩形的输出格式是:[宽, 高] – 面积,均保留2位小数。
第3步,编写一个名为Main的主类,放在main包中。在主类中调用Utility类定
义的方法实现如下功能:
(1) 创建一个由10个矩形对象组成的数组,每个矩形的大小随机产生,颜色是
默认值,矩形的宽度和高度的范围是[0,100)。
(2) 输出这10个矩形;
(3) 输出面积最大的矩形,输出格式是:[宽, 高] – 面积,均保留2位小数。
(4) 对这10个矩形对象按面积进行降序排序;
(5) 修改矩形的颜色为"RED";
(6) 输出排序之后10个矩形。
shape包:
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 (height+width)*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){
double area1=rect1.getHeight()*rect1.getWidth();
double area2=rect2.getHeight()*rect2.getWidth();
if(Double.compare(area1,area2)==1){ //area1>ares2
return 1;
}else if(Double.compare(area1,area2)==-1){ //area1
return -1;
}else{ //area1==ares2
return 0;
}
}
//按照矩形的面积从大到小,对数组rectangles进行排序。
public static void sort(Rectangle[] rectangles){
for(int i=0;i<rectangles.length;i++){
int max=i;
for(int j=i+1;j<rectangles.length;j++){
if(compare(rectangles[j],rectangles[max])==1){
max=j;
}
}
Rectangle rect=rectangles[i];
rectangles[i]=rectangles[max];
rectangles[max]=rect;
}
}
//在数组rectangles中找到并返回面积最大的矩形,
// 如果有多个面积相同的最大矩形,返回数组中下标最小的矩形。
public static Rectangle getMaxRectangle(Rectangle[] rectangles){
Rectangle[] tempRect=rectangles.clone(); //复制数组
sort(tempRect); //复制数组排序
double maxArea=tempRect[0].getArea(); //拿到最大面积
int i;
for(i=0;i<rectangles.length;i++){
double tempArea=rectangles[i].getHeight()*rectangles[i].getWidth();
if(tempArea==maxArea){
break; //在原数组找到最大面积就退出循环(下标最小)
}
}
return rectangles[i];
}
public static void output(Rectangle[] rectangles){
System.out.println("共有"+rectangles.length+"个矩形对象,颜色是:"+Rectangle.getColor());
for (Rectangle i:rectangles) {
double width=i.getWidth();
double height=i.getHeight();
double area=width*height;
System.out.print("[");
System.out.printf("%.2f",width); //保留两位小数
System.out.print(",");
System.out.printf("%.2f",height);
System.out.print("] – ");
System.out.printf("%.2f",area);
System.out.println();
}
}
}
main包:
package main;
import shape.Rectangle;
import shape.Utility;
import java.util.Random;
public class Main {
public static void main(String[] args) {
Rectangle[] rectangles=new Rectangle[10];
Random r=new Random();
for(int i=0;i<rectangles.length;i++){
Rectangle rect=new Rectangle(r.nextDouble()*100,r.nextDouble()*100);
rectangles[i]=rect;
}
System.out.println("输出这10个矩形--------------");
Utility.output(rectangles);
System.out.println();
System.out.println("输出面积最大的矩形--------------");
Rectangle rect=Utility.getMaxRectangle(rectangles);
double width=rect.getWidth();
double height=rect.getHeight();
double area=rect.getArea();
System.out.print("[");
System.out.printf("%.2f",width);
System.out.print(",");
System.out.printf("%.2f",height);
System.out.print("] – ");
System.out.printf("%.2f",area);
System.out.println();
System.out.println();
Utility.sort(rectangles);
Rectangle.setColor("RED");
System.out.println("输出排序之后10个矩形--------------");
Utility.output(rectangles);
}
}