topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Thursday March 28, 2024, 9:43 am
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: Beginner's dumb question ...  (Read 5031 times)

tsaint

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 497
  • Hi from the a*** end of the earth
    • View Profile
    • Read more about this member.
    • Donate to Member
Beginner's dumb question ...
« on: April 22, 2010, 08:30 AM »
I have defined a class called Products (attached file)
The class has a method getNumInStock() to return the numbers in stock for any Product object I've created
I have made 2 objects called sprocket and wrench and can set the numbers of each in stock using a driver program.

My problem:
Add a method to the Product class called compareTo which takes a parameter specifying
another product- and returns an integer satisfying the following conditions:
0     if this product has the same number of items in stock as the other product
< 0   if this product has fewer items in stock than the other product
> 0   if this course has more items in stock than the other product

Fine, except from inside this new method, I can't retrieve the numbers in stock of objects. My effort at doing so is in line 41 of the attached file.
If any knowledgeable person is masochistic enough, or altruistic enough to explain how to do this, I would be really greatful.
thanks
tony
 

f0dder

  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 9,153
  • [Well, THAT escalated quickly!]
    • View Profile
    • f0dder's place
    • Read more about this member.
    • Donate to Member
Re: Beginner's dumb question ...
« Reply #1 on: April 22, 2010, 09:17 AM »

First, the code - I've included only the relevant changes, so they're easy to see.

Code: Java [Select]
  1. public class Product implements Comparable<Product>
  2. {
  3.         // rest of stuff goes here
  4.        
  5.         @Override
  6.         public int compareTo(Product other)
  7.         {
  8.                 int comparison = 0;
  9.                 comparison = other.getNumInStock() - this.getNumInStock();
  10.                 return comparison;
  11.         }
  12. }

The first thing to notice is that we're now saying the Product implements the Comparable interface, specialized for Product. In other words: we're telling the compiler we can compare Products to other Products.

Next, notice that compareTo() now takes a *Product* as the "other" object. If you had just implemented the Comparable interface (without specializing it for Product), you would get an Object and not a Product, which you'd then have to typecast and be all messy about.

Finally, notice the @Override attribute. This tells the compiler that you're *expecting* a base class to provide a compareTo method that you want to override - if it doesn't (you forgot to add an "implements interface", or have misspelled the method name, or aren't using the right argument overloads), you'll get a compiler warning rather than scratching your beard wondering why your (not really :)) overriden method is never called.

Hope this helps!
- carpe noctem

tsaint

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 497
  • Hi from the a*** end of the earth
    • View Profile
    • Read more about this member.
    • Donate to Member
Re: Beginner's dumb question ...
« Reply #2 on: April 22, 2010, 05:17 PM »
Hope that helps? Yes, a lot thanks, and thanks for taking the time.
I think my understanding is tenuous, at best, but I know enough to follow what you say needs to be done
tony

tsaint

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 497
  • Hi from the a*** end of the earth
    • View Profile
    • Read more about this member.
    • Donate to Member
Re: Beginner's dumb question ...
« Reply #3 on: April 23, 2010, 08:15 AM »
Sorry, 1 more question...
public int compareTo(Product other) works well if Ive created 2 objects, say wrench and sprocket ... I just use someinteger = wrench.compareTo(sprocket)
BUT.. what if I asked the user to input the names of 2 products into strings item1 and item2 for example. (so item1 = "wrench" and item2 = "sprocket")

Now someinteger = item1.compareTo(item2) won't work because item1 is a string, not an object, as is item2. So I need to get java to actually use the object whose name is the string in item1 and same for the item2
Can be done?

f0dder

  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 9,153
  • [Well, THAT escalated quickly!]
    • View Profile
    • f0dder's place
    • Read more about this member.
    • Donate to Member
Re: Beginner's dumb question ...
« Reply #4 on: April 23, 2010, 10:42 AM »
Hm, I'm not sure trying to do that a good idea - a String isn't a Product. You can do item.getName().compareTo(itemNameString) :)

You can also do a
Code: Java [Select]
  1. @override
  2. public String toString()
  3. {
  4.   return this.name;
  5. }
- carpe noctem