reset password
Author Message
disrael
Posts: 44
Posted 23:46 Oct 05, 2013 |

I've come across this several times now and its really holding me up and no matter how I go about it I just don't understand why.

It keeps saying "cannot find symbol- method getWidth(Rectangle)"

Here is my code so far if it helps


    public static void main(String[] args)
    {
        Rectangle box = new Rectangle(5, 10, 60, 90);
        int width= getwidth(box);
        int height= getHeight(box);
        
        int area= (width) * (height);
        System.out.println=(area);
        System.out.println=("Expected 7300");
        
       

 

vluu2
Posts: 64
Posted 00:26 Oct 06, 2013 |

Do this in the Rectangle File, it should work then. I also get that problem, but use the file thats in the Lesson 2 Code and it should work fine.

Echoofdeath
Posts: 28
Posted 05:32 Oct 06, 2013 |

getWidth() and getHeight() are within the methods of  Rectangle class. so basically , you should use the dot notation on your specified 'box' Rectangle object.

lol i used the name box as well but you can name it anything you'd like by the way.

this is the clue i can give

=box.getWidth();

is the way to use it but find out what goes before and afterwards by reading the book.

kknaur
Posts: 540
Posted 08:58 Oct 06, 2013 |

Good.  I like to see students helping out other students.  The book is moving into object oriented programming a lot faster than one would normally learn in this class.  It can be a shift in thinking if you have never seen this before.  Generally you need to create an instance of the object you want to work with and through that instance you access all of the methods that are available to you.  So if I created an instance of the Rectangle class such as:

Rectangle rect1 = new Rectangle(10, 10, 20, 20);

Then I would have to access the methods in the Rectangle class through the rect1 object such as rect1.methodNameHere().  Also, if you haven't noticed each object you create is independent of any other object you create.  For example if I create:

Rectangle rect2 = new Rectangle(5, 5, 3, 3);

rect1 and rect2 are both instances of the Rectangle class, but they each have their own independent properties.  When I use getWidth on rect1, I will get a different value than if I were to use it on rect2.

disrael
Posts: 44
Posted 13:03 Oct 06, 2013 |

Whoops figured it out.

Last edited by disrael at 13:21 Oct 06, 2013.