设计一个长方形Rectangle类,包含左上角坐标(x1,y1)、长(w)和高(h)四个数据成员。编写长方形类的以下方法

import java.util.Scanner;

public class HotelTest{
public static void main(String[]ars) {
int x1,y1,w,h,x,y;
Scanner sc=new Scanner(System.in);
x1=sc.nextInt();
y1=sc.nextInt();
w=sc.nextInt();
h=sc.nextInt();
x=sc.nextInt();
y=sc.nextInt();
Rectangle re=new Rectangle(x1,y1,w,h);//构造对象
System.out.println(“area=”+re.getArea(w,h));//引用函数1
re.Contains(x,y); //引用函数2
}
}
class Rectangle{
public int x,y,w,h; //把长方形的数据引入

	public Rectangle(int x1,int y1,int x2,int y2)
	{
		x=x1;y=y1;w=x2;h=y2;	//定义长方形
	}
	public  double getArea(double w,double h) 
	{
		double s;				//求面积,并返回s
		s=w*h;
		return s;
	}
	public void Contains(int a,int b)
{
		if(this.x<a&&this.y>b)	//判断点是否在长方形内
		System.out.println("("+a+","+b+")"+"在长方形("+this.x+","+this.y+"),"+"w="+w+","+"h="+(int)h+"内");
		else
		System.out.println("("+a+","+b+")"+"在长方形("+this.x+","+this.y+"),"+"w="+w+","+"h="+(int)h+"外");
}	

}
/1、编写合适的构造函数初始化一个矩形;
2、编写计算矩形面积的方法getArea();
3、编写计算判断矩形是否包含点的方法Contains(int x,int y).
/

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值