Results 1 to 11 of 11
Like Tree1Likes
  • 1 Post By Scopic

Thread: Help with a specific display function

  1. #1
    Scopic is offline Member
    Join Date
    Oct 2011
    Posts
    24
    Rep Power
    0

    Default Help with a specific display function

    Hello again,

    I am having trouble figuring out the logic for my display function. The job of this method is to display all of the objects of a specified class from within an array of objects containing another class.

    For example, I have a Subscriber and an Application class. Information from both of these classes is added to an array in a separate class named ElementSet. I want to print only the information from the Subscriber class.

    the function should start like this:

    Java Code:
    public void displayAllInClass(String theClassName)
        {
            //logic...
        }
    It takes in a string parameter; this being the name of the class (either Subscriber or Application) and then prints only the information from that class.

    an example of the call would be:

    Java Code:
    set.displayAllInClass("Subscriber");
    Could anyone please help me with the logic? For some reason I am drawing a complete blank and am very unsure where to start.

    I have the rest of my code (5 classes total, so it is very long) if anyone needs to see it. Thank you very much!

  2. #2
    Scopic is offline Member
    Join Date
    Oct 2011
    Posts
    24
    Rep Power
    0

    Default Re: Help with a specific display function

    Okay, so I was playing around with it, but I am getting errors.

    Java Code:
     public void displayAllInClass(String theClassName)
        {
            System.out.println("Here.");
            if (currentSize == 0)
             {
                 System.out.println("Here.");
                System.out.println("There are no objects in the set. ");
             }
            else
            {
                System.out.println("Here.");
                for (int i = 0; i < currentSize; i++)
                {
                    System.out.println("Here.");
                   if(theClassName.equals(elementList[i].getClassName()))
                   {
                       System.out.println("Here.");
                        elementList[i].display();
                        System.out.println("Here.");
                        System.out.println("\n");
                        System.out.println("Here.");
                   }
                }
            }
        }
    After adding an item to the elementList, this prints:

    Java Code:
    Here.
    Here.
    Here.
    This portion of code is having issues:

    Java Code:
     for (int i = 0; i < currentSize; i++)
                {
                   if(theClassName.equals(elementList[i].getClassName()))
                   {
                        elementList[i].display();
                        System.out.println("\n");
                   }
                }
    It fails as it attempts to enter the for loop. I tried changing the variable currentSize to MAXSETSIZE (integer constant, 100) in the loop declaration and received this error:

    Java Code:
    Exception in thread "main" java.lang.NullPointerException
    	at assignment.pkg2.ElementSet.displayAllInClass(ElementSet.java:220)
    	at assignment.pkg2.Assignment2.main(Assignment2.java:153)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 8 seconds)
    Any ideas guys? I am a little lost.

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

    Default Re: Help with a specific display function

    I would put numbers or text in each of your println("here") lines, e.g.,

    Java Code:
    public void displayAllInClass(String theClassName)
       {
           System.out.println("debug: in displayAllInClass");
           if (currentSize == 0)
            {
                System.out.println("debug: currentSize == 0.");
               System.out.println("There are no objects in the set. ");
            }
           else
           {
               System.out.println("debug: currentSize != 0");
    I worry about currentSize being a variable rather than use the length of an array or the size() of a collection.

    I worry about your having objects of different types in the same array. Why not an array or collection for each type? Whenever I see use of instanceOf or use of class names my stomach gets a little queasy.

  4. #4
    Scopic is offline Member
    Join Date
    Oct 2011
    Posts
    24
    Rep Power
    0

    Default Re: Help with a specific display function

    Unfortunately, my instructor requires we have them all stored in the same array, "elementList[]".

    Is there a way for it to ignore objects of the class that is not specified by "theClassName"?

    e.g.:

    Java Code:
    set.displayAllInClass("Subscriber");
    //ignores Application objects
    set.displayAllInClass("Application");
    //ignores Subscriber objects

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

    Default Re: Help with a specific display function

    Do Subscriber and Application have a shared parent class (other than Object) or interface? Do they both have a getClassName() method? I'm confused I think by the details that we don't see, but I can understand it since I'm guessing it's a pretty big project.

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

    Default Re: Help with a specific display function

    Sometimes I try to test a method in isolation with very simple versions of the classes of interest. For instance, if I make your method static and test it here:

    Java Code:
    public class Test2 {
       private static final int SIZE = 10;
       private static ParentClass[] elementList = new ParentClass[SIZE];
       private static int currentSize = elementList.length;
    
       public static void displayAllInClass(String theClassName) {
          System.out.println("Here.");
          if (currentSize == 0) {
             System.out.println("Here.");
             System.out.println("There are no objects in the set. ");
          } else {
             System.out.println("Here.");
             for (int i = 0; i < currentSize; i++) {
                System.out.println("Here.");
                if (theClassName.equals(elementList[i].getClassName())) {
                   System.out.println("Here.");
                   elementList[i].display();
                   System.out.println("Here.");
                   System.out.println("\n");
                   System.out.println("Here.");
                }
             }
          }
       }
       
       public static void main(String[] args) {
          for (int i = 0; i < elementList.length; i++) {
             ParentClass item = null;
             if (i % 2 == 0) { // if even
                item = new Subscriber();
             } else {
                item = new Application();
             }
             elementList[i] = item;
          }
          
          // try to display
          System.out.println("try application");
          displayAllInClass(Application.CLASS_NAME);
          
          System.out.println("try subscriber");
          displayAllInClass(Subscriber.CLASS_NAME);
       }
    }
    
    abstract class ParentClass {
    
       public abstract String getClassName();
    
       public abstract void display();
    
    }
    
    class Subscriber extends ParentClass {
    
       public static final String CLASS_NAME = "Subscriber";
    
       @Override
       public void display() {
          System.out.println("Subscriber Display");
       }
    
       @Override
       public String getClassName() {
          return CLASS_NAME;
       }
       
    }
    
    class Application extends ParentClass {
    
       public static final String CLASS_NAME = "Application";
    
       @Override
       public void display() {
          System.out.println("Application Display");
       }
    
       @Override
       public String getClassName() {
          return CLASS_NAME;
       }
       
    }
    It actually seems to test OK (again, I'd change the debug statements, but didn't do this yet for lack of time).

  7. #7
    Scopic is offline Member
    Join Date
    Oct 2011
    Posts
    24
    Rep Power
    0

    Default Re: Help with a specific display function

    Oh I'm so sorry!

    Yes! They both share the getClassName method from the Abstract class Element:

    Java Code:
    package assignment.pkg2;
    
    public abstract class Element {
        
        public String getClassName()
        { 
            String resultStr;
            
            int location;
            
            resultStr = this.toString();
            location = resultStr.indexOf('@');
            return resultStr.substring(0, location);
        }
        
        public abstract void readIn();
    
        
        public abstract void display();
    
        
        public abstract boolean equals(Element otherElement);
        
        @Override
        public abstract Element clone();
    
    }
    Hopefully that clears things up! I can't believe I forgot to mention that.

    Also, the class that handles the elementList is separate from the Element class, and is where the displayAllInClass method lies:

    Java Code:
    package assignment.pkg2;
    
    
    public class ElementSet {
        private Element[] elementList;
        
        private boolean[] flagged;
        
        private int currentIndex;
        private int currentSize;
        
        private final int MAXSETSIZE = 100;
        
        public ElementSet()
        {
            elementList = new Element[MAXSETSIZE];
            flagged = new boolean[MAXSETSIZE];
            
            for(int i = 0; i < MAXSETSIZE; i++)
            {
                flagged[i] = false;
            }
      
            currentIndex = -1;
            currentSize = 0;
        }
        
        public boolean isMemberOf(Element anElement)
        {
            String paramClass = anElement.getClassName();
            String currClass;
            
            for (int i = 0; i < currentSize; i++)
            {
                currClass = elementList[i].getClassName();
                
                if (currClass.equals(paramClass))
                {
                    if (elementList[i].equals(anElement))
                    {
                        return true;
                    }
                }
            }
            return false;
        }
        
        public boolean isFull()
        {
             return currentSize == MAXSETSIZE;
        }
        
        public boolean isEmpty()
        {
             return currentSize == 0;
        }
        
        public int size()
        {
             return currentSize;
        }
        
        public Element getCurrent()
        {
             // Local data ...
             int saveIndex = currentIndex;
           	
          	// Logic ...
             if (currentIndex == currentSize - 1)
             {
                // Recycle to beginning of list
                currentIndex = 0;
             }
             else
             {
                // Advance currentIndex to next object
                currentIndex++;
             }
             
          	// Return a reference to a clone of the current object
             return elementList[saveIndex].clone(); 
        }
        
        public int add(Element anElement)
        {
              // Logic ...
             if (currentSize == MAXSETSIZE)
             {
                 System.out.println("Set is full, member not added.");
                return 0;  // set is full
             }
             else if (this.isMemberOf(anElement))
             {
                 System.out.println("Member is already in the set.");
                return -1; // it's already in there
             }
           
             // We will add a clone of anElement to
          	// the set.
             elementList[currentSize] = anElement.clone();
           
             // Increment currentSize.
             currentSize++;
          
             // Set currentIndex to object we just added if it was the
             // first object in the set. 
             if (currentSize == 1) currentIndex = 0;
          
             // We succeeded.
             return 1;
        }
         
        public void clear()
          {
           	// Clean up the memory associated with this object 
          	// while it is still in use.
             for (int i = 0; i < currentSize; i++)
             {
                elementList[i] = null;
             }
             
          	// Reset currentSize and currentIndex to empty set 
          	// values.
             currentIndex = -1;
             currentSize = 0;
          }
       
         public void display()
          {
             if (currentSize == 0)
             {
                System.out.println("There are no objects in the set. ");
             }
             else
             {
                System.out.println("Here are the objects in the set: \n");
                for (int i = 0; i < currentSize; i++)
                {
                   elementList[i].display();
                   System.out.println("\n");
                }
             }
          
          }
       
    
        public int flagIt(Element anElement)
        {
            String paramClass = anElement.getClassName();
            String currClass;
            
            for (int i = 0; i < currentSize; i++)
            {
                currClass = elementList[i].getClassName();
                
                if (currClass.equals(paramClass))
                {
                    if (elementList[i].equals(anElement) && flagged[i] == true)
                    {
                        System.out.println("Member found, but has already been"
                                + " flagged.");
                        return 1;
                    }
                    
                    else if (elementList[i].equals(anElement) && flagged[i] != true)
                    {
                        System.out.println("Member found and is now"
                                + " flagged.");
                        flagged[i] = true;
                        return 0;
                    }
                }
            }
            System.out.println("Member not found.");
            return -1;
        }
        
        public int unFlagIt(Element anElement)
        {
            String paramClass = anElement.getClassName();
            String currClass;
            
            for (int i = 0; i < currentSize; i++)
            {
                currClass = elementList[i].getClassName();
                
                if (currClass.equals(paramClass))
                {
                    if (elementList[i].equals(anElement) && flagged[i] == true)
                    {
                        flagged[i] = false;
                        System.out.println("Member found, this member is no longer"
                                + " flagged");
                        return 1;
                    }
                    
                    else if (elementList[i].equals(anElement) && flagged[i] != true)
                    {
                        System.out.println("Member found, but is already "
                                + "unflagged");
                        return 0;
                    }
                }
            }
            System.out.println("Member not found.");
            return -1;    
        }
        
        public void displayAllInClass(String theClassName)
        {
            if (currentSize == 0)
             {
                System.out.println("There are no objects in the set. ");
             }
            else
            {
                for (int i = 0; i < MAXSETSIZE; i++)
                {
                   if(theClassName.equals(elementList[i].getClassName()))
                   {
                        elementList[i].display();
                        System.out.println("\n");
                   }
                }
            }
        }
    
    }
    Last edited by Scopic; 10-23-2011 at 01:19 AM.

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

    Default Re: Help with a specific display function

    No problem. I just made up an abstract parent class that I called ParentClass in my test program above.

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

    Default Re: Help with a specific display function

    Aha!!! further testing of your new code shows that you need to take packages into consideration! Do a println in the getClassName() method:

    Java Code:
             for (int i = 0; i < currentSize; i++) {
                System.out.println("Here.");
    
                // **** add this! ****
                System.out.printf("getClassName() returns: '%s'%n", elementList[i].getClassName());
    
                if (theClassName.equals(elementList[i].getClassName())) {
                   System.out.println("Here.");
                   elementList[i].display();
                   System.out.println("Here.");
                   System.out.println("\n");
                   System.out.println("Here.");
                }
             }
    Jos's poor man's debugger (println statements) to the rescue!

  10. #10
    Scopic is offline Member
    Join Date
    Oct 2011
    Posts
    24
    Rep Power
    0

    Default Re: Help with a specific display function

    Quote Originally Posted by Fubarable View Post
    Aha!!! further testing of your new code shows that you need to take packages into consideration! Do a println in the getClassName() method:

    Java Code:
             for (int i = 0; i < currentSize; i++) {
                System.out.println("Here.");
    
                // **** add this! ****
                System.out.printf("getClassName() returns: '%s'%n", elementList[i].getClassName());
    
                if (theClassName.equals(elementList[i].getClassName())) {
                   System.out.println("Here.");
                   elementList[i].display();
                   System.out.println("Here.");
                   System.out.println("\n");
                   System.out.println("Here.");
                }
             }
    Jos's poor man's debugger (println statements) to the rescue!
    Ah! Got it, THANK YOU very much!

    For those wondering, the function calls in main:

    Java Code:
    set.displayAllInClass("Subscriber");
    set.displayAllInClass("Application");
    Were not matching up with what the if statement was checking (packages). Knowing this, the revised and working calls would be:

    Java Code:
    set.displayAllInClass("assignment.pkg2.Subscriber");
    set.displayAllInClass("assignment.pkg2.Application");
    Thanks again!
    Fubarable likes this.

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

    Default Re: Help with a specific display function

    Quote Originally Posted by Scopic View Post
    Ah! Got it, THANK YOU very much!
    You're welcome!

    For those wondering, the function calls in main:

    Java Code:
    set.displayAllInClass("Subscriber");
    set.displayAllInClass("Application");
    Were not matching up with what the if statement was checking (packages). Knowing this, the revised and working calls would be:

    Java Code:
    set.displayAllInClass("assignment.pkg2.Subscriber");
    set.displayAllInClass("assignment.pkg2.Application");
    Thanks again!
    Or even better, let Java get the name for you:


    Java Code:
    set.displayAllInClass(Subscriber.class.getName());
    set.displayAllInClass(Application.class.getName());

Similar Threads

  1. Replies: 3
    Last Post: 04-29-2011, 02:08 AM
  2. Calling function in Javascript- from other function
    By jdigger in forum New To Java
    Replies: 1
    Last Post: 02-27-2011, 10:00 PM
  3. Replies: 3
    Last Post: 12-04-2009, 04:08 PM
  4. Replies: 8
    Last Post: 12-29-2008, 08:12 AM
  5. Replies: 2
    Last Post: 11-08-2008, 11:40 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
  •