Win a copy of React Cookbook: Recipes for Mastering the React Framework this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Comparing between 2 objects inside a method

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello guys,   I have an issue with comparing between 2 Objects:

I created a  2 different objects with a constructor which has the same properties.
One method that i need to create is comparing between the price of the 2 objects. if equals - return true, else return false.

Now, i can put in the method's brackets only 1 object that the method can use, and i don't know how with this way i can compare between the 2 objects if the method doesn't has the option to use the other ones.
If you didn't understand i will just write the code below:



If i try to write the code this way, it will write me :"cant find variable p1"

How can i do it?

Thank you !
 
Marshal
Posts: 73738
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to work out what the samePrice method should do. It should compare the price of the current object to the other object. So you don't need a reference to p1, as you have been told on your other thread. Also, you don't need all of line 23. You only need return (getPrice()==other.getPrice()); or return price == other.price;
You would find the code easier to read with spaces between tokens. Have a look at our suggestions here. It is regarded as good Java® style not to use _ characters in variable names. More details in the old Sun Style Guide (they have swapped the words constants and variables in one place).
 
Campbell Ritchie
Marshal
Posts: 73738
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Change your constructor to read:-
 
rian bron
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:You need to work out what the samePrice method should do. It should compare the price of the current object to the other object. So you don't need a reference to p1, as you have been told on your other thread. Also, you don't need all of line 23. You only need return (getPrice()==other.getPrice()); or return price == other.price;
You would find the code easier to read with spaces between tokens. Have a look at our suggestions here. It is regarded as good Java® style not to use _ characters in variable names. More details in the old Sun Style Guide (they have swapped the words constants and variables in one place).



Thank you again!
 
rian bron
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Change your constructor to read:-



The" this." keyword refers to the variable inside the method's brackets variable right?
 
Campbell Ritchie
Marshal
Posts: 73738
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not sure I understand that last post, I am afraid. Start by removing the _ from the variable names.
 
author
Posts: 23906
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

rian bron wrote:
The" this." keyword refers to the variable inside the method's brackets variable right?



The this reference variable refers to the instance that is used to call the method / constructor. So... for example, in the main thread (executing the main() method)... During the first instantiation (in main), the this reference will be the value that has been instantiated and will eventually be assigned to the p1 local variable. During the second instantiation (in main), the this reference will be the value that has been instantiated and will eventually be assigned to the other local variable.

Henry
 
A teeny tiny vulgar attempt to get you to buy our stuff
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic