Java定义一个名为Square继承Shape抽象类,覆盖Shape类中的抽象方法getPerimeter()和getArea

题目:

1). 按照下列要求完成抽象类的设计。

(1)定义一个public抽象类Shapes,其包名为haut.oop.exp2.abstr;

(2)在抽象类Shapes中定义两个int类型的private成员变量,其中成员变量名为:width,height;

(3)用带有两个形式参数的public构造方法对Shapes类中的成员变量进行初始化,其中形式参数名为:width,height;

(4)定义返回值为double类型并且无输入参数的public抽象方法,其中方法名为getArea;

(5)定义返回值为double类型并且无输入参数的public抽象方法,其中方法名为getPerimeter。

2). 按照下列要求完成类的继承设计。

(1)定义一个public类Square,其包名为haut.oop.exp2.exten,且该类继承了第1题中的抽象类Shapes;

(2)用带有两个形式参数的public构造方法对父类的成员变量进行初始化,其中形式参数名为:width,height;

(3)在Square类中对getArea方法进行实现,返回面积,即width* height;

(4)在Square类中对getPerimeter方法进行实现,返回周长,即2*(width+height)。

3). 在第1题和第2题的基础之上,写出下列程序的执行结果。

package haut.oop.exp2;

haut.oop.exp2.exten;

public class SquareTest {

      public static void main(String[] args) {

          Square squ = new Square(5, 15);

          double area = squ.getArea();

          double peri = squ. getPerimeter();

          System.out.print("area:" + area + "  peri:" + peri);

       }

}

package haut.oop.exp2.exten;
abstract class Shape{
	private int width,height;
	public Shape(){}
	public Shape(int width,int height){
		this.width = width;
        this.height = height;
	}
	public abstract double getPerimeter();
	public abstract double getArea();
	}
	public  class Square extends Shape{
			double width,height;
			public Square(){
				this.width =0;
                this.height = 0;
		}
		public Square(double width,double height){
			this.width = width;
            this.height = height;
		}
		public double getArea(){
			double area = width * height;
			return area;
		}
		public double getPerimeter(){
			return 2*(width + height);
		}
        public static void main(String[] args) {

         Square squ = new Square(5, 15);

         double area = squ.getArea();

         double peri = squ. getPerimeter();

         System.out.print("area:" + area + "  peri:" + peri);

      }


	}

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值