内容要求:
一、实验目的
(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个矩形。
提交:打包为可以执行的JAR文档,其中要包含源程序文件。
总结:
1.因为包不同,所以我们需要导入包。
import shape.Rectangle;
import shape.Utility;
2.static
static关键字基本概念:
方便在没有创建对象的情况下来进行调用。
也就是说:被static关键字修饰的不需要创建对象去调用,直接根据类名就可以去访问。
private static String color = "BLACK";
public static void setColor(String color) { Rectangle.color = color; }
Rectangle.setColor("RED");
这样就是说在类数组中,我们只有一个颜色,也就是说,只要是同一个Class,我们的color成员是共用的。
用法上,就是用类名直接访问就好了。
具体的参考:深入分析Java中的关键字static
public static void output(Rectangle[] rectangles)
Utility.output(rec);
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 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;
}
}
Utility类:
package shape;
public class Utility {
public static int compare(Rectangle rect1, Rectangle rect2){
return Double.compare(rect1.getArea(), rect2.getArea());
}
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 (Rectangle rectangle : rectangles) {
System.out.printf("[%.2f,%.2f] - [%.2f]\n", rectangle.getWidth(), rectangle.getHeight(), rectangle.getArea());
}
}
}
Main类:
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.output(rec);
Utility.sort(rec);
Rectangle.setColor("RED");
Utility.output(rec);
}
}