2011-03-09 98 views
0

这是一个家庭作业问题。还有一些其他的java文件涉及到。请注意,shapes.Rectangle和shapes.Oval包含一个getArea方法。我有一个疯狂的时间试图找出任何帮助表示赞赏。我的getArea方法有什么问题?

package model; 

import java.awt.Color; 
import java.awt.Container; 

import shapes.Line; 
import shapes.Oval; 
import shapes.Rectangle; 
import shapes.Shape; 
import shapes.Triangle; 
import interfaces.ComparableShape; 
import interfaces.Resettable; 

public class Model implements Resettable,ComparableShape { 
    private Container container; 
    private String message; 
    private String action = DRAW; 
    private boolean fill = false; 
    private String currentShapeType;  
    private Shape currentShape; 
    private Color fillColor = Color.gray; 

    public Color lineColor; 
    public final static String DRAW = "Draw"; 
    public final static String MOVE = "Move"; 
    public final static String REMOVE = "Remove"; 
    public final static String RESIZE = "Resize"; 
    public final static String FILL = "Fill"; 
    public final static String CHANGE = "Change"; 
    public final static String RECTANGLE = "Rectangle"; 
    public final static String OVAL = "Oval"; 
    public final static String LINE = "Line"; 
    public final static String TRIANGLE = "Triangle"; 
    public static String[] selections = {"Rectangle", "Oval", "Line", "Triangle"}; 
    //project 9 begin 
    public Shape[] myShapes = new Shape[2]; 
    //project 9 stop 


    public Shape createShape() { 

     if(currentShapeType == RECTANGLE){ 
      currentShape = new Rectangle(0, 0, 0, 0, lineColor, fillColor, fill); 
     } 
     if(currentShapeType == OVAL) { 
      currentShape = new Oval(0,0,0,0, lineColor, fillColor, fill); 
     } 
     if(currentShapeType == LINE) { 
      currentShape = new Line(0,0,0,0, lineColor, fillColor, fill); 
     } 
     if(currentShapeType == TRIANGLE) { 
      currentShape = new Triangle(0,0,0,0, lineColor, fillColor, fill); 
     } 
     //project 9 start 
     if(myShapes[0] == null) { 
      myShapes[0]=currentShape; 
     } 
     else { 
      myShapes[1]=currentShape; 
     } 
     //project 9 stop 
     return currentShape; 

    } 

    public Shape getCurrentShape() { 
     return currentShape; 
    } 
    //project 9 begin 
    public Shape[] getMyShapearray() { 
     return myShapes; 
    } 
    //project 9 end 
    public String getCurrentShapeType(){ 
     return currentShapeType; 
    } 

    public void setCurrentShapeType(String shapeType){ 
     currentShapeType = shapeType; 
    } 

    public Model(Container container) { 
     this.container = container; 
    } 

    public void repaint() { 
     container.repaint(); 
    } 

    public String getAction() { 
     return action; 
    } 

    public void setAction(String action) { 
     this.action = action; 
    } 

    public boolean isFill() { 
     return fill; 
    } 

    public void setFill(boolean fill) { 
     this.fill = fill; 
    } 

    public void setMessage(String msg) { 
     this.message = msg; 
    } 

    public String getMessage() { 
     return this.message; 
    } 

    public Color getLineColor() { 
     return this.lineColor; 
    } 

    public void setLineColor(Color c) { 
     this.lineColor = c; 
    } 

    public String toString() { 
     return "Model:\n\tAction: " + action + "\n\tFill: " + fill + "\n\tArea: " ; 
    } 

    public void resetComponents() { 
     action = DRAW; 
     currentShape = null; 
     myShapes = null; 
     if (container instanceof Resettable) { 
      ((Resettable) container).resetComponents(); 
     } 
    } 
    //Add a method to your model called compareShapes(), 
    //which will return either 0, 1, or 2--1 if the area of the first Shape is bigger than the second, 
    //2 if it is smaller, and 0 if the two Shapes are the same size. 
    //Create an interface named ComparableShape that will be used by the shape objects. 
    //The interface should require implementing classes to have a 
    //method getArea() capable of returning the area of the object. Obviously, only closed shapes can do this. 
    //The instanceof operator will be handy here. 

     public int getArea() { 

    return getWidth()*getHeight(); 
} 

private int getHeight() { 
    ///what goes here?! 
    return 0; 
} 

private int getWidth() { 
    //what goes here?! 
    return 0; 
} 



    public int compareShapes(ComparableShape b) { 
     ComparableShape oneToCompare = null; 

     if (b instanceof ComparableShape) { 
     oneToCompare = (ComparableShape)b; 
     if (getArea() < oneToCompare.getArea()) return 2; // this one is smaller 
     if (getArea() > oneToCompare.getArea()) return 1; // this one is larger 
     return 0; 
     } 
     return 0; 

    } 

} 
+1

因为getArea()方法是空的,所以错了! – Hery 2011-03-09 01:37:38

+0

你注意到了“//这里有什么?!”评论?!!!!! – thefonso 2011-03-09 01:46:48

+0

为什么这是低调的?它被正确标记为一个家庭作业问题,并且格式良好。 – Pete 2011-03-09 01:52:53

回答

0

矩形的面积是长*宽。但其他形状不同。所以对于一个矩形,你需要访问它的长度和宽度。对于一个圆圈,你需要它的半径。圆的面积是piR^2。

正如有人下面所提到的,对于椭圆形:

椭圆的面积是PI * A * B,其中a和b是长轴和短轴的长度的一半。圆的面积可以写成pi * r * r或pi * r^2,因为a和b是相等的。

+0

实际上,椭圆的面积是pi * a * b,其中a和b是长轴和短轴长度的一半。圆的面积可以写成pi * r * r或pi * r^2,因为a和b是相等的。 – 2011-03-09 02:01:43

+0

感谢您的追捕,我想我跳过了枪。我会更新我的答案。 – Pete 2011-03-09 02:04:32

0

getArea()应该有这样的事情: -

如果currentShapeType不是LINE(因为线不是封闭的形状),然后计算并返回基于currentShapeTypecurrentShape区域。