题目
一个矩形可以由两个相邻边的长度确定。编写一个类,使之拥有两个表示边长的整数数据,并提供一个有用的矩形计算方法,如计算面积,更新矩形边的长 度,计算两个矩形的组合面积等。提示:需要写出具有下列形式的签名的方法:
int combinedArea(secondRect sr) // the first rect is “this” of course
源代码
package com.company;
public class rectangle {
public int length; //基础属性参数
public int width;
public static int area1;
public rectangle(int x,int y){
length=x;
width=y;
}
public int area() //计算矩形面积
{
return width*length;
}
public static void combinedArea(rectangle r1,rectangle r2) //实现两个矩形 类对象的相加操作
{
area1=r1.area()+r2.area();
}
public void refreshrec(int x,int y) //更新矩形
{
length=x;
width=y;
}
public static void main(String args[]){
rectangle r1=new rectangle(3,4);
rectangle r2=new rectangle(4,5);
System.out.println("更新前第 1 个矩形面积为:"+r1.area());
r1.refreshrec(5, 6);
System.out.println("更新后第 1 个矩形面积为:"+r1.area());
System.out.println("第 2 个矩形面积为:"+r2.area());
combinedArea(r1,r2);
System.out.println("两个矩形面积和为:"+area1);
}
}