1,105,578 Community Members

I don't know where I went wrong!

Member Avatar
wonderlandslost
Newbie Poster
19 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hello! So I've been working on a 1D array that represents a school bus that can hold up to 10 children. The array needs to have names put into it in the first available 'seat'. I've constructed code to do so, but something seems to be wrong. The findFirstEmpty method seems to only return 0's instead of incrementing. I'm not sure where I went wrong. I am also having trouble figuring out how to get this to print like this; "0, Anna". If anyone could just give a small amount of advice, I would really appreciate it. I'm just hoping I can figure it out tonight, since it's due tomorrow!! Thank you all for your time!

package schoolbus;  

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
        System.out.println(x);
        if (x >= 0)
        {
            names[x] = name;
        }
        else
        {
            //indicates that the array is full
            System.out.println("The bus is at maximum capacity");
        }
        //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 <= names.length; 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 x = 0; x < 10; x++)
        {
           if (names[x] != null)
           {
               System.out.println(x + " , " + names[x]);
           }
       }
        
    }
    
    
    /**
     * 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 String swap(String name1, String name2)
    {
         
        String temp = name1;
        name1 = name2;
        name2 = temp;
        return name1;
    }
    
    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);
               
        //Nancy enters the bus and sits in the first empty seat.
        n1 = new SchoolBus();
        name = "Nancy";
        n1.getOn(name);

        //Erica enters the bus and sits in the first empty seat
        n1 = new SchoolBus();
        name = "Erica";
        n1.getOn(name);

        //Karen enters the bus and sits in the first empty seat.
        n1 = new SchoolBus();
        name = "Karen";
        n1.getOn(name);

        //Joe enters the bus and sits in the first empty seat.
        n1 = new SchoolBus();
        name = "Joe";
        n1.getOn(name);

        /**
        * 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: ");
        n1.print();
        System.out.println();
        
        //Swap Nancy and Karen
       // n1.swap("Karen", "Nancy");

        /**
        * 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: ");
        n1.print();
        System.out.println();

        //Erica exits the bus at her stop
        n1 = new SchoolBus();
        name = "Erica";
        n1.getOff(name);

        //Nancy exits the bus at her stop
        n1 = new SchoolBus();
        name = "Nancy";
        n1.getOff(name);

        /**
        * Print the bus with the children in it, include the message "after 
        * Nancy left, this is what the bus contains" 
        */
        System.out.print("After Nancy and Erica exited the bus, this is who remains: ");
        n1.print();
        System.out.println();

        //New child, Mike, enters bus and takes the first available seat.
        name = "Mike";
        n1.getOn(name);

        /**
        * 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: ");
        n1.print();
        System.out.println();
    }
}
Member Avatar
zeroliken
Nearly a Posting Virtuoso
1,222 posts since Nov 2011
Reputation Points: 79 [?]
Q&As Helped to Solve: 216 [?]
Skill Endorsements: 15 [?]
 
1
 
SchoolBus n1 = new SchoolBus();

Instantiate only once, like only at line 144 and removes other initializations like

n1 = new SchoolBus();

you need not to create a new object everytime and overwrite the contents

Member Avatar
wonderlandslost
Newbie Poster
19 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 
SchoolBus n1 = new SchoolBus();

Instantiate only once, like only at line 144 and removes other initializations like

n1 = new SchoolBus();

you need not to create a new object everytime and overwrite the contents

Oh! That made it work a lot better. My notes from class said I had to do that every time. So, when I was trying that, was it just emptying the array over and over again instead of filling it?
Also, I know there is something wrong with my getOff method. It isn't removing the children from the bus. Same with my swap method.

Thank you so much for getting back to me! I was getting extremely panicked about this project. I really appreciate your helping!

Member Avatar
zeroliken
Nearly a Posting Virtuoso
1,222 posts since Nov 2011
Reputation Points: 79 [?]
Q&As Helped to Solve: 216 [?]
Skill Endorsements: 15 [?]
 
0
 

Oh! That made it work a lot better. My notes from class said I had to do that every time. So, when I was trying that, was it just emptying the array over and over again instead of filling it?

It will not overwrite the contents if you use a new variable other than n1, though I think you don't need to in this case

I know there is something wrong with my getOff method.

instead of trying to find an empty seat like,

int x = findFirstEmpty();

I think you just need to find the array index with the name you need to remove,
As for the swap you need to use the array and swap the values in them

Member Avatar
wonderlandslost
Newbie Poster
19 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

It will not overwrite the contents if you use a new variable other than n1, though I think you don't need to in this case

instead of trying to find an empty seat like,

int x = findFirstEmpty();

I think you just need to find the array index with the name you need to remove,
As for the swap you need to use the array and swap the values in them

I know that normally I could do;
temp = names[1]; // holding variable
names[1] = names[2];
names[2] = temp;
Or something along those lines. But what do I return?
If I figured out what numbers those two names were by printing it before hand and set swap to those specific numbers, would that work?

As for locating the index of what I need to remove, how do I locate the specific string name?

Member Avatar
zeroliken
Nearly a Posting Virtuoso
1,222 posts since Nov 2011
Reputation Points: 79 [?]
Q&As Helped to Solve: 216 [?]
Skill Endorsements: 15 [?]
 
0
 

But what do I return?

I think it's better if you use public void so there won't be a need for a return statement cause all you need is to change the value
as for checking if the name is equal, loop through the array then

if (name.equals(names[x]))

delete the contents of that array index

Member Avatar
wonderlandslost
Newbie Poster
19 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I think it's better if you use public void so there won't be a need for a return statement cause all you need is to change the value
as for checking if the name is equal, loop through the array then

if (name.equals(names[x]))

delete the contents of that array index

Okay, that makes a lot more sense. Now using the if statement that your stated, I did this;

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 (name.equals(names[x]))
         {
              //Removes a specific person from the bus array
              names[x] = null;
               
         }
//         else
//         {
//             System.out.println("The bus contained no children.");
//             
//         }        
        //Return the seat with child in it.
        return name;
    }

But it's still not removing the person from the arry. Is there something I did wrong with that?

Member Avatar
zeroliken
Nearly a Posting Virtuoso
1,222 posts since Nov 2011
Reputation Points: 79 [?]
Q&As Helped to Solve: 216 [?]
Skill Endorsements: 15 [?]
 
1
 

1.

int x = findFirstEmpty();

remove this you don't need to find an empty seat
2. use a loop to check the content of the array
3. use public void cause there's no need to return anything

Member Avatar
wonderlandslost
Newbie Poster
19 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

1.

int x = findFirstEmpty();

remove this you don't need to find an empty seat
2. use a loop to check the content of the array
3. use public void cause there's no need to return anything

It works! Thank you so much! I have one final question, my teacher wants the package name to be bus, but I don't know how to set that to bus when the program's name is SchoolBus. Is there a way to do that?

Member Avatar
zeroliken
Nearly a Posting Virtuoso
1,222 posts since Nov 2011
Reputation Points: 79 [?]
Q&As Helped to Solve: 216 [?]
Skill Endorsements: 15 [?]
 
0
 

set this at the topmost

package bus;

the package is different than the public class, it doesn't affect the class name only the access to it
check these links for more info
http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html
http://stackoverflow.com/questions/215497/in-java-whats-the-difference-between-public-default-protected-and-private

PS. if the problem is solved then you can mark this thread as solved, have a nice day :)

Member Avatar
wonderlandslost
Newbie Poster
19 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Okay :) Thank you so much! I don't think I would've finished without your help!

Question Answered as of 2 Years Ago by zeroliken
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: