Skip to main content
fixed the block of code
Source Link
Doi9t
  • 3.4k
  • 3
  • 11
  • 23
 public static void main(String[] args) {
    int[][] plane;//this is the array that will be plugged into the methods
    plane = fillSeatsRandomly();//creates the 12x4 array that will be used
    displaySeats(plane);//shows the user what seats are available/taken
    isSeatAvailable(plane);//allows the user to see if a seat is available
    reserveSeat(plane);//allows the user to reserve a seat
    displaySeats(plane);//shows the user their seat has been reserved
    seatsAvailInRow(plane);//shows how many seats are avail in given row
    findRowWithTwoSeats(plane);//finds closest row with adjacent seats
    countSeatsAvail(plane);//counts how many seats are available
    countTakenSeats(plane);//counts how many seats are taken
}

public static void displaySeats(int seats[][]) {//displays the seats
    System.out.print("\tA  \tB  \tC  \tD \n");
    for (int row = 0; row < seats.length; row++) {
        System.out.print(row + 1 + "");
        for (int seatNum = 0; seatNum < seats[row].length; seatNum++) {
            if (seats[row][seatNum] == 1) {
                System.out.print("\tX  ");
            } else {
                System.out.print("\t0  ");
            }
        }
        System.out.println();
    }
}

public static int[][] fillSeatsRandomly() {//fills seats randomly, returns
    //an array of seats either filled or empty 
    int[][] rndm = new int[12][4];
    for (int[] rndm1 : rndm) {
        for (int col = 0; col < rndm1.length; col++) {
            rndm1[col] = (int) Math.round(Math.random());
        }
    }
    return rndm;//return the array
}//end of fillSeatsRandomly

public static boolean isSeatAvailable(int[][] plane) {//asks user for input
    //checks if seat is available, returns true if it is and false if not
    Scanner input = new Scanner(System.in);
    int rowNum;//row number of the seat chosen
    int colNum = 4;//column of the seat chosen
    char seatSelect;//character of the seat chosen
    String str;//used to get char value for switch
    System.out.println("Choose which row you would like to check (1-12)");
    rowNum = input.nextInt() - 1;
    System.out.println("Choose which seat you would like: A,B,C, or D (Case"
            + " Sensitive)");
    str = input.next();
    seatSelect = str.charAt(0);
    switch (seatSelect) {
        case 'A':
            colNum = 0;
            break;
        case 'B':
            colNum = 1;
            break;
        case 'C':
            colNum = 2;
            break;
        case 'D':
            colNum = 3;
    }//end of switch
    if (colNum > 3 || colNum < 0 || rowNum > 11 || rowNum < 0) {
        System.out.println("Invalid Selection");
        return false;
    }//end of if
    if (plane[rowNum][colNum] != 0) {
        System.out.println("Sorry, seat is taken");
        return false;
    } else {
        System.out.println("This seat is available");
        return true;
    }//end of else
}//end of isSeatAvail

public static int reserveSeat(int[][] plane) {//asks user for input
    //reserves a seat based on user input, returns updated value to array
    Scanner input = new Scanner(System.in);
    int rowNum;//Row number of seat chosen
    int colNum = 4;//Column number of seat chosen
    char seatSelect;//Character of seat chosen
    String str;//used to get char value for switch
    System.out.println("Choose which row you would like to reserve (1-12)");
    rowNum = input.nextInt() - 1;
    System.out.println("Choose which seat you would like: A,B,C, or D (Case"
            + " Sensitive)");
    str = input.next();
    seatSelect = str.charAt(0);
    switch (seatSelect) {
        case 'A':
            colNum = 0;
            break;
        case 'B':
            colNum = 1;
            break;
        case 'C':
            colNum = 2;
            break;
        case 'D':
            colNum = 3;
    }//end of switch
    if (colNum > 3 || colNum < 0 || rowNum > 11 || rowNum < 0) {
        System.out.println("Invalid Selection");

    }//end of if
    if (plane[rowNum][colNum] != 0) {
        System.out.println("Sorry, seat is taken");

    } else {
        System.out.println("Your seat has been reserved");

    }//end of if else
    return plane[rowNum][colNum] = 1;//return updated array value
} //end of reserveSeat

public static int seatsAvailInRow(int[][] plane) {//checks how many seats
    //are available in a row determined by user input returns how many seats
    Scanner input = new Scanner(System.in);
    int rowNum;
    int rowSum = 0;
    int avail;
    System.out.println("Choose which row to see how many seats are"
            + " available (1-12)");
    rowNum = input.nextInt() - 1;
    for (int[] plane1 : plane) {
        rowSum = 0;
        for (int j = 0; j < plane1.length; j++) {
            rowSum += plane[rowNum][j];
        } //end of nested for
    } //end of for
    avail = 4 - rowSum;
    System.out.println("There is " + avail + " seat(s) available in that "
            + "row");
    return avail;//returns number of seats available
}//end of seatsAvailInRow

public static int findRowWithTwoSeats(int[][] plane) { //finds the closest
    //row that has 2 adjacent seats, returns row num, or 0 if none
    for (int i = 0; i < plane.length; i++) {
        for (int j = 0; j < plane[i].length; j++) {
        if (plane[i][0] + plane[i][1] == 0 || plane[i][1] + plane[i][2] == 0
                    || plane[i][2] + plane[i][3] == 0) {
                System.out.println("Row " + (i + 1)
                        + " has adjacent seats");
                return i;//returns row num with adjacent seats
            }//end of if
        } //end of nested for
    } //end of for
    return 0;//returns 0 if there is no adjacent seats
}//end of findRowWithTwoSeats

public static int countSeatsAvail(int[][] plane) {//counts how many seats
    //are available on the plane, returns int value for number of seats
    int count = 0;//used to keep track of how many seats are available
    for (int i = 0; i < plane.length; i++) {
        for (int j = 0; j < plane[i].length; j++) {
            if (plane[i][j] == 0) {
                count++;
            }//end of if
        }//end of nested for
    }//end of for
    System.out.println("There are " + count + " seats available");
    return count;//returns the number of seats available
}//end of countSeatsAvail

public static int countTakenSeats(int[][] plane) {//counts number of seats
    //taken on the plane, returns int value for number of seats taken
    int count = 0;//used to keep track of how many seats are taken
    for (int i = 0; i < plane.length; i++) {
        for (int j = 0; j < plane[i].length; j++) {
            if (plane[i][j] == 1) {
                count++;
            }
        }//end of nested for
    }//end of for
    System.out.println("There are " + count + " seats taken");
    return count;//returns number of seats taken
}//end of countTakenSeats

}//end of class

     public static void main(String[] args) {
        int[][] plane;//this is the array that will be plugged into the methods
        plane = fillSeatsRandomly();//creates the 12x4 array that will be used
        displaySeats(plane);//shows the user what seats are available/taken
        isSeatAvailable(plane);//allows the user to see if a seat is available
        reserveSeat(plane);//allows the user to reserve a seat
        displaySeats(plane);//shows the user their seat has been reserved
        seatsAvailInRow(plane);//shows how many seats are avail in given row
        findRowWithTwoSeats(plane);//finds closest row with adjacent seats
        countSeatsAvail(plane);//counts how many seats are available
        countTakenSeats(plane);//counts how many seats are taken
    }

    public static void displaySeats(int seats[][]) {//displays the seats
        System.out.print("\tA  \tB  \tC  \tD \n");
        for (int row = 0; row < seats.length; row++) {
            System.out.print(row + 1 + "");
            for (int seatNum = 0; seatNum < seats[row].length; seatNum++) {
                if (seats[row][seatNum] == 1) {
                    System.out.print("\tX  ");
                } else {
                    System.out.print("\t0  ");
                }
            }
            System.out.println();
        }
    }

    public static int[][] fillSeatsRandomly() {//fills seats randomly, returns
        //an array of seats either filled or empty 
        int[][] rndm = new int[12][4];
        for (int[] rndm1 : rndm) {
            for (int col = 0; col < rndm1.length; col++) {
                rndm1[col] = (int) Math.round(Math.random());
            }
        }
        return rndm;//return the array
    }//end of fillSeatsRandomly

    public static boolean isSeatAvailable(int[][] plane) {//asks user for input
        //checks if seat is available, returns true if it is and false if not
        Scanner input = new Scanner(System.in);
        int rowNum;//row number of the seat chosen
        int colNum = 4;//column of the seat chosen
        char seatSelect;//character of the seat chosen
        String str;//used to get char value for switch
        System.out.println("Choose which row you would like to check (1-12)");
        rowNum = input.nextInt() - 1;
        System.out.println("Choose which seat you would like: A,B,C, or D (Case"
                + " Sensitive)");
        str = input.next();
        seatSelect = str.charAt(0);
        switch (seatSelect) {
            case 'A':
                colNum = 0;
                break;
            case 'B':
                colNum = 1;
                break;
            case 'C':
                colNum = 2;
                break;
            case 'D':
                colNum = 3;
        }//end of switch
        if (colNum > 3 || colNum < 0 || rowNum > 11 || rowNum < 0) {
            System.out.println("Invalid Selection");
            return false;
        }//end of if
        if (plane[rowNum][colNum] != 0) {
            System.out.println("Sorry, seat is taken");
            return false;
        } else {
            System.out.println("This seat is available");
            return true;
        }//end of else
    }//end of isSeatAvail

    public static int reserveSeat(int[][] plane) {//asks user for input
        //reserves a seat based on user input, returns updated value to array
        Scanner input = new Scanner(System.in);
        int rowNum;//Row number of seat chosen
        int colNum = 4;//Column number of seat chosen
        char seatSelect;//Character of seat chosen
        String str;//used to get char value for switch
        System.out.println("Choose which row you would like to reserve (1-12)");
        rowNum = input.nextInt() - 1;
        System.out.println("Choose which seat you would like: A,B,C, or D (Case"
                + " Sensitive)");
        str = input.next();
        seatSelect = str.charAt(0);
        switch (seatSelect) {
            case 'A':
                colNum = 0;
                break;
            case 'B':
                colNum = 1;
                break;
            case 'C':
                colNum = 2;
                break;
            case 'D':
                colNum = 3;
        }//end of switch
        if (colNum > 3 || colNum < 0 || rowNum > 11 || rowNum < 0) {
            System.out.println("Invalid Selection");

        }//end of if
        if (plane[rowNum][colNum] != 0) {
            System.out.println("Sorry, seat is taken");

        } else {
            System.out.println("Your seat has been reserved");

        }//end of if else
        return plane[rowNum][colNum] = 1;//return updated array value
    } //end of reserveSeat

    public static int seatsAvailInRow(int[][] plane) {//checks how many seats
        //are available in a row determined by user input returns how many seats
        Scanner input = new Scanner(System.in);
        int rowNum;
        int rowSum = 0;
        int avail;
        System.out.println("Choose which row to see how many seats are"
                + " available (1-12)");
        rowNum = input.nextInt() - 1;
        for (int[] plane1 : plane) {
            rowSum = 0;
            for (int j = 0; j < plane1.length; j++) {
                rowSum += plane[rowNum][j];
            } //end of nested for
        } //end of for
        avail = 4 - rowSum;
        System.out.println("There is " + avail + " seat(s) available in that "
                + "row");
        return avail;//returns number of seats available
    }//end of seatsAvailInRow

    public static int findRowWithTwoSeats(int[][] plane) { //finds the closest
        //row that has 2 adjacent seats, returns row num, or 0 if none
        for (int i = 0; i < plane.length; i++) {
            for (int j = 0; j < plane[i].length; j++) {
            if (plane[i][0] + plane[i][1] == 0 || plane[i][1] + plane[i][2] == 0
                        || plane[i][2] + plane[i][3] == 0) {
                    System.out.println("Row " + (i + 1)
                            + " has adjacent seats");
                    return i;//returns row num with adjacent seats
                }//end of if
            } //end of nested for
        } //end of for
        return 0;//returns 0 if there is no adjacent seats
    }//end of findRowWithTwoSeats

    public static int countSeatsAvail(int[][] plane) {//counts how many seats
        //are available on the plane, returns int value for number of seats
        int count = 0;//used to keep track of how many seats are available
        for (int i = 0; i < plane.length; i++) {
            for (int j = 0; j < plane[i].length; j++) {
                if (plane[i][j] == 0) {
                    count++;
                }//end of if
            }//end of nested for
        }//end of for
        System.out.println("There are " + count + " seats available");
        return count;//returns the number of seats available
    }//end of countSeatsAvail

    public static int countTakenSeats(int[][] plane) {//counts number of seats
        //taken on the plane, returns int value for number of seats taken
        int count = 0;//used to keep track of how many seats are taken
        for (int i = 0; i < plane.length; i++) {
            for (int j = 0; j < plane[i].length; j++) {
                if (plane[i][j] == 1) {
                    count++;
                }
            }//end of nested for
        }//end of for
        System.out.println("There are " + count + " seats taken");
        return count;//returns number of seats taken
    }//end of countTakenSeats
}//end of class
 public static void main(String[] args) {
    int[][] plane;//this is the array that will be plugged into the methods
    plane = fillSeatsRandomly();//creates the 12x4 array that will be used
    displaySeats(plane);//shows the user what seats are available/taken
    isSeatAvailable(plane);//allows the user to see if a seat is available
    reserveSeat(plane);//allows the user to reserve a seat
    displaySeats(plane);//shows the user their seat has been reserved
    seatsAvailInRow(plane);//shows how many seats are avail in given row
    findRowWithTwoSeats(plane);//finds closest row with adjacent seats
    countSeatsAvail(plane);//counts how many seats are available
    countTakenSeats(plane);//counts how many seats are taken
}

public static void displaySeats(int seats[][]) {//displays the seats
    System.out.print("\tA  \tB  \tC  \tD \n");
    for (int row = 0; row < seats.length; row++) {
        System.out.print(row + 1 + "");
        for (int seatNum = 0; seatNum < seats[row].length; seatNum++) {
            if (seats[row][seatNum] == 1) {
                System.out.print("\tX  ");
            } else {
                System.out.print("\t0  ");
            }
        }
        System.out.println();
    }
}

public static int[][] fillSeatsRandomly() {//fills seats randomly, returns
    //an array of seats either filled or empty 
    int[][] rndm = new int[12][4];
    for (int[] rndm1 : rndm) {
        for (int col = 0; col < rndm1.length; col++) {
            rndm1[col] = (int) Math.round(Math.random());
        }
    }
    return rndm;//return the array
}//end of fillSeatsRandomly

public static boolean isSeatAvailable(int[][] plane) {//asks user for input
    //checks if seat is available, returns true if it is and false if not
    Scanner input = new Scanner(System.in);
    int rowNum;//row number of the seat chosen
    int colNum = 4;//column of the seat chosen
    char seatSelect;//character of the seat chosen
    String str;//used to get char value for switch
    System.out.println("Choose which row you would like to check (1-12)");
    rowNum = input.nextInt() - 1;
    System.out.println("Choose which seat you would like: A,B,C, or D (Case"
            + " Sensitive)");
    str = input.next();
    seatSelect = str.charAt(0);
    switch (seatSelect) {
        case 'A':
            colNum = 0;
            break;
        case 'B':
            colNum = 1;
            break;
        case 'C':
            colNum = 2;
            break;
        case 'D':
            colNum = 3;
    }//end of switch
    if (colNum > 3 || colNum < 0 || rowNum > 11 || rowNum < 0) {
        System.out.println("Invalid Selection");
        return false;
    }//end of if
    if (plane[rowNum][colNum] != 0) {
        System.out.println("Sorry, seat is taken");
        return false;
    } else {
        System.out.println("This seat is available");
        return true;
    }//end of else
}//end of isSeatAvail

public static int reserveSeat(int[][] plane) {//asks user for input
    //reserves a seat based on user input, returns updated value to array
    Scanner input = new Scanner(System.in);
    int rowNum;//Row number of seat chosen
    int colNum = 4;//Column number of seat chosen
    char seatSelect;//Character of seat chosen
    String str;//used to get char value for switch
    System.out.println("Choose which row you would like to reserve (1-12)");
    rowNum = input.nextInt() - 1;
    System.out.println("Choose which seat you would like: A,B,C, or D (Case"
            + " Sensitive)");
    str = input.next();
    seatSelect = str.charAt(0);
    switch (seatSelect) {
        case 'A':
            colNum = 0;
            break;
        case 'B':
            colNum = 1;
            break;
        case 'C':
            colNum = 2;
            break;
        case 'D':
            colNum = 3;
    }//end of switch
    if (colNum > 3 || colNum < 0 || rowNum > 11 || rowNum < 0) {
        System.out.println("Invalid Selection");

    }//end of if
    if (plane[rowNum][colNum] != 0) {
        System.out.println("Sorry, seat is taken");

    } else {
        System.out.println("Your seat has been reserved");

    }//end of if else
    return plane[rowNum][colNum] = 1;//return updated array value
} //end of reserveSeat

public static int seatsAvailInRow(int[][] plane) {//checks how many seats
    //are available in a row determined by user input returns how many seats
    Scanner input = new Scanner(System.in);
    int rowNum;
    int rowSum = 0;
    int avail;
    System.out.println("Choose which row to see how many seats are"
            + " available (1-12)");
    rowNum = input.nextInt() - 1;
    for (int[] plane1 : plane) {
        rowSum = 0;
        for (int j = 0; j < plane1.length; j++) {
            rowSum += plane[rowNum][j];
        } //end of nested for
    } //end of for
    avail = 4 - rowSum;
    System.out.println("There is " + avail + " seat(s) available in that "
            + "row");
    return avail;//returns number of seats available
}//end of seatsAvailInRow

public static int findRowWithTwoSeats(int[][] plane) { //finds the closest
    //row that has 2 adjacent seats, returns row num, or 0 if none
    for (int i = 0; i < plane.length; i++) {
        for (int j = 0; j < plane[i].length; j++) {
        if (plane[i][0] + plane[i][1] == 0 || plane[i][1] + plane[i][2] == 0
                    || plane[i][2] + plane[i][3] == 0) {
                System.out.println("Row " + (i + 1)
                        + " has adjacent seats");
                return i;//returns row num with adjacent seats
            }//end of if
        } //end of nested for
    } //end of for
    return 0;//returns 0 if there is no adjacent seats
}//end of findRowWithTwoSeats

public static int countSeatsAvail(int[][] plane) {//counts how many seats
    //are available on the plane, returns int value for number of seats
    int count = 0;//used to keep track of how many seats are available
    for (int i = 0; i < plane.length; i++) {
        for (int j = 0; j < plane[i].length; j++) {
            if (plane[i][j] == 0) {
                count++;
            }//end of if
        }//end of nested for
    }//end of for
    System.out.println("There are " + count + " seats available");
    return count;//returns the number of seats available
}//end of countSeatsAvail

public static int countTakenSeats(int[][] plane) {//counts number of seats
    //taken on the plane, returns int value for number of seats taken
    int count = 0;//used to keep track of how many seats are taken
    for (int i = 0; i < plane.length; i++) {
        for (int j = 0; j < plane[i].length; j++) {
            if (plane[i][j] == 1) {
                count++;
            }
        }//end of nested for
    }//end of for
    System.out.println("There are " + count + " seats taken");
    return count;//returns number of seats taken
}//end of countTakenSeats

}//end of class

     public static void main(String[] args) {
        int[][] plane;//this is the array that will be plugged into the methods
        plane = fillSeatsRandomly();//creates the 12x4 array that will be used
        displaySeats(plane);//shows the user what seats are available/taken
        isSeatAvailable(plane);//allows the user to see if a seat is available
        reserveSeat(plane);//allows the user to reserve a seat
        displaySeats(plane);//shows the user their seat has been reserved
        seatsAvailInRow(plane);//shows how many seats are avail in given row
        findRowWithTwoSeats(plane);//finds closest row with adjacent seats
        countSeatsAvail(plane);//counts how many seats are available
        countTakenSeats(plane);//counts how many seats are taken
    }

    public static void displaySeats(int seats[][]) {//displays the seats
        System.out.print("\tA  \tB  \tC  \tD \n");
        for (int row = 0; row < seats.length; row++) {
            System.out.print(row + 1 + "");
            for (int seatNum = 0; seatNum < seats[row].length; seatNum++) {
                if (seats[row][seatNum] == 1) {
                    System.out.print("\tX  ");
                } else {
                    System.out.print("\t0  ");
                }
            }
            System.out.println();
        }
    }

    public static int[][] fillSeatsRandomly() {//fills seats randomly, returns
        //an array of seats either filled or empty 
        int[][] rndm = new int[12][4];
        for (int[] rndm1 : rndm) {
            for (int col = 0; col < rndm1.length; col++) {
                rndm1[col] = (int) Math.round(Math.random());
            }
        }
        return rndm;//return the array
    }//end of fillSeatsRandomly

    public static boolean isSeatAvailable(int[][] plane) {//asks user for input
        //checks if seat is available, returns true if it is and false if not
        Scanner input = new Scanner(System.in);
        int rowNum;//row number of the seat chosen
        int colNum = 4;//column of the seat chosen
        char seatSelect;//character of the seat chosen
        String str;//used to get char value for switch
        System.out.println("Choose which row you would like to check (1-12)");
        rowNum = input.nextInt() - 1;
        System.out.println("Choose which seat you would like: A,B,C, or D (Case"
                + " Sensitive)");
        str = input.next();
        seatSelect = str.charAt(0);
        switch (seatSelect) {
            case 'A':
                colNum = 0;
                break;
            case 'B':
                colNum = 1;
                break;
            case 'C':
                colNum = 2;
                break;
            case 'D':
                colNum = 3;
        }//end of switch
        if (colNum > 3 || colNum < 0 || rowNum > 11 || rowNum < 0) {
            System.out.println("Invalid Selection");
            return false;
        }//end of if
        if (plane[rowNum][colNum] != 0) {
            System.out.println("Sorry, seat is taken");
            return false;
        } else {
            System.out.println("This seat is available");
            return true;
        }//end of else
    }//end of isSeatAvail

    public static int reserveSeat(int[][] plane) {//asks user for input
        //reserves a seat based on user input, returns updated value to array
        Scanner input = new Scanner(System.in);
        int rowNum;//Row number of seat chosen
        int colNum = 4;//Column number of seat chosen
        char seatSelect;//Character of seat chosen
        String str;//used to get char value for switch
        System.out.println("Choose which row you would like to reserve (1-12)");
        rowNum = input.nextInt() - 1;
        System.out.println("Choose which seat you would like: A,B,C, or D (Case"
                + " Sensitive)");
        str = input.next();
        seatSelect = str.charAt(0);
        switch (seatSelect) {
            case 'A':
                colNum = 0;
                break;
            case 'B':
                colNum = 1;
                break;
            case 'C':
                colNum = 2;
                break;
            case 'D':
                colNum = 3;
        }//end of switch
        if (colNum > 3 || colNum < 0 || rowNum > 11 || rowNum < 0) {
            System.out.println("Invalid Selection");

        }//end of if
        if (plane[rowNum][colNum] != 0) {
            System.out.println("Sorry, seat is taken");

        } else {
            System.out.println("Your seat has been reserved");

        }//end of if else
        return plane[rowNum][colNum] = 1;//return updated array value
    } //end of reserveSeat

    public static int seatsAvailInRow(int[][] plane) {//checks how many seats
        //are available in a row determined by user input returns how many seats
        Scanner input = new Scanner(System.in);
        int rowNum;
        int rowSum = 0;
        int avail;
        System.out.println("Choose which row to see how many seats are"
                + " available (1-12)");
        rowNum = input.nextInt() - 1;
        for (int[] plane1 : plane) {
            rowSum = 0;
            for (int j = 0; j < plane1.length; j++) {
                rowSum += plane[rowNum][j];
            } //end of nested for
        } //end of for
        avail = 4 - rowSum;
        System.out.println("There is " + avail + " seat(s) available in that "
                + "row");
        return avail;//returns number of seats available
    }//end of seatsAvailInRow

    public static int findRowWithTwoSeats(int[][] plane) { //finds the closest
        //row that has 2 adjacent seats, returns row num, or 0 if none
        for (int i = 0; i < plane.length; i++) {
            for (int j = 0; j < plane[i].length; j++) {
            if (plane[i][0] + plane[i][1] == 0 || plane[i][1] + plane[i][2] == 0
                        || plane[i][2] + plane[i][3] == 0) {
                    System.out.println("Row " + (i + 1)
                            + " has adjacent seats");
                    return i;//returns row num with adjacent seats
                }//end of if
            } //end of nested for
        } //end of for
        return 0;//returns 0 if there is no adjacent seats
    }//end of findRowWithTwoSeats

    public static int countSeatsAvail(int[][] plane) {//counts how many seats
        //are available on the plane, returns int value for number of seats
        int count = 0;//used to keep track of how many seats are available
        for (int i = 0; i < plane.length; i++) {
            for (int j = 0; j < plane[i].length; j++) {
                if (plane[i][j] == 0) {
                    count++;
                }//end of if
            }//end of nested for
        }//end of for
        System.out.println("There are " + count + " seats available");
        return count;//returns the number of seats available
    }//end of countSeatsAvail

    public static int countTakenSeats(int[][] plane) {//counts number of seats
        //taken on the plane, returns int value for number of seats taken
        int count = 0;//used to keep track of how many seats are taken
        for (int i = 0; i < plane.length; i++) {
            for (int j = 0; j < plane[i].length; j++) {
                if (plane[i][j] == 1) {
                    count++;
                }
            }//end of nested for
        }//end of for
        System.out.println("There are " + count + " seats taken");
        return count;//returns number of seats taken
    }//end of countTakenSeats
}//end of class
Source Link

Applying 2d Array To Methods Plus Return Statements (Java)

I have an assignment that I have completed, but I feel as though the way I went about solving the problems could be optimized a bit. As well, I don't know if I'm properly utilizing return statements. I'm relatively new to coding so I don't have the knowledge, but I would love to learn if anyone wants to go through and see if they can help me, even just pointing me in the right direction without actually giving me a definitive answer would be fantastic.

Here is the problem: Write a program that displays, examines and manipulates a two dimensional array representing plane seats for a plane with 12 rows and 4 seats per row. Write methods for the following operations:

fillSeatsRandomly:Fills the array with random values in range from 0 to 1. Zero represents an empty seat and one represents a reserved seat.

displaySeats: Displays all the seats in the plane row by row. Empty seat is displayed as 0 and a reserved seat is displayed as X. Create vertical headings for the row numbers from 1 to 12 on the left hand side and horizontal column headings from A to D (for the 4 seats in the row).

isSeatAvailable: Receives an integer representing a row number and a character representing a seat from A to D. This method tests if the specified seat is available, otherwise it returns false. This method displays an error message when the seat selection is invalid.

reserveSeat: Reserves a specified seat. Receives two parameters: an integer representing a row number and a character representing a seat number from A to D. This method displays an error message when the seat is invalid.

seatsAvailInRow: counts and returns number of seats available in given row. Receives an integer representing row number.

findRowWithTwoSeats: This method look for the closest row with two adjacent available seats. If two adjacent seats are not available it returns 0.

countSeatsAvail: counts the number of all seats available in the plane and returns that number.

countTakenSeats: counts the number of all seats reserved in the plane and returns that number.

Here is my code

 public static void main(String[] args) {
    int[][] plane;//this is the array that will be plugged into the methods
    plane = fillSeatsRandomly();//creates the 12x4 array that will be used
    displaySeats(plane);//shows the user what seats are available/taken
    isSeatAvailable(plane);//allows the user to see if a seat is available
    reserveSeat(plane);//allows the user to reserve a seat
    displaySeats(plane);//shows the user their seat has been reserved
    seatsAvailInRow(plane);//shows how many seats are avail in given row
    findRowWithTwoSeats(plane);//finds closest row with adjacent seats
    countSeatsAvail(plane);//counts how many seats are available
    countTakenSeats(plane);//counts how many seats are taken
}

public static void displaySeats(int seats[][]) {//displays the seats
    System.out.print("\tA  \tB  \tC  \tD \n");
    for (int row = 0; row < seats.length; row++) {
        System.out.print(row + 1 + "");
        for (int seatNum = 0; seatNum < seats[row].length; seatNum++) {
            if (seats[row][seatNum] == 1) {
                System.out.print("\tX  ");
            } else {
                System.out.print("\t0  ");
            }
        }
        System.out.println();
    }
}

public static int[][] fillSeatsRandomly() {//fills seats randomly, returns
    //an array of seats either filled or empty 
    int[][] rndm = new int[12][4];
    for (int[] rndm1 : rndm) {
        for (int col = 0; col < rndm1.length; col++) {
            rndm1[col] = (int) Math.round(Math.random());
        }
    }
    return rndm;//return the array
}//end of fillSeatsRandomly

public static boolean isSeatAvailable(int[][] plane) {//asks user for input
    //checks if seat is available, returns true if it is and false if not
    Scanner input = new Scanner(System.in);
    int rowNum;//row number of the seat chosen
    int colNum = 4;//column of the seat chosen
    char seatSelect;//character of the seat chosen
    String str;//used to get char value for switch
    System.out.println("Choose which row you would like to check (1-12)");
    rowNum = input.nextInt() - 1;
    System.out.println("Choose which seat you would like: A,B,C, or D (Case"
            + " Sensitive)");
    str = input.next();
    seatSelect = str.charAt(0);
    switch (seatSelect) {
        case 'A':
            colNum = 0;
            break;
        case 'B':
            colNum = 1;
            break;
        case 'C':
            colNum = 2;
            break;
        case 'D':
            colNum = 3;
    }//end of switch
    if (colNum > 3 || colNum < 0 || rowNum > 11 || rowNum < 0) {
        System.out.println("Invalid Selection");
        return false;
    }//end of if
    if (plane[rowNum][colNum] != 0) {
        System.out.println("Sorry, seat is taken");
        return false;
    } else {
        System.out.println("This seat is available");
        return true;
    }//end of else
}//end of isSeatAvail

public static int reserveSeat(int[][] plane) {//asks user for input
    //reserves a seat based on user input, returns updated value to array
    Scanner input = new Scanner(System.in);
    int rowNum;//Row number of seat chosen
    int colNum = 4;//Column number of seat chosen
    char seatSelect;//Character of seat chosen
    String str;//used to get char value for switch
    System.out.println("Choose which row you would like to reserve (1-12)");
    rowNum = input.nextInt() - 1;
    System.out.println("Choose which seat you would like: A,B,C, or D (Case"
            + " Sensitive)");
    str = input.next();
    seatSelect = str.charAt(0);
    switch (seatSelect) {
        case 'A':
            colNum = 0;
            break;
        case 'B':
            colNum = 1;
            break;
        case 'C':
            colNum = 2;
            break;
        case 'D':
            colNum = 3;
    }//end of switch
    if (colNum > 3 || colNum < 0 || rowNum > 11 || rowNum < 0) {
        System.out.println("Invalid Selection");

    }//end of if
    if (plane[rowNum][colNum] != 0) {
        System.out.println("Sorry, seat is taken");

    } else {
        System.out.println("Your seat has been reserved");

    }//end of if else
    return plane[rowNum][colNum] = 1;//return updated array value
} //end of reserveSeat

public static int seatsAvailInRow(int[][] plane) {//checks how many seats
    //are available in a row determined by user input returns how many seats
    Scanner input = new Scanner(System.in);
    int rowNum;
    int rowSum = 0;
    int avail;
    System.out.println("Choose which row to see how many seats are"
            + " available (1-12)");
    rowNum = input.nextInt() - 1;
    for (int[] plane1 : plane) {
        rowSum = 0;
        for (int j = 0; j < plane1.length; j++) {
            rowSum += plane[rowNum][j];
        } //end of nested for
    } //end of for
    avail = 4 - rowSum;
    System.out.println("There is " + avail + " seat(s) available in that "
            + "row");
    return avail;//returns number of seats available
}//end of seatsAvailInRow

public static int findRowWithTwoSeats(int[][] plane) { //finds the closest
    //row that has 2 adjacent seats, returns row num, or 0 if none
    for (int i = 0; i < plane.length; i++) {
        for (int j = 0; j < plane[i].length; j++) {
        if (plane[i][0] + plane[i][1] == 0 || plane[i][1] + plane[i][2] == 0
                    || plane[i][2] + plane[i][3] == 0) {
                System.out.println("Row " + (i + 1)
                        + " has adjacent seats");
                return i;//returns row num with adjacent seats
            }//end of if
        } //end of nested for
    } //end of for
    return 0;//returns 0 if there is no adjacent seats
}//end of findRowWithTwoSeats

public static int countSeatsAvail(int[][] plane) {//counts how many seats
    //are available on the plane, returns int value for number of seats
    int count = 0;//used to keep track of how many seats are available
    for (int i = 0; i < plane.length; i++) {
        for (int j = 0; j < plane[i].length; j++) {
            if (plane[i][j] == 0) {
                count++;
            }//end of if
        }//end of nested for
    }//end of for
    System.out.println("There are " + count + " seats available");
    return count;//returns the number of seats available
}//end of countSeatsAvail

public static int countTakenSeats(int[][] plane) {//counts number of seats
    //taken on the plane, returns int value for number of seats taken
    int count = 0;//used to keep track of how many seats are taken
    for (int i = 0; i < plane.length; i++) {
        for (int j = 0; j < plane[i].length; j++) {
            if (plane[i][j] == 1) {
                count++;
            }
        }//end of nested for
    }//end of for
    System.out.println("There are " + count + " seats taken");
    return count;//returns number of seats taken
}//end of countTakenSeats

}//end of class