Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 3 of 3 FirstFirst 123
Results 51 to 65 of 65

Thread: Stuck and can't get past it!

  1. #51
    Member
    Join Date
    Feb 2012
    Posts
    43
    My Mood
    Confused
    Thanks
    33
    Thanked 0 Times in 0 Posts

    Default Re: Stuck and can't get past it!

    It was put into the array and then set out so that it could get more information. So, would this one need to return anything at all, since it is merely setting a portion to null once more?

    And okay, so something like this may work?

     if (x >= 0)
             {
                  //Removes a specific person from the bus array
     
                   name = null;
             }
    Since wouldn't that set the name desired to null again?

  2. #52
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,088
    Thanks
    52
    Thanked 2,284 Times in 2,255 Posts

    Default Re: Stuck and can't get past it!

    You should test each method as you write it to be sure it is working before going to the next method.
    Have you tested the previous method(s) that you wrote?
    If not, you need to write a testing program to call the method and check that it works properly.
    Call it when the names array is empty, when it has one entry and some empty slots and again when it is full.

  3. #53
    Member
    Join Date
    Feb 2012
    Posts
    43
    My Mood
    Confused
    Thanks
    33
    Thanked 0 Times in 0 Posts

    Default Re: Stuck and can't get past it!

    I've tested them. They all work so far. I'm just confused how to get the name in there to set it to null. I thought that using if x>= 0, then following that with names = null; would work. And would I not need to return a null at the end of the method?

  4. #54
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,088
    Thanks
    52
    Thanked 2,284 Times in 2,255 Posts

    Default Re: Stuck and can't get past it!

    I listed two steps that needed to be done in this method.
    Have you gotten the first one to work?
    1) find a specific name,

    Write and test a method that just does that one step.

  5. The Following User Says Thank You to Norm For This Useful Post:

    Wonderlandslost (February 13th, 2012)

  6. #55
    Member
    Join Date
    Feb 2012
    Posts
    43
    My Mood
    Confused
    Thanks
    33
    Thanked 0 Times in 0 Posts

    Default Re: Stuck and can't get past it!

    Okay, now I'm having some troubles with the test method and my getOn method;

     public String sample(String name)
        {
            name = "Anna";
            names.getOn(name);
            return name;
        }

     public String getOn(String name)
        {
            //Use the findFirstEmpty to locate the first null seat
            int x = findFirstEmpty();
            //Take the empty seat and put the child in it
             if (x == -1)
             {
                    //indicates that the array is full
                    System.out.print("The bus is at maximum capacity");
             }
             else
             {
                 //Indicates that the scan located a null spot in the array
                 name = names[x];
             }        
            //Return the seat with child in it.
            return name;
        }

    In the test method, I should be able to just do a names.getOn(name) right?

  7. #56
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,088
    Thanks
    52
    Thanked 2,284 Times in 2,255 Posts

    Default Re: Stuck and can't get past it!

    Can you explain what the problem is?
    Post all of the code and its output and add comments explaining what is wrong.

  8. #57
    Member
    Join Date
    Feb 2012
    Posts
    43
    My Mood
    Confused
    Thanks
    33
    Thanked 0 Times in 0 Posts

    Default Re: Stuck and can't get past it!

     
    /*
     * Author: Rachel Moss
     * Class: CS 3100
     * Teacher: Dr. Silverman
     * 
     * Compiles:
     * Works:
     */
    package schoolbus;  //Check this portion 2/10/12
     
    public class SchoolBus 
    {
     
        //Declare array for the schoolbus to use
        public String names[] = new String[10];
     
        /*
         * Description: This method will allow the children to get on the bus and work with
         *              findFirstEmpty to place them in the first available seat.
         * Precondition: The bus array must already exist and have been set to null.
         * Postcondition:  The child will reside in the array (on the bus).
         */
        public String getOn(String name)
        {
            //Use the findFirstEmpty to locate the first null seat
            int x = findFirstEmpty();
            //Take the empty seat and put the child in it
             if (x == -1)
             {
                    //indicates that the array is full
                    System.out.print("The bus is at maximum capacity");
             }
             else
             {
                 //Indicates that the scan located a null spot in the array
                 name = names[x];
             }        
            //Return the seat with child in it.
            return name;
        }
     
        public String sample(String name)
        {
            name = "Anna";
            names.getOn(name);
            return name;
        }
     
         /**
         * Description: This method will search for the first empty spot in the array 
         *  (the bus).
         * Precondition: The bus array must already exist and there must be at least
         *  one child on the bus.
         * Postcondition: There will now be one less empty spot since a 'child' will 
         *  be residing in the formerly empty location.
         */
        public int findFirstEmpty()
        {
            /**
             * Function of this method is to scan the array for the first null 
             * position  
             */    
            for (int i = 0; i < 10; i++)
    	{
                /**
                 * Scan the array by using int i to correspond to the position of the 
                 * 'seat' in the 'bus'
                 */
                if (names[i] != null)
                {
                    //Indicates that the scan located a null spot in the array
                    return i;
                }
          	}
            //Indicates that the scan produced no null spots in array
            return -1; 
         }
     
     
        /**
         * Description: This method will print all of the occupied seats.
         * Precondition: The bus array must already exst and there must be at least
         *  one child on the bus.
         * Postcondition:  There will be a printout of all of the occupied seats and
         *  who is occupying them.
         */
        public void print()
        {
            //Print out the array and the position of the item in the array.
            for (int i = 0; i < 10; i++)
            {
               if (names[i] != null)
               {
                   System.out.println();
               }
               else
               {
     
               }
            }
     
        }
     
     
        /**
         * Description: This method is when the bus stops and a child gets off, 
         *  freeing up a seat.
         * Precondition: The bus array must already exist and there must be at least
         *  one child on the bus.
         * Postcondition: There will be one more empty spot since the child that was
         *  residing in that spot got off at the bus stop.
         */
        public String getOff(String name)
        {
     
            //Use the findFirstEmpty to set the formerly filled seat to null
            int x = findFirstEmpty();
            //Take the filled seat and remove the child
             if (x >= 0)
             {
                  //Removes a specific person from the bus array
     
                   name = null;
             }
             else
             {
                 System.out.println("The bus contained no children.");
     
             }        
            //Return the seat with child in it.
            return null;
             //Change the child's name to 'empty' to signify their leaving the bus.
        }
     
     
        /**
         * Description: This method is used for when a 'child' is being too roudy and 
         *  has to switch seats with another to calm them down.
         * Precondition: The bus array must alraedy exist and there must be at least 
         *  two children on the bus.
         * Postcondition: The 'children' who were chosen for the swap will be 
         *  located in different seats.
         */
        public void swap(String name1, String name2)
        {
     
            String temp = name1;
            name1 = name2;
            name2 = temp;       
        }
     
        public static void main(String[] args) 
        {
            // initialize schoolbus class
     
            System.out.println("The bus pulls up and the children load onto it.");
     
     
     
            //Anna enters bus and sits in the first empty seat
     
     
     
            //Nancy enters the bus and sits in the first empty seat.
     
     
            //Erica enters the bus and sits in the first empty seat
     
     
            //Karen enters the bus and sits in the first empty seat.
     
            //Joe enters the bus and sits in the first empty seat.
     
     
            /**
            * Print the bus with the children in it, include the message "after 
            * loading kids, this is what the bus contains"
            */
            System.out.print("After loading the children onto the bus, this is who the bus contains: ");
            System.out.println();
            //Swap Nancy and Karen
     
     
            /**
            * Print the bus with the children in it, include the message "after 
            * swapping the children, this is what the bus contains"
            */
            System.out.print("After swapping Nancy and Karen's seats, this is what remains on the bus: ");
            System.out.println();
     
            //Erica exits the bus at her stop
     
     
            //Nancy exits the bus at her stop
     
     
            /**
            * Print the bus with the children in it, include the message "after 
            * Nancy left, this is what the bus contains" 
            * ASK IF WE NEED TO TYPE AFTER ERICA LEFT!!!!
            */
            System.out.print("After Nancy and Erica exited the bus, this is who remains: ");
            System.out.println();
     
            //New child, Mike, enters bus and takes the first available seat.
     
     
            /**
            * Print the bus with the children in it, include the message "after Mike
            * enters the bus, this is what the bus contains"
            */
            System.out.print("After Mike enters the bus, this is who the bus contains: ");
            System.out.println();
        }
    }

    in my sample method, I'm getting an error where it doesn't recognize the getOn command when I type names.getOn

  9. #58
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,088
    Thanks
    52
    Thanked 2,284 Times in 2,255 Posts

    Default Re: Stuck and can't get past it!

    it doesn't recognize the getOn command when I type names.getOn
    names is a String array. It does not have any methods named getOn()

    What is the statement: names.getOn
    supposed to do? It does not make any sense.

    Where did you test the previous methods that you wrote?
    I don't see any tests being done in the code you posted.

    You need to test the methods before you continue. Add some testing code to the clsas before you try to write another method.
    Last edited by Norm; February 13th, 2012 at 03:07 PM.

  10. #59
    Member
    Join Date
    Feb 2012
    Posts
    43
    My Mood
    Confused
    Thanks
    33
    Thanked 0 Times in 0 Posts

    Default Re: Stuck and can't get past it!

    I was mistaken, thinking that names was something I could use there. I corrected that. I'm now testing in my main method;

     
    /*
     * Author: Rachel Moss
     * Class: CS 3100
     * Teacher: Dr. Silverman
     * 
     * Compiles:
     * Works:
     */
    package schoolbus;  //Check this portion 2/10/12
     
    public class SchoolBus 
    {
     
        //Declare array for the schoolbus to use
        public String names[] = new String[10];
     
        /*
         * Description: This method will allow the children to get on the bus and work with
         *              findFirstEmpty to place them in the first available seat.
         * Precondition: The bus array must already exist and have been set to null.
         * Postcondition:  The child will reside in the array (on the bus).
         */
        public String getOn(String name)
        {
            //Use the findFirstEmpty to locate the first null seat
            int x = findFirstEmpty();
            //Take the empty seat and put the child in it
             if (x == -1)
             {
                    //indicates that the array is full
                    System.out.print("The bus is at maximum capacity");
             }
             else
             {
                 //Indicates that the scan located a null spot in the array
                 name = names[x];
             }        
            //Return the seat with child in it.
            return name;
        }
     
     
         /**
         * Description: This method will search for the first empty spot in the array 
         *  (the bus).
         * Precondition: The bus array must already exist and there must be at least
         *  one child on the bus.
         * Postcondition: There will now be one less empty spot since a 'child' will 
         *  be residing in the formerly empty location.
         */
        public int findFirstEmpty()
        {
            /**
             * Function of this method is to scan the array for the first null 
             * position  
             */    
            for (int i = 0; i < 10; i++)
    	{
                /**
                 * Scan the array by using int i to correspond to the position of the 
                 * 'seat' in the 'bus'
                 */
                if (names[i] != null)
                {
                    //Indicates that the scan located a null spot in the array
                    return i;
                }
          	}
            //Indicates that the scan produced no null spots in array
            return -1; 
         }
     
     
        /**
         * Description: This method will print all of the occupied seats.
         * Precondition: The bus array must already exst and there must be at least
         *  one child on the bus.
         * Postcondition:  There will be a printout of all of the occupied seats and
         *  who is occupying them.
         */
        public void print()
        {
            //Print out the array and the position of the item in the array.
            for (int i = 0; i < 10; i++)
            {
               if (names[i] != null)
               {
                   System.out.println();
               }
               else
               {
     
               }
            }
     
        }
     
     
        /**
         * Description: This method is when the bus stops and a child gets off, 
         *  freeing up a seat.
         * Precondition: The bus array must already exist and there must be at least
         *  one child on the bus.
         * Postcondition: There will be one more empty spot since the child that was
         *  residing in that spot got off at the bus stop.
         */
        public String getOff(String name)
        {
     
            //Use the findFirstEmpty to set the formerly filled seat to null
            int x = findFirstEmpty();
            //Take the filled seat and remove the child
             if (x >= 0)
             {
                  //Removes a specific person from the bus array
     
                   name = null;
             }
             else
             {
                 System.out.println("The bus contained no children.");
     
             }        
            //Return the seat with child in it.
            return null;
             //Change the child's name to 'empty' to signify their leaving the bus.
        }
     
     
        /**
         * Description: This method is used for when a 'child' is being too roudy and 
         *  has to switch seats with another to calm them down.
         * Precondition: The bus array must alraedy exist and there must be at least 
         *  two children on the bus.
         * Postcondition: The 'children' who were chosen for the swap will be 
         *  located in different seats.
         */
        public void swap(String name1, String name2)
        {
     
            String temp = name1;
            name1 = name2;
            name2 = temp;       
        }
     
        public static void main(String[] args) 
        {
            // initialize schoolbus class
     
            System.out.println("The bus pulls up and the children load onto it.");
     
            SchoolBus n1 = new SchoolBus();
     
            //Anna enters bus and sits in the first empty seat
            String name = "Anna";
            n1.getOn(name);
            n1.print();
     
            //Nancy enters the bus and sits in the first empty seat.
     
     
            //Erica enters the bus and sits in the first empty seat
     
     
            //Karen enters the bus and sits in the first empty seat.
     
            //Joe enters the bus and sits in the first empty seat.
     
     
            /**
            * Print the bus with the children in it, include the message "after 
            * loading kids, this is what the bus contains"
            */
            System.out.print("After loading the children onto the bus, this is who the bus contains: ");
            System.out.println();
            //Swap Nancy and Karen
     
     
            /**
            * Print the bus with the children in it, include the message "after 
            * swapping the children, this is what the bus contains"
            */
            System.out.print("After swapping Nancy and Karen's seats, this is what remains on the bus: ");
            System.out.println();
     
            //Erica exits the bus at her stop
     
     
            //Nancy exits the bus at her stop
     
     
            /**
            * Print the bus with the children in it, include the message "after 
            * Nancy left, this is what the bus contains" 
            * ASK IF WE NEED TO TYPE AFTER ERICA LEFT!!!!
            */
            System.out.print("After Nancy and Erica exited the bus, this is who remains: ");
            System.out.println();
     
            //New child, Mike, enters bus and takes the first available seat.
     
     
            /**
            * Print the bus with the children in it, include the message "after Mike
            * enters the bus, this is what the bus contains"
            */
            System.out.print("After Mike enters the bus, this is who the bus contains: ");
            System.out.println();
        }
    }

    As of this time, this is what is printing, despite my putting in the string name Anna;

    The bus is at maximum capacity

  11. #60
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,088
    Thanks
    52
    Thanked 2,284 Times in 2,255 Posts

    Default Re: Stuck and can't get past it!

    Where is the test code for the findFirstEmpty() method?
    I suggested that you test it with three different array contents.

    For example: Manually create an array: names = new String[]{"aname"};
    then call findFreeEmpty() and print out what it returns

  12. The Following User Says Thank You to Norm For This Useful Post:

    Wonderlandslost (February 13th, 2012)

  13. #61
    Member
    Join Date
    Feb 2012
    Posts
    43
    My Mood
    Confused
    Thanks
    33
    Thanked 0 Times in 0 Posts

    Default Re: Stuck and can't get past it!

    So, I tried doing what you said, I had troubles with it, but i was looking at the code once more and it looks as if the findFirstEmpty is only returning -1.

    It'll go through the for loop returning my int i, but the return -1 is outside the for look, which is what it returned overall.

    public int findFirstEmpty()
        {
            /**
             * Function of this method is to scan the array for the first null 
             * position  
             */    
            for (int i = 0; i < 10; i++)
    	{
                /**
                 * Scan the array by using int i to correspond to the position of the 
                 * 'seat' in the 'bus'
                 */
                if (names[i] != null)
                {
                    //Indicates that the scan located a null spot in the array
                    return i;
                }
          	}
            //Indicates that the scan produced no null spots in array
            return -1; 
     
         }

  14. #62
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,088
    Thanks
    52
    Thanked 2,284 Times in 2,255 Posts

    Default Re: Stuck and can't get past it!

    You need to show how you are testing the method. What does the array contain when the method is called?
    What response do you expect when you call the method?


    if (names[i] != null)
    Do you know what the operator != does? When will the above condition be true?

  15. The Following User Says Thank You to Norm For This Useful Post:

    Wonderlandslost (February 13th, 2012)

  16. #63
    Member
    Join Date
    Feb 2012
    Posts
    43
    My Mood
    Confused
    Thanks
    33
    Thanked 0 Times in 0 Posts

    Default Re: Stuck and can't get past it!

    I do not know that the != operator does. I do know that that will be true when there is an empty spot, or a null spot, in the array.

    I also ran the program, printing out what x was from the findFirstEmpty in getOn, it is sending a -1 as the value.

    public String getOn(String name)
        {
            //Use the findFirstEmpty to locate the first null seat
            int x = findFirstEmpty();
            //Take the empty seat and put the child in it
            System.out.println(x);
             if (x == -1)
             {
                    //indicates that the array is full
                    System.out.print("The bus is at maximum capacity");
             }
             else
             {
                 //Indicates that the scan located a null spot in the array
                 name = names[x];
             }        
            //Return the seat with child in it.
            return name;
        }
     
     
         /**
         * Description: This method will search for the first empty spot in the array 
         *  (the bus).
         * Precondition: The bus array must already exist and there must be at least
         *  one child on the bus.
         * Postcondition: There will now be one less empty spot since a 'child' will 
         *  be residing in the formerly empty location.
         */
        public int findFirstEmpty()
        {
            /**
             * Function of this method is to scan the array for the first null 
             * position  
             */    
            for (int i = 0; i < 10; i++)
    	{
                /**
                 * Scan the array by using int i to correspond to the position of the 
                 * 'seat' in the 'bus'
                 */
                if (names[i] != null)
                {
                    //Indicates that the scan located a null spot in the array
                    return i;
                }
          	}
            //Indicates that the scan produced no null spots in array
            return -1; 
     
         }

  17. #64
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,088
    Thanks
    52
    Thanked 2,284 Times in 2,255 Posts

    Default Re: Stuck and can't get past it!

    If you don't understand what the != operator does or how to use it, how do you expect to be able to write a program?

    My suggestion is that you go back a couple of chapters and start again. There are many, many computer programming concepts that you do not seem to understand. Too many for me to help you with.

  18. The Following User Says Thank You to Norm For This Useful Post:

    Wonderlandslost (February 13th, 2012)

  19. #65
    Member
    Join Date
    Feb 2012
    Posts
    43
    My Mood
    Confused
    Thanks
    33
    Thanked 0 Times in 0 Posts

    Default Re: Stuck and can't get past it!

    Okay, I reviewed the chapters and figured out the meaning. That means not equal to.

Page 3 of 3 FirstFirst 123

Similar Threads

  1. [SOLVED] Nothing really wrong but i cant seem to get past a stone wall
    By Lurie1 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: February 1st, 2012, 11:35 AM
  2. Please help not sure how to get past the Syntax error
    By KnightC in forum Other Programming Languages
    Replies: 2
    Last Post: December 23rd, 2011, 09:09 AM
  3. problems getting past the out of bounds error
    By dbdny in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 28th, 2011, 05:57 PM
  4. Getting date based on days past year
    By aussiemcgr in forum Java Theory & Questions
    Replies: 3
    Last Post: July 14th, 2010, 04:06 PM
  5. Having Issues Past this
    By baGoGoodies111 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 12th, 2009, 07:19 PM