Hello everyone,
I'm currently working on a java problem that does the following;
1. There will be method getOn(String name).
2. Method int findFirstEmpty() will find first empty seat. <-where it puts name in array
3. print() //print all occupied seats.
4. getOff(String name) //bus stops and kid leaves.
5. swap(String name1, String name2) //if some kid misbehaves the driver will make him swap seats.
6. In the main method submit with the following operations.
7. anna comes in and finds first empty seat
8. nancy comes in and finds first empty seat
9. erica comes in and finds first empty seat
10. karen comes in and finds first empty seat
11. joe comes in and finds first empty seat
12. print all //print message what you are printing "after loading kids"
13. swap nancy and karen
14. print all // print message what you are printing "after swapping kids"
15. bus stops, erica leaves.
16. bus stops, nancy leaves.
17. Print all. //announce what you are printing "after nancy left"
18. Bus stops, new kid (mike) is standing on the corner wants to take a ride, so give him first available seat in the bus.
19. Print all in the above format. //announce what you are printing "after mike came on"
It is a 1-d array with the size of 10 (representing 10 seats on a bus). I have to place each child in the first available seat, hence the findFirstEmpty function. The problem I'm having is mainly with the getOn portion and the findFirstEmpty portion;
package schoolbus; import java.util.*; import java.io.*; public class SchoolBus { //Declare array for the schoolbus to use private String[] names = new String[10]; /* * Description: This method sets the array to null, which will indicate where * empty seats are later on. * Precondition: The array must have already been created. * Postcondition: The array will be filled with null. */ public SchoolBus() { String names[] = null; } /* * 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 void getOn(String name) { //Read the name into the method. //Use the findFirstEmpty to locate the first null seat and fill it. name.findFirstEmpty(); //Return the seat with child in it. } /** * Description: This method will search for the first empty spot in the array * (the bus) and place the 'child' in that empty seat. * 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() { //Where it puts the name into the array //If there is nobody in the seat, put the child in that seat. String n; for (int i = 0; i < 100; i++) { if (names[i] != null) { n = names[i]; n.getOn(names); } else { } } return names; //create string 'empty' and search for that } /** * 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 < names.length; 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 void getOff(String name) { //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 //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: "); //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(); } }
I would really appreciate a nudge in the right direction, or an example of something similar(I learn well from related examples) Thank you all so much in advanced! I really just want to understand how this works!