reset password
Author Message
rabbott
Posts: 1649
Posted 12:15 Mar 03, 2014 |

class RestRoom


import java.util.*;
public class RestRoom {

    public static void runRestRoom() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Please enter a rest room size: ");
        int size = scanner.nextInt();
        RestRoom rr = new RestRoom(size);
        for (int i = 1; i <= size; i++) {
            rr.enter();
            System.out.println(rr);
        }
    }

    public boolean[] occupied;
    public int[] spaces;

    /**
     * Constructor for objects of class RestRoom
     */
    public RestRoom(int size){
        occupied = new boolean[size];
        Arrays.fill(occupied, false);
        spaces = new int[size];
        Arrays.fill(spaces, 0);    
        spaces[0] = size;
    }

    /**
     * Auxiliary constructor for use in JUnit
     */
    public
RestRoom(boolean[] initial) {
        occupied = initial;
    }

    public void enter() {             // First entry values
        int max = -1;                        // 10
        int pos = 0;                         //  0
        for (int i = 0; i < spaces.length; i++){
            ...
        }
        halfMax = max/2;                     //  5
        newGuyPosition = pos + halfMax;
        occupied[newGuyPosition] = true;
        spaces[newGuyPosition] = 0
        spaces[newGuyPosition + 1] =         //  4
        spaces[pos] =                        //  5
    }

    public String toString() {
        StringBuilder sb = new StringBuilder();
        for (boolean occ: occupied) {
            if (occ) sb.append('x');
            else sb.append('_');
        }
        return sb.toString();
    }
}

 

JUnit test for toString

    @Test
    public void testToString() {
        RestRoom rr = new RestRoom(new boolean[]{false, true, true});
        assertEquals("_xx", rr.toString());
        rr = new RestRoom(new boolean[]{true, true, false, true, false, true});
        assertEquals("xx_x_x", rr.toString());
    }

Last edited by rabbott at 12:39 Mar 03, 2014.