一、实验目的
掌握类和对象的定义,以及它们之间的关系
学习类中属性和方法的设计
会用UML图来对类进行描述
熟悉修饰符的作用,会用set/get方法对数据进行封装
二、实验要求
设计一个名为Rectangle的类表示矩形。
这个类包括:
∙两个名为width和height的私有double型数据域。
用来表示宽、高。
默认值为1.0。
∙创建默认矩形的无参构造方法。
∙一个创建width和height为指定值的矩形的构造方法。
∙一个名为GetArea()的公有方法返回矩形面积double
∙一个静态的Compare方法,比较两个矩形的面积大小是否相等并返回一个布尔型结果,相等返回true,不等返回false;
画出UML 类图,实现Rectangle类,编写测试程序创建两个Rectangle类的对象。
第一个对象的width为1,height为1;第一个对象的width为5,height为6。
调用compare方法判断两个矩形是否相等并显示结果。
三、实验内容
1.使用Jude工具绘制UML图
2.编写代码实现类Rectangle
package edu.neu.li.test;
publicclass Rectangle {
privatedouble width;
privatedouble height;
public Rectangle() {
this.width = 1.0;
this.height = 1.0;
public Rectangle(double width, double height) {
this.width = width;
this.height = width;
}
publicdouble GetArea() {
returnthis.width * this.height ;
}
publicstaticboolean Compare(Rectangle rec1, Rectangle rec2) { if(rec1.GetArea()==rec2.GetArea())
{
returntrue;
}
returnfalse;
}
}
3.编写测试程序完成功能。
package edu.neu.li.test.run;
import edu.neu.li.test.Rectangle;
publicclass test4run {
publicstaticvoid main(String[] args)
{
boolean bEqual=false;
Rectangle rec1=new Rectangle();
Rectangle rec2=new Rectangle(5.0,6.0);
bEqual=pare(rec1, rec2);
System.out.println("The Compare Resule is:"+bEqual);
}
}
四、实验结果。