Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with Objects and Classes  RSS feed

 
Michael Von Thorn
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We're learning about multiple classes and objects right now.
We are seeing how private variables can only be accessed within their own classes, etc.
Can someone tell me why my diagonal method won't work when I try to print it? It says "cannot find symbol."
My getter methods do not work, either. It seems that no methods from my Point class can be found within my main class...

 
Michael Von Thorn
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, my data fields are all supposed to be private. I meant to change that back. I was testing whether it made a difference and it did not.
 
Michael Von Thorn
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I figured out that my getter and setter were improper. That much has been fixed.
But I am still 100% confused as to why I cannot use my diagonalLength method.
I am trying to use an object array now:


 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to access your method from an object, like point1.  Unfortunately, diagonal length in a single Point doesn't make much sense.
 
Michael Von Thorn
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. I get what you are saying. I tried to use an array.
I created an array containing the two points and attempting it that way.
I still received the cannot find symbol message.
Will that never work? Is the array of those two objects (point1 and point2) not an object itself?
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your getters have this problem: they have a Point as a parameter that they don't need.

After you remove the parameter, you should be able to access your getter from a Point object:

 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Von Thorn wrote:Thank you. I get what you are saying. I tried to use an array.
I created an array containing the two points and attempting it that way.
I still received the cannot find symbol message.

You still need to access the method from a Point object, like:

Will that never work? Is the array of those two objects (point1 and point2) not an object itself?

Yes, the array is an object too.  It will "work", but it's not the best place for a method like that.  But get it working first and then we'll talk about that later.
 
Michael Von Thorn
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow. Thank you! I got it working and everything just clicked for me. I can't believe that I was leaving out the most critical part; though I agree that it seems to be an odd manner of performing the calculation.
(Perhaps our instructor would prefer it if our calculations were placed within the main method, simply enough. He was really only specific about using private data fields and then making use of getters/setters).
In any case, you have helped me to reach a greater level of understanding. I am truly grateful.
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My pleasure.
 
Campbell Ritchie
Marshal
Posts: 56545
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure that is what you want? It is more object‑oriented to use the distance from the current Point to a second Point. This is what the pseudocode would look like:-There is no need to do all the calculations of squares and sqaure roots. Go through the Math class and you will find a method to calculate a hypotenuse, which is intended for converting rectangular to polar coordinates, but it will do exactly what you want. Start looking under h for hypotenuse. You don't need Math#abs because the multiplication for creating the square will never return a negative result. Have you called a square root method anywhere? That is a good argument for using ready‑made methods. There is no chance of missing part of the formula out.
You can of course have both methods in your class. I would suggest you change your method to take point1 and point2 as its parameters.

Have you been told to use option panes for keyboard input? That is a very old‑fashioned style. Most people would use Scanner. Read its documentation and you will find a suggestion about how it might work.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!