封装长方形-Java

设计一个长方形Rectangle类,包含左上角坐标(x1,y1)、长(w)和高(h)四个数据成员。编写长方形类的以下方法:
1、编写合适的构造函数初始化一个矩形;
2、编写计算矩形面积的方法getArea();
3、编写计算判断矩形是否包含点的方法Contains(int x,int y).

输入

输入一个矩形的左上角坐标、长和高;
输入一个坐标x,y;

输出

长方形的面积
判定点(x,y)是否在矩形内。如果在矩形内,请如样例格式输出在长方形内,如不在,请如样例格式输出在长方形外。
System.out.println("("+i+","+j+")"+"在长方形("+a+","+b+"),"+"w="+x+","+"h="+(int)y+"内");

System.out.println("("+i+","+j+")"+"在长方形("+a+","+b+"),"+"w="+x+","+"h="+(int)y+"外");
}

难度

入门

输入示例

1 4
5 3
2 2
import java.util.Scanner;
 
public class test1 
{
private static final int x2 = 0;
public static void main(String[] args )
   {
           Scanner sc=new Scanner(System.in);
           int x1,y1,w,h,x,y,area;
           double a=0;
           x1=sc.nextInt();
           y1=sc.nextInt();
           w=sc.nextInt();
           h=sc.nextInt();
           x=sc.nextInt();
           y=sc.nextInt();
           Rectangle p1=new Rectangle(x1,y1,w,h);
           a= p1.getArea(w, h);
           System.out.printf("area=%.1f\n",a);//引用函数1
           p1.Contains(x, y);
            
   }
 static    class Rectangle
{
        public  int x,y,w,h;
     
    public  double getArea(double w,double h) 
    {
        double s;               //求面积,并返回s
        s=w*h;
        return s;
    }
    public Rectangle(int x1,int y1,int x2,int y2)
    {
        x=x1;y=y1;w=x2;h=y2;    //定义长方形
    }
    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+"外");
    }   
}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小杨不想秃头

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值