Okay so I have three class
abstract class Shape { int width, height; String color; public void draw() { } } // end Shape class ``
class Rectangle extends Shape { Rectangle(int w, int h, String color) { width = w; height = h; this.color = new String(color); } public void draw() { System.out.println("I am a " + color + " Rectangle " + width + " wide and " + height + " high."); } }// end Rectangle class ``
class Circle extends Shape { Circle (int r, String color) { width = 2*r; height = 2*r; this.color = new String(color); } public void draw() { System.out.println("I am a " + color + " Circle with radius " + width + "."); } } // end Circle class `` What I am trying to do is create a new class to produce the following outputs: I am a blue Rectangle 20 wide and 10 high. I am a red Circle with radius 30. I am a green Rectangle 25 wide and 25 high But I am having a problem calling the method draw();
This is the main class: public class Caller { public static void main(String args[]) { Caller call= new Caller(); Shape[] myShape = new Shape[3]; myShape[0] = new Rectangle(20,10,"blue"); myShape[1] = new Circle(30, "red"); myShape[2] = new Rectangle(25,25, "green"); for (int i=0; i < 3; i++) { System.out.println(); } call.draw(Rectangle); call.draw(Circle); } } 34 Answers
Your code formatting is horrible, so this is just a guess. I think you should change
for (int i=0; i < 3; i++) { System.out.println(); } call.draw(Rectangle); call.draw(Circle); to
for (int i=0; i < myShape.length; i++) { myShape[i].draw(); } Also, in the Shape class change
public void draw() { } to
public abstract void draw(); 1Your draw() method is defined on your Shape classes, not on your Caller class.
myShape[0].draw() to print out the rectangle for example.
Inside your for loop you need to call the draw method for the specific Shape that you're on and you don't need to call System.out.println() unless you want another blank line.
for (int i=0; i < 3; i++) { myShape[i].draw(); } Remove the lines like call.draw. You don't use call to call a method. In fact you don't even need an instance of your Caller object. Just call the draw method on the Shape objects you already have.
As in jlordo's answer, there is no need for the Shape class to be abstract if it has no methods. So you can either make the draw method abstract, or you can remove abstract from the Shape class.
There are several issues with your code:
1) The draw() method in the abstract class should be made abstract. Or, you could implement it as "partial" like in the solution I post further down, then without the abstract keyword at the Shape class:
2) new String("color") is not needed. Strings are immutable, so you can just write as below.
3) you miss specifying if your class members are private or public or protected. It is always a good idea to think about visibility of variables.
4) You try to print the diameter not the radius
5) You try to call a method on the class and not on the instance
Here is my suggestion (untested... there might be more issues that i did not see):
class Shape { protected int width, height; protected String color; public void draw() { System.out.print("I am a " + color"); } } // end Shape class class Rectangle extends Shape { Rectangle(int w, int h, String color) { this.width = w; this.height = h; this.color = color; } public void draw() { super(); System.out.print(" Rectangle " + width + " wide and " + height + " high.\n"); } }// end Rectangle class class Circle extends Shape { Circle (int r, String color) { this.width = 2*r; this.height = 2*r; this.color = new String(color); } public void draw() { super(); System.out.print(" Circle with radius " + (width/2) + ".\n"); } } // end Circle class later in the main method you need to do something like:
Shape[] myShape = new Shape[3]; myShape[0] = new Rectangle(20,10,"blue"); myShape[1] = new Circle(30, "red"); myShape[2] = new Rectangle(25,25, "green"); for (int i=0; i < 3; i++) { myShape[i].draw(); }