简体   繁体   中英

What's wrong with my getArea method?

This is a homework problem. There are a few other java files involved. Note that shapes.Rectangle and shapes.Oval contain a getArea method. I'm having a maddening time trying to figure this out any help is appreciated.

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;

    }

}

The area of a rectangle is length*width. It's different for the other shapes though. So for a rectangle you need access to its length and width. For a circle you would need its radius. The area of a circle is piR^2.

As someone mentioned below, for an ellipse:

The area of an ellipse is pi*a*b, where a and b are half the length of the major and minor axes. The area of a circle could be written as pi*r*r or pi*r^2 since a and b would be equal.

Your getArea() should have something like this:-

If currentShapeType is not a LINE (since a line is not closed shape), then calculate and return area of currentShape based on the currentShapeType .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM