1 2 Previous Next 29 Replies Latest reply: Oct 24, 2009 1:27 PM by 3004 RSS

    Working with Classes and Arrays

    728539
      hello to all,
      public class Store
      {
      //private data
      private Person list[];
      private int count;
      private int maxSize;
      
      //constructor starts
      public Store(int max)
      {
      count = Person.count(); //sets count to 0      
      maxSize = max;//maxSize is equals to max
      Person list[] = new Person[maxSize]; 
      }//end of store
      
      //constructor ends
      
      //accessor starts    
      
      public int getCount()
      {
      for(int i=0; i<list.length; i++) 
      {
      int result = list;
      }
      return result;

      }//returns the number of elements currently in store
      the code above is working with classes and some arrays
      what am trying to do is to display what the array holds
      and then i meet an error saying that 
      
      INCOPATIBLE TYPES - FOUND PERSON BUT EXPECTED INT
      
      Please note that am new to this!
      
      Thank you in advanced!
      <img src="file:///C:/Documents%20and%20Settings/fh84/Desktop/untitled.JPG" alt="" />                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
        • 1. Re: Working with Classes and Arrays
          843789
          I think this line:
          int result = list;
          should be:
          Person result = list[i];
          ...since +list+ is an array of Persons.                                                                                                                                                                                                                                                                                        
          • 2. Re: Working with Classes and Arrays
            728539
             public int getCount()
                {
                    for(int i=0; i<list.length; i++) 
                    {
                        int result = 0;
                        Person  result = list;
            }
            return result;

            }
            now i meet this error
            
            
            result is already defined in getCount()
            
            i changed the variable to other name but the error was the same.
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
            • 3. Re: Working with Classes and Arrays
              843789
              So, what purpose does this line serve exactly? Why do you have it in your code?
              int result = 0;
              • 4. Re: Working with Classes and Arrays
                728539
                sorry am new to this....
                am just using it to return the values!
                why, is not needed?
                • 5. Re: Working with Classes and Arrays
                  843789
                  fh84 wrote:
                  sorry am new to this....
                  am just using it to return the values!
                  This doesn't explain why you want to have that particular line of code in your program. Every line of code needs to be there for a reason.
                  why, is not needed?
                  You need to answer my question, why is it there in the first place? Why do you feel you need it? It's frcking you up, you know.
                  • 6. Re: Working with Classes and Arrays
                    728539
                    ok man don't kill me...
                    I said am new to this and am trying to experiment!
                    • 7. Re: Working with Classes and Arrays
                      3004
                      Do you want your getCount method to return a number, or a Person object? I would assume a number, since it's called "getCount". If so, then declaring it to return int is correct.

                      That means that inside the method, when you say, return something, that something has to be an int. Bu you have an array or Person objects, and you're trying to do basically return list[ i ], which means you're returning a Person from a method that's supposed return a number. Obviously that makes no sense. You can't say, "The answer to your question 'How many?' is this Person." If I ask you "How many kids in your family?" do you answer by handing my your little brother?

                      So.

                      What is that method supposed to do? What is the purpose of the method? What you return from it will be driven by that.

                      Edited by: jverd on Oct 24, 2009 11:01 AM
                      • 8. Re: Working with Classes and Arrays
                        843789
                        fh84 wrote:
                        ok man don't kill me...
                        I'm not trying to kill you, I'm just trying to get a simple answer for a simple question. If you don't know the answer, then maybe you shouldn't have that line. Again, it's frcking you up.
                        • 9. Re: Working with Classes and Arrays
                          728539
                          what i am trying to do is to return the number of elements currently in store!
                          • 10. Re: Working with Classes and Arrays
                            843789
                            fh84 wrote:
                            what i am trying to do is to return the number of elements currently in store!
                            ah, my bad as I didn't look at your method signature. So you have a variable called "count" and a method called "getCount". Then this is a simple getter method and should simply return count, nothing more, nothing less.j
                            • 11. Re: Working with Classes and Arrays
                              3004
                              fh84 wrote:
                              what i am trying to do is to return the number of elements currently in store!
                              That would presumably just be your count variable then, yes? You update it every time you add or remove something from the store, I'd assume.
                              public int getCount() {
                                return count;
                              }
                              Also, as it stands, your maxSize variable is redundant and completely pointless. You can just create the array from the max variable that's passed into the constructor, and then if you need to know the size later, use the length member of the array.
                              • 12. Re: Working with Classes and Arrays
                                728539
                                the maxSize is used in another part of code.

                                the thing is that it has to return each element of an array named List and not just the count!
                                that is why i used the for loop!
                                • 13. Re: Working with Classes and Arrays
                                  843789
                                  fh84 wrote:
                                  the thing is that it has to return each element of an array named List and not just the count!
                                  that is why i used the for loop!
                                  Let's see your instructions, verbatim.
                                  • 14. Re: Working with Classes and Arrays
                                    728539
                                    public class Store
                                    {
                                        //private data
                                        private Person list[];
                                        private int count;
                                        private int maxSize;
                                    
                                    //constructor starts
                                        public Store(int max)
                                        {
                                             count = Person.count(); //sets count to 0      
                                             maxSize = max;//maxSize is equals to max
                                            // Person list[] = new Person[maxSize]; 
                                             Person list[] = new Person[maxSize]; //set tne 
                                        }//end of store
                                    
                                    //constructor ends
                                    
                                    //accessor starts   
                                    
                                    
                                        public int getCount()
                                        {
                                    
                                            for(int i=0; i<list.length; i++) 
                                            {      
                                                int  result = list;
                                    }
                                    return result;
                                    am sending the code again                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
                                    1 2 Previous Next