题目:
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);
}
}