Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat's wrong with my getArea method?
    text
    copied!<p>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.</p> <pre><code>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() &lt; oneToCompare.getArea()) return 2; // this one is smaller if (getArea() &gt; oneToCompare.getArea()) return 1; // this one is larger return 0; } return 0; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload