Java程序设计实验报告 继承与多态

(1)设计一个图形抽象类(Shape),其中应包括两个抽象方法:计算周长的getCir()方法和计算面积的getArea()方法。
(1)派生出Shape类的一个非抽象类:点类(Point),其中应包括构造函数、设置坐标、读取坐标等方法,并实现父类的抽象方法getCir()和getArea()。(要求:至少写两个构造函数。)
(2)派生出点类的一个子类:圆类(Circle),其中应包括构造函数、设置半径、读取半径、并改写父类实现的抽象方法getCir()和getArea()。(要求:至少写两个构造函数,并要显式调用父类的构造函数;对于半径的设置要考虑有效性。)
(3)定义接口Printable,其中包括一个方法printItMyWay(),这个方法没有形参,返回值为空。
(4)在点类中实现Printable接口,用printItMyWay()方法将点的相关信息(点的坐标位置、周长、面积)打印在屏幕上。
(5)在圆类中重载printItMyWay()方法,将圆的相关信息(圆心的坐标位置、半径长度、周长、面积)打印在屏幕上。
(6)编写一个Application程序验证以上的操作,并将所有文件组织成一个包MyShape。

package MyShape;


public class Test {


/**

* @param args

*/

public static void main(String[] args) {

Circle c = new Circle(2,4,3);

c.printItMyWay();

}


}

abstract class Shape{

public abstract float getCir();

public abstract float getArea();

}

class Point extends Shape implements Printable{

public int x;

public int y;

public Point(int x, int y){

this.x= x;

this.y=y;

}

public Point (){

}


@Override

public float getCir() {

// TODO Auto-generated method stub

return 0;

}

@Override

public float getArea() {

// TODO Auto-generated method stub

return 0;

}

public int getX() {

return x;

}

public void setX(int x) {

this.x = x;

}

public int getY() {

return y;

}

public void setY(int y) {

this.y = y;

}

@Override

public void printItMyWay() {

System.out.println(" Point ("+x+","+y+")");

System.out.println(" Point Area:"+this.getArea());

System.out.println(" Point Circle:"+this.getCir());

}

}


class Circle extends Point implements Printable{

public float r ;

public Circle(){

}

public Circle(float r,int x, int y ){

        if(r>0){

        this.r =r;

        this.x =x;

        this.y=y;

}

}

public float getR() {

return r;

}

public void setR(float r) {

this.r = r;

}

@Override

public float getArea() {

return (float) (r*r*3.14/2);

}

@Override

public float getCir() {

return (float) (3.14*r*2);

}

@Override

public void printItMyWay() {

System.out.println(" Circle ("+x+","+y+")");

System.out.println(" Circle R:"+r);

System.out.println(" Circle Area:"+this.getArea());

System.out.println(" Circle Circle:"+this.getCir());

}

}


interface Printable {

public void printItMyWay();

}

温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答