自定义类-矩形(Rectangle Class)

为了方便,一个source文件中包括了实体类和测试类两个类。

麻雀虽小,五脏俱全,慢慢的体会到OOP的博大精深了。


代码如下:

package example;
//JHTP Exercise 8.4: Rectangle Class
//by pandenghuang@163.com
/**(Rectangle Class) Create a class Rectangle with attributes length and 
 * width, each of which defaults to 1. Provide methods that calculate the
 *  rectangle’s perimeter and area. It has set and get methods for both 
 *  length and width. The set methods should verify that length and width 
 *  are each floating-point numbers larger than 0.0 and less than 20.0. 
 *  Write a program to test class Rectangle.
*/
import java.util.Scanner;

class Rectangle {
	private double length=1;
	private double width=1;
	
	public double getLength(){	
		return this.length;	
	}
	public double getWidth(){	
		return this.width;	
	}
	
	public void setLength(double length){	
		if (length<0.0 || length>20.0){
			throw new IllegalArgumentException ("长度不在正确范围内!");
		}
		this.length=length;
	}
	public void setWidth(double width){
		if (length<0.0 || length>20.0){
			throw new IllegalArgumentException ("宽度不在正确范围内!");
		}
		this.width=width;	
	}
	
	public	double calcPerimeter(){
		return 2*(this.length+this.width);
	}
	
	public	double calcArea(){
		return this.length*this.width;
		}
}


public class RectangleTest {

	public static void main(String[] args) {

		Scanner input=new Scanner(System.in);
		Rectangle r1=new Rectangle();
		Rectangle r2=new Rectangle();
		Rectangle r3=new Rectangle();
		double length=0;
		double width=0;
		
		r1.setLength(2.5);
		r1.setWidth(3.5);
		r2.setLength(16.5);
		r2.setWidth(19.8);
		
		System.out.printf("预定义矩形1长度:%.2f,宽度:%.2f\n",r1.getLength(),r1.getWidth());
		System.out.printf("预定义矩形1周长:%.2f,面积:%.2f\n\n",r1.calcPerimeter(),r1.calcArea());
		System.out.printf("预定义矩形2长度为:%.2f,宽度为:%.2f\n",r2.getLength(),r2.getWidth());
		System.out.printf("预定义矩形2周长:%.2f,面积:%.2f\n\n",r2.calcPerimeter(),r2.calcArea());
		
		System.out.printf("请输入自定义矩形的长和宽:");
		length=input.nextDouble();
		width=input.nextDouble();
		r3.setLength(length);
		r3.setWidth(width);
		System.out.printf("自定义矩形3长度为:%.2f,宽度为:%.2f\n",r3.getLength(),r3.getWidth());
		System.out.printf("自定义矩形3周长:%.2f,面积:%.2f\n\n",r3.calcPerimeter(),r3.calcArea());
		
		}		
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#include <iostream> #include <string> using namespace std; struct CPoint { int x ; int y ; }; class CRectangle { private: const int id;//常量数据成员 static int total;//静态数据成员 const static string sclass; const static int f=1.0f; CPoint lefttop ; CPoint rightdown ; public: CRectangle( ); CRectangle( CPoint& lt, CPoint& rd ); CPoint GetLefttop() const { return lefttop; } CPoint GetRightdown() const { return rightdown; } void SetLefttop(CPoint & pt) { lefttop=pt; } void SetRightdown(CPoint & pt) { rightdown=pt; } int Getid() const { return id; } static int Gettotal() { return total; } int Area( ) const; int Perimeter( ) const; }; int CRectangle::total=0;//静态数据成员必须在类的外部定义(正好一次)。 const string CRectangle::sclass="CRectangle"; CRectangle::CRectangle( ):id(++total) { lefttop.x=0; lefttop.y=0; rightdown.x=1; rightdown.y=1; } CRectangle::CRectangle( CPoint& lt, CPoint& rd ):id(++total) { lefttop = lt ; rightdown = rd ; } int CRectangle::Area( ) const { int wd= rightdown.x - lefttop.x ; int ht= rightdown.y - lefttop.y ; return wd * ht ; } int CRectangle::Perimeter( ) const { int wd= rightdown.x - lefttop.x ; int ht= rightdown.y - lefttop.y ; return 2 * ( wd + ht ) ; } int main() { CPoint lt, rd; cin >> lt.x >> lt.y; cin >> rd.x >> rd.y; CRectangle crt(lt,rd);//调用有参构造函数 CRectangle crt2;//调用默认构造函数 //创建常量对象 const CRectangle crt3(lt,rd); cout<<"当前创建的矩形个数为:"; cout<<CRectangle::Gettotal()<<endl; //返回矩形的左上和右下点 CPoint lt1=crt.GetLefttop(); CPoint lt2=crt.GetRightdown(); //显示矩形的坐标 cout<<crt.Getid()<<"号矩形的坐标是:"<<"("<<lt1.x<<","<<lt1.y<<"), "; cout<<"("<<lt2.x<<","<<lt2.y<<")"<<endl; //显示矩形的面积和周长 cout << "Area:"<<crt.Area( )<<endl; cout <<"Perimeter:"<<crt.Perimeter( )<<endl; //修改矩形的左上角点 cout<<"请输入矩形新的左上点坐标:"; cin>> lt.x>>lt.y; crt.SetLefttop(lt); lt1=crt.GetLefttop(); //显示修改后矩形的坐标 cout<<"矩形的坐标是:"<<"("<<lt1.x<<","<<lt1.y<<"), "; cout<<"("<<lt2.x<<","<<lt2.y<<")"<<endl; //显示修改后矩形的面积和周长 cout << "Area:"<<crt.Area( )<<endl; cout <<"Perimeter:"<<crt.Perimeter( )<<endl; }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值