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();
}
}