/*定义一个类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+"}");
}
}
TwoArea
最新推荐文章于 2023-03-21 10:55:49 发布