zoukankan      html  css  js  c++  java
  • 矩形

    25.按要求编写一个Java应用程序:

    (1)编写一个矩形类Rect,包含:

    两个属性:矩形的宽width;矩形的高height。

    两个构造方法:

    1.一个带有两个参数的构造方法,用于将width和height属性初化;

    2.一个不带参数的构造方法,将矩形初始化为宽和高都为10。

    两个方法:

    求矩形面积的方法area()

    求矩形周长的方法perimeter()

    (2)通过继承Rect类编写一个具有确定位置的矩形类PlainRect,其确定位置用

    矩形的左上角坐标来标识,包含:

    添加两个属性:矩形左上角坐标startX和startY。

    两个构造方法:

    带4个参数的构造方法,用于对startX、startY、width和height属性

    初始化;

    不带参数的构造方法,将矩形初始化为左上角坐标、长和宽都为0

    的矩形;

    添加一个方法:

    判断某个点是否在矩形内部的方法isInside(double x,double y)。如在矩

    形内,返回true, 否则,返回false。

                    提示:点在矩形类是指满足条件:

                         x>=startX&&x<=(startX+width)&&y<startY&&y>=(startY-height)

    (3)编写PlainRect类的测试程序

    创建一个左上角坐标为(10,10),长为20,宽为10的矩形对象;

    计算并打印输出矩形的面积和周长;

    判断点(25.5,13)是否在矩形内,并打印输出相关信息。

    package zuoye;
    
    public class Rect 
    {
        private int height =10;
        private int width =10;
        public int getHeight() {
            return height;
        }
        public void setHeight(int height) {
            this.height = height;
        }
        public int getWidth() {
            return width;
        }
        public void setWidth(int width) {
            this.width = width;
        }
        
        //有参的构造方法   源码  使用字段构造函数
        public Rect(int height, int width) {
            super();
            this.height = height;
            this.width = width;
        }
        
        //无参的构造方法  源码  使用字段构造函数(不打勾)
        public Rect() {
            super();
        }
        
        public int area()
        {
            return this.height*this.width;
                    
        }
        public int perimeter()
        {
            return 2*(this.height+this.width);
        }
        
    
    }
    package zuoye;
    
    //具有确定位置的矩形类
    public class PlainRect extends Rect 
    {
        private int starX;
        private int starY;
        public int getStarX() {
            return starX;
        }
        public void setStarX(int starX) {
            this.starX = starX;
        }
        public int getStarY() {
            return starY;
        }
        public void setStarY(int starY) {
            this.starY = starY;
        }
        //带参的构造方法
        public PlainRect(int height, int width, int starX, int starY) {
            super(height, width);
            this.starX = starX;
            this.starY = starY;
        }
        
        //无参
        public PlainRect() {
            super(0,0);
        }
        
        /***
         * 
         * @param x 点的X坐标
         * @param y    的的Y坐标
         * 
         * @return    是否在矩形内部
         */
        public boolean isInside(double x,double y)
        {
            return x>=starX&&x<=(starX+getWidth())&&y<starY&&y>=(starY-getHeight());
        }
    }
    package zuoye;
    
    public class PlainRect1 {
    
        public static void main(String[] args)
        {    
            PlainRect a =new PlainRect(10,20,10,10);
            
            System.out.println("矩形面积="+a.area()+"矩形的周长="+a.perimeter());
            
            System.out.println("点是否在矩形内部"+a.isInside(25.5, 13));
    
        }
    }

  • 相关阅读:
    HBase with MapReduce (MultiTable Read)
    HBase with MapReduce (SummaryToFile)
    HBase with MapReduce (Summary)
    HBase with MapReduce (Read and Write)
    HBase with MapReduce (Only Read)
    Hbase中的BloomFilter(布隆过滤器)
    HBase的快照技术
    How To Use Hbase Bulk Loading
    Cloudera-Manager修改集群的IP
    Java中的HashSet和TreeSet
  • 原文地址:https://www.cnblogs.com/Levi1995/p/5891510.html
Copyright © 2011-2022 走看看