2011-03-09 82 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區域。