我在这里有一些代码。我试图解决《如何编程Java》一书中的问题。我总结了以下问题:
您需要在另一个类中实现以下设计以及
main()
方法来测试您的实现: 在以下位置实现层次结构:
MyShape
是具有抽象Draw
方法的抽象类,MyBoundedShape
是具有抽象GetArea
方法的抽象类,MyLine
,MyOval
,MyRectangle
是具体的类在
main()
方法中,
- 要求用户选择5种形状并输入其尺寸
- 绘制选定的形状
- 计算并显示所选形状(如果它们是有界形状)的面积
运行代码时,它没有显示面积或计算出的面积。
这是这些代码:
MyShape
类
public abstract class MyShape
{
public abstract void Draw();
}
Myline
类
public class Myline extends MyShape
{
private int length;
public Myline (int length)
{
length=0;
}
public void setlength( int length )
{
length = 0;
}
public int getlength()
{
return length;
}
public void Draw()
{
System.out.printf("Drawing a line with the length",getlength());
}
}
Myextendedshape
类
public abstract class MyextendedShape extends MyShape
{
protected double area;
public abstract double getArea();
}
Myoval
类
public class Myoval extends MyextendedShape
{
private double Line1;
private double Line2;
public void Draw()
{
System.out.println("I am drawing a Oval");
}
Myoval()
{
Line1= 0.0;
Line2 = 0.0;
}
Myoval(double Line1, double Line2){
this.Line1 =Line1;
this.Line2 = Line2;
}
public double getLine1()
{
return Line1;
}
public void setLine1(double Line1)
{
this.Line1 = Line1;
}
public double getLine2()
{
return Line2;
}
public void setLine2(double Line2)
{
this.Line2 = Line2;
}
@Override
public double getArea()
{
return calculateArea();
}
private double calculateArea()
{
return area = 3.14*Line1 * Line2;
}
public String toString()
{
return "The Line number 1 of the oval is: " + Line1 + " and the Line number 2 is: " + Line2 + ", "+ "and the area is: " + getArea();
}
}
MyRectangle
类
public class MyRectangle extends MyextendedShape {
private double length, width;
public void Draw()
{
System.out.println("I am drawing a Rectangle");
}
MyRectangle()
{
length= 0.0;
width = 0.0;
}
MyRectangle(double length, double width)
{
this.length =length;
this.width = width;
}
public double getLenght()
{
return length;
}
public void setLength(double length)
{
this.length = length;
}
public double getWidth()
{
return width;
}
public void setWidth(double width)
{
this.width = width;
}
@Override
public double getArea()
{
return calculateArea();
}
private double calculateArea()
{
return area = width * length;
}
public String toString()
{
return "The width of the rectangle is: " + width + " and the length is: " + length + ", "+ "and the area is: " + getArea();
}
}
Test
public class test
{
public static void main(String [] args)
{
MyShape s = new Myline(6);
s.Draw();
s = new Myoval(4.0, 5.0);
s.Draw();
s = new MyRectangle(4.0,6.0);
s.Draw();
}
}
答案 0 :(得分:1)
您只在main中执行Draw()
方法。如果您查看代码,则Draw()
方法唯一要做的就是打印到屏幕“ I am drawing a [shape]
”
如果您还想打印出每种形状的区域,则必须在main中调用“ getArea()”方法(就像对“ Draw”所做的那样),或者使“ Draw”包含功能以输出区域。我将演示第一种方法。
public class test
{
public static void main(String [] args)
{
MyShape s = new Myline(6);
s.Draw();
s = new Myoval(4.0, 5.0);
s.Draw();
//This calculates the area of s, and then prints it to screen.
System.out.println(s.getArea());
s = new MyRectangle(4.0,6.0);
s.Draw();
System.out.println(s.getArea());
}
}
请注意,代码中没有任何地方尝试实际绘制形状。我建议您重新阅读本书的部分内容,以达到目的。