TwoArea

本文介绍如何使用Point和Graph类定义矩形并计算面积差,包括矩形移动和尺寸调整。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*定义一个类Point*/
class Point 
{
	public int x;
	public int y;
	public Point()
	{
		this.x = 0;
		this.y = 0;
	}
	public Point(int x,int y)
	{
		this.x=x;
		this.y=y;
	}
}
/*定义一个类Graph*/
class Graph
{
	public int width=0;
	public int height=0;
	public Point origin;
	public Graph()
	{
		origin=new Point(0,0);
	}
	public Graph(Point p)
	{
		origin = p;
	}
	public Graph(Point p,int w,int h)
	{
		origin=p;
		width=w;
		height=h;
	}
	public void move1(int x, int y)
	{
		origin.x=x;
		origin.y=y;
	}
	public void move2(int a,int b)
	{
		origin.x+=a;
		origin.y+=b;
	}
	public int area()
	{
		return width * height;
	}
}
public class TwoArea
{
	public static void main(String args[])
	{
		int x =3,y = 4;
		int w =4,h = 5;
		Point p1 = new Point(x,y);
		Point p2 = new Point(6,8);
		Graph r1 = new Graph(p1,w,h);
		/*以点p1为原点,长为w,高为h画矩形r1*/
		Graph r2 = new Graph(p2,8,10);
		/*以点p2为原点,长为8,高为10画矩形r2*/
		r1.move1(4,6);
		r2.move2(-2,4);
		System.out.println("两个矩形面积差= "+(r2.area()-r1.area()));
		/*调用成员方法计算矩形r2与r1面积差*/
		System.out.print("r1的原点为(" +r1.origin.x+ ",");
		System.out.print(r1.origin.y+")");
		System.out.print("r2的原点为(" +r2.origin.x+",");
		System.out.print(r2.origin.y+"}");
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值