Results 1 to 15 of 15
  1. #1
    jwb4291 is offline Member
    Join Date
    Jul 2010
    Posts
    20
    Rep Power
    0

    Default What does this method do?

    So i wrote a method for an assignment, and I think it's right but I just don't know exactly what it does.
    Java Code:
    public static String huh(Shed[] sheds){
       int start = 0;
       for(int j = 1; j < sheds.length; j++)
         if (sheds[j].size < sheds[start].size)
         start = j;
       return (sheds[start].owner);
     }
    I know what it does, in a step-by-step way but what is its like...recognizable chore?

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    i wrote a method

    Why do you think it is right, ie why do you think it does what you intended it to do? What did you intend it to do?

  3. #3
    jwb4291 is offline Member
    Join Date
    Jul 2010
    Posts
    20
    Rep Power
    0

    Default

    Because I complied it with the source code
    Java Code:
    public class Shed{
      private String owner;
      private int size;
      
      public Shed(String who, int area){
        owner = who;
        size = area;
      }
      
      public String getOwner(){return owner;}
      public int getSize(){return size;}
      
      public String toString()
      {return("owner: " + owner + " size: " + size);}
    }
    and it compiled successfully and I am just trying to know what this specific method does

  4. #4
    Muskar is offline Member
    Join Date
    Nov 2010
    Posts
    73
    Rep Power
    0

    Default

    Quote Originally Posted by jwb4291 View Post
    So i wrote a method for an assignment, and I think it's right but I just don't know exactly what it does.
    Java Code:
    public static String huh(Shed[] sheds){
       int start = 0;
       for(int j = 1; j < sheds.length; j++)
         if (sheds[j][B].size[/B] < sheds[start][B].size[/B])
         start = j;
       return (sheds[start].owner);
     }
    I know what it does, in a step-by-step way but what is its like...recognizable chore?
    Firstly, the bolded lines looks like you're trying to call the method ".size()" on an Object-Array - but instead you're calling it on a 'Shed' at the index of "j" and "start" respectively - and also without parameters as well.

    This method shouldn't be working, as far as I know.

  5. #5
    jwb4291 is offline Member
    Join Date
    Jul 2010
    Posts
    20
    Rep Power
    0

    Default

    Quote Originally Posted by Muskar View Post
    This method shouldn't be working, as far as I know.
    It does work, I just cant figure out exactly what the method is doing. Which is my original question, I just want to know what the method is doing.

  6. #6
    jwb4291 is offline Member
    Join Date
    Jul 2010
    Posts
    20
    Rep Power
    0

    Default

    Nvm, I figured it out..
    It searches an array of Shed objects for the object with the lowest numeric size and returns the owner of that object as a String.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I'm terribly confused. If you state that you have written the method, then how could you write it if you don't know what it does? Don't you need to know what it does before you can write it?

    Or was it copied from an assignment and part of your assignment is to tell the instructor what it does?

  8. #8
    Muskar is offline Member
    Join Date
    Nov 2010
    Posts
    73
    Rep Power
    0

    Default

    Quote Originally Posted by jwb4291 View Post
    It does work, I just cant figure out exactly what the method is doing. Which is my original question, I just want to know what the method is doing.
    Alright, there's obviously something I didn't know about objects then - since the only types of code "called" on an object I know (that doesn't have parameters) are enums - and they don't contain integers in them as far as I know.

    Anyway, from what I understand it returns the "Shed" with the lowest value of "size".
    Last edited by Muskar; 11-21-2010 at 09:10 PM.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Muskar View Post
    Alright, there's obviously something I didn't know about object-arrays, I just checked the API.

    Anyway, from what I understand it returns the "Shed" with the lowest value of "size".
    It doesn't return a Shed object, it returns a String that represents the owner of the Shed with the lowest size property. My previous questions however still stand.

  10. #10
    Muskar is offline Member
    Join Date
    Nov 2010
    Posts
    73
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    It doesn't return a Shed object, it returns a String that represents the owner of the Shed with the lowest size property. My previous questions however still stand.
    Yeah sorry, that's what I meant. Yet I still don't understand what is "called" on the objects (the .size and .owner).
    EDIT:
    I just looked up Properties and it involves keys, which is something I know I'm scheduled to learn until spring (on my education) - if it's a property then I'll stay confused - so sorry to the OP.
    Last edited by Muskar; 11-21-2010 at 09:20 PM.

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Also, to the original poster -- I recommend that you enclose all blocks in curly braces so as to avoid confusion. For instance, I recommend something more like this:

    Java Code:
     public static String huh(Shed[] sheds){
       int indexOfSmallest = 0;
       for(int j = 1; j < sheds.length; j++) {
         if (sheds[j].getSize < sheds[indexOfSmallest].getSize) {
           indexOfSmallest = j;
         }
       }
       return (sheds[indexOfSmallest].owner);
     }

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Muskar View Post
    EDIT:
    I just looked up Properties and it involves keys, which is something I know I'm scheduled to learn until spring (on my education) - if it's a property then I'll stay confused - so sorry to the OP.
    You're confusing Java Properties which can store configurable parameters of a program with the more generic term, properties, which are fields that have somewhat limited access via getters and sometimes setters: Property

  13. #13
    Muskar is offline Member
    Join Date
    Nov 2010
    Posts
    73
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    You're confusing Java Properties which can store configurable parameters of a program with the more generic term, properties, which are fields that have somewhat limited access via getters and sometimes setters: Property
    Quote Originally Posted by Wikipedia.org
    Some object-oriented languages, such as Java, don't support properties, and require the programmer to define a pair of accessor and mutator methods instead.
    I'm even more confused now. I cannot seem to find the difference between a property and a field. But no matter - I'm not topic-starter here, so let's not go too off-topic.

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Agree. And the original poster has left the forum and without explanation has ignored my questions to him. Hm.

  15. #15
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default

    the original poster has left the forum and without explanation has ignored my questions to him.
    Probably gone to another forum to ask what another copy-pasted method does.

    db

Similar Threads

  1. Thread problem, calling method in run method
    By majk in forum Threads and Synchronization
    Replies: 4
    Last Post: 09-27-2010, 12:40 PM
  2. Replies: 2
    Last Post: 03-26-2010, 06:12 PM
  3. ArrayLists compareTo method, equals method
    By random0munky in forum New To Java
    Replies: 2
    Last Post: 10-26-2009, 08:20 PM
  4. Replies: 29
    Last Post: 09-25-2008, 08:55 PM
  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
  •