(急!!)java inheritance problem

Write an inheritance hierarchy for classes Quadrilateral, Trapezoid, Parallelogram,

Rectangle and Square. Use Quadrilateral as the superclass of the hierarchy. Make the hierarchy as deep (i.e., as many levels) as possible. Specify the instance variables and methods for each class. The private instance variables of Quadrilateral should be the x-y coordinate pairs for the four endpoints of the Quadrilateral. Write a program that instantiates objects of your classes and outputs each object’s area (except Quadrilateral)

1 Answer

Rating
  • 1 decade ago
    Favorite Answer

    class Point {

    private double x,y;

    public Point(){}

    public Point(double x, double y){

    this.x = x;

    this.y = y;

    }

    public double getX(){

    return x;

    }

    public void setX(double x){

    this.x = x;

    }

    public double getY(){

    return y;

    }

    public void setY(double y){

    this.y = y;

    }

    public String toString(){

    return "("+this.getX()+","+this.getY()+")";

    }

    }

    class Quadrilateral {//四邊形

    protected Point point1;

    protected Point point2;

    protected Point point3;

    protected Point point4;

    public Quadrilateral(){}

    public Quadrilateral( Point p1, Point p2, Point p3, Point p4 ){

    point1 = p1;

    point2 = p2;

    point3 = p3;

    point4 = p4;

    }

    public Quadrilateral( double x1, double y1, double x2, double y2,

    double x3, double y3, double x4, double y4 ){

    point1 = new Point(x1,y1);

    point2 = new Point(x2,y2);

    point3 = new Point(x3,y3);

    point4 = new Point(x4,y4);

    }

    public double calcArea(){//算面積

    return 0;

    }

    public String toString(){

    return String.format( "%s:\n%s",

    "Coordinates of Quadrilateral are", getCoordinatesAsString() );

    }

    public String getCoordinatesAsString(){

    return String.format(

    "%s, %s, %s, %s\n", point1, point2, point3, point4 );

    }

    }

    class Parallelogram extends Quadrilateral{//平行四邊形 

    public Parallelogram(){}

    public Parallelogram( Point p1, Point p2, Point p3, Point p4 ){

    super(p1,p2,p3,p4);

    }

    public Parallelogram( double x1, double y1, double x2, double y2,

    double x3, double y3, double x4, double y4 ){

    super(x1,y1,x2,y2,x3,y3,x4,y4);

    }

    public double calcArea(){//算面積

    return 0;

    }

    }

    class Rectangle extends Parallelogram{//矩形,長方形

    public Rectangle(){}

    public Rectangle( Point p1, Point p2, Point p3, Point p4 ){

    super(p1,p2,p3,p4);

    }

    public Rectangle( double x1, double y1, double x2, double y2,

    double x3, double y3, double x4, double y4 ){

    super(x1,y1,x2,y2,x3,y3,x4,y4);

    }

    public double calcArea(){//算面積

    return 0;

    }

    }

    2009-04-22 11:24:14 補充:

    class Square extends Rectangle{//正方形

    public Square(){}

    public Square( Point p1, Point p2, Point p3, Point p4 ){

    super(p1,p2,p3,p4);

    }

    2009-04-22 11:24:37 補充:

    public Square( double x1, double y1, double x2, double y2,

    double x3, double y3, double x4, double y4 ){

    super(x1,y1,x2,y2,x3,y3,x4,y4);

    }

    public double calcArea(){//算面積

    return 0;

    }

    }

    2009-04-22 11:25:09 補充:

    class Trapezoid extends Quadrilateral{//不規則四邊形,梯形

    public Trapezoid(){}

    public Trapezoid( Point p1, Point p2, Point p3, Point p4 ){

    super(p1,p2,p3,p4);

    }

    2009-04-22 11:25:30 補充:

    public Trapezoid( double x1, double y1, double x2, double y2,

    double x3, double y3, double x4, double y4 ){

    super(x1,y1,x2,y2,x3,y3,x4,y4);

    }

    public double calcArea(){//算面積

    return 0;

    }

    }

    2009-04-22 11:26:53 補充:

    public class QuadrilateralDemo {

    public static void main(String[] args) {

    Quadrilateral a = new Quadrilateral(1,1,4,1,1,8,4,8);

    System.out.println(a);

    2009-04-22 11:27:17 補充:

    Parallelogram b = new Parallelogram(2,2,5,2,2,9,5,9);

    System.out.println(b);

    Trapezoid c = new Trapezoid(3,3,6,3,3,10,6,10);

    System.out.println(c);

    2009-04-22 11:27:39 補充:

    Square d = new Square(4,4,4,9,9,9,9,4);

    System.out.println(d);

    }

    }

    2009-04-22 11:29:52 補充:

    public double calcArea(){//算面積

    return 0;

    }

    //面積的計算煩請自行補完

    2009-04-22 11:40:56 補充:

    http://tw.knowledge.yahoo.com/question/question?qi...

    另解

Still have questions? Get your answers by asking now.