Page 1 of 3 123 LastLast
Results 1 to 20 of 52

Thread: Method call

  1. #1
    ralf is offline Member
    Join Date
    Jul 2011
    Posts
    98
    Rep Power
    0

    Default Method call

    I know this is a very simple question about method call.
    I have a method that should count elements of the same item in an arraylist.
    This method below work, it count all items with the same id, but this is not the correct way to do it after the requierments, see below.

    "numberInStock can call the
    findProduct method to do the searching, and then call the
    getQuantity method on the result."

    Yes, I know this is basic

    Java Code:
       public int numberInStock(int id)
        {
            if(id < 0)
            {
                System.out.println("This is not a valid id " + id);
            }
            else{          
                int count = 0;
                Iterator it = stock.iterator();
                while(it.hasNext())
                  {                        
                  Product product = (Product) it.next();
                  if(product.getID() == id)
                  {
                      count++;
                  } 
               }
            return count;
        }
    return 0;

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    What are the definitions for the findProduct() and the getQuantity() methods?
    How are they called and what do they return?

  3. #3
    ralf is offline Member
    Join Date
    Jul 2011
    Posts
    98
    Rep Power
    0

    Default

    Java Code:
       /**
         * Try to find a product in the stock with the given id.
         * @return The identified product, or null if there is none
         *         with a matching ID.
         */
      public Product findProduct(int id) //This method is in the same class as the numberInStock method and the getQuantity() is in another class named product
       {
           if(id < 1)
           {
               System.out.println("Not a valid id number: " + id);
            }
            else{
            Iterator it = stock.iterator();
            while(it.hasNext())
            {
                Product product = (Product) it.next();
                if(product.getID() == id)
                {
                    return product;
                } 
              }
            }
    return null;
        }
    /////////////////////////////////////////////////
      /**
         * @return The quantity in stock.
         */
        public int getQuantity()
        {
            return quantity;
        }

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    "numberInStock can call thefindProduct method to do the searching, and then call the getQuantity method on the result."
    Are those two methods useful for your numberInStock() method?
    Can you call one and then the other to get the value you need?

  5. #5
    ralf is offline Member
    Join Date
    Jul 2011
    Posts
    98
    Rep Power
    0

    Default

    Yes, they should be useful since the numberInStock should use findproduct to do the searching and then call the getQuantity.
    No, I can't call any of them to find the value I need to find. I am unsure how to make the call to find the value in the numberInStock

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    I can't call any of them to find the value I need to find
    "numberInStock can call thefindProduct method to do the searching, and then call the getQuantity method on the result."
    Please explain why you can't call those methods?

  7. #7
    ralf is offline Member
    Join Date
    Jul 2011
    Posts
    98
    Rep Power
    0

    Default

    That is the problem I have now, How I can call from method numberInStock to findProduct method and then get the result from getQuantity?
    Java Code:
    public int numberInstock(int id)
    {
      //do a call to findproduct  //I am not shore how I should write this code
    
    //return getQuantity;
    
    }

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    Can you show examples of when you have called any method that takes an argument? Any method for any class.
    Post some examples here.

    What is different about calling the findProduct() method?

  9. #9
    ralf is offline Member
    Join Date
    Jul 2011
    Posts
    98
    Rep Power
    0

    Default

    In this method I have called to the method getMonth() in class membership.
    Java Code:
     public int joinedInMonth(int month)
        {
            int count = 0;
            if(month < 1 || month > 12)
            {
                System.out.println("Not a valid month");
                {
               return 0;
                    }
         }else{   
        
            Iterator it = club.iterator();
            while(it.hasNext()) 
            {
               Membership membership = (Membership) it.next();
               if(month == membership.getMonth())
               {
                     count++;
                    // System.out.println("count 3 =" + count);                
                }
             }
            return count;
           } 
         
         }

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    Write a call to the findProduct() method
    After it write a call to the getQuantity() method.

  11. #11
    ralf is offline Member
    Join Date
    Jul 2011
    Posts
    98
    Rep Power
    0

    Default

    Do I need to use casting?

    I have tryed this: Product product = (Product) product.findProduct(); // something like this?

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    product.findProduct();
    What class is findProduct() a member of? What class is product?
    Do I need to use casting?
    What is the data type/class of the value returned by findProduct()?

  13. #13
    ralf is offline Member
    Join Date
    Jul 2011
    Posts
    98
    Rep Power
    0

    Default

    findProduct(); is a method in the class StockManager. I have now written: findProduct(id); I think that one can be right intsead of product.findProduct();

    Class Product contain id, product name and quantity, (info about product)

    The value returned by findProduct(); is return product; or return null;

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    Sorry, I don't see what your problem is. You know all the parts that you need to know about. Now you need to put them together. There is nothing complicated about what you are trying to do.

  15. #15
    ralf is offline Member
    Join Date
    Jul 2011
    Posts
    98
    Rep Power
    0

    Default

    Hello,

    Could you give me some hint? I have tryed a lot of several version, but nothing seems to be correct
    Product product = Product getQuantitiy(); etc

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    This is very basic java syntax.
    You need to go back to your textbook or tutorial and read about how to call a method and how to receive what a method returns.

  17. #17
    Joel's Avatar
    Joel is offline Member
    Join Date
    May 2011
    Location
    Placerville, CA
    Posts
    86
    Rep Power
    0

    Default

    I didn't know you could typecast a variable into a Class. Weird.

  18. #18
    ralf is offline Member
    Join Date
    Jul 2011
    Posts
    98
    Rep Power
    0

    Default

    Could you tell me if I am a bit closer to a correct code now?

    Java Code:
     public int numberInStock(int id)
        {
            if(id < 0)
            {
                System.out.println("Not a valid id " + id);
            }
            else{
                if(findProduct(id).getQuantity() == id)
                {
            
                  //  System.out.println("4. findProduct(id).getQuantity() " + findProduct(id).getQuantity());
                   return findProduct(id).getQuantity();                                   // it only return 0
                }
         return 0;
                }

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    Compile it and execute it and look at the results.
    If you get errors you don't understand, copy and paste them here with your questions.

    Your code doesn't define what the methods you are calling return.
    Does getQuantity() return an id?

    Java Code:
     System.out.println("4. findProduct(id).getQuantity() " + findProduct(id).getQuantity());
    What did that print out when you executed the code?

  20. #20
    ralf is offline Member
    Join Date
    Jul 2011
    Posts
    98
    Rep Power
    0

    Default

    Hello,

    I can compile and run it, but only 0 is returned.
    getQuantity(); should return an int of how many items there are in stock.
    When a use a debugger I can see that when it come to getQuantity() it seems that there don't exist any value other than 0,
    even it should have contained the items I have added.
    The print you ask about don't print anything at all.

Page 1 of 3 123 LastLast

Similar Threads

  1. call a JSP method
    By sauravsinha in forum JavaServer Pages (JSP) and JSTL
    Replies: 4
    Last Post: 09-26-2011, 05:19 PM
  2. How do I call this method
    By africanhacker in forum New To Java
    Replies: 1
    Last Post: 06-29-2011, 05:03 PM
  3. How to call this method?
    By Rockefella in forum New To Java
    Replies: 12
    Last Post: 01-20-2011, 05:32 PM
  4. how to call method?
    By leapinlizard in forum New To Java
    Replies: 9
    Last Post: 04-30-2009, 12:55 AM
  5. cannot call private method from static method
    By jon80 in forum New To Java
    Replies: 3
    Last Post: 05-07-2008, 09:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •