初学的JAVA的一道难题

来源:百度知道 编辑:UC知道 时间:2024/06/07 07:49:01
以前学过C++,现在刚开始接触JAVA 要求做这道,编译好几次都错,找问题找了几小时没找出来,无奈来这先求下解答,再回去找问题
本程序目的是学习并掌握类的继承,静态联编和动态联编(掌握多态方法或函数的设计)。
二 实验内容和步骤: 对给定的下列几何图形抽象类
abstract class Shape {
private double xPos;
private double yPos;

public Shape () { xPos = 0; yPos = 0; }
public Shape (double x, double y){ xPos = x; yPos = y; }
abstract public double area();
abstract public void stretchBy (double factor); // 拉伸因子
public final double getXPos(){ return xPos; }
public final double getYPos(){ return yPos; }
public void moveTo (double xLoc, double yLoc) { xPos = xLoc; yPos = yLoc; }
public String toString(){
String str = "(X,Y) Position: (" + xPos + "," + yPos + ")\n";
return str;
}
}
试设计如下继承Shape类的各具体类:
1。Triangle 类(等边三角形),Rect 类(矩形), Circle类(园)。
注意各具体类应增置各自必须的属性域和方法,并覆盖继承下来的有关方法。
2。ShapeTest 类。
该类中应提供

建议你用Eclipse,上面会对编译的错误有提示。

public class Triangle extends Shape {
private double length;

public Triangle(double length){
this.length = length;
}
@Override
public double area() {
return (this.length * this.length) * 7 / 8;
}

@Override
public void stretchBy(double factor) {
this.length = this.length * (1 + factor);
}

@Override
public String toString() {
String str = "Triangle: (" + this.length + ","
+ this.area() + ") the area is " + this.area() + ".\n";
return str;
}
}

public class Rect extends Shape {
private double length;
private double width;

public Rect(double length,double width){
this.length = length;
this.width = width;
}
@Override
public double area() {
return this.length * this.width;
}

@Override