Today I have for you an interesting example. It deals with 2D arrays and the use of classes and their methods.

You can refresh your memory about regular arrays (also known as 1D array) in this article.

The difference between a 2D array and a regular array is that in a 2D array there are 2 dimensions height (columns) and width (rows). Imagine that you have a series of 1D arrays, one stacked on top of the other. On each row you have an array made up of however many elements. Each element exists in a column.

public class PerformanceSeating { private char[][] seats; private int i; //here we initialize our instance variables //remember they only exist within our object public PerformanceSeating(int x, int y)//this is a constructor { seats = new char[x][y];//creates a 2d array as specified //this is a nested for loop //it will iterate through every column, and every row of our 2D array //sets every element to be the character A for (int r = 0; r < this.seats.length; r++)//itterates through the rows { for (int c = 0; c < this.seats[0].length; c++)//itterates through the cols { seats[r][c] = 'A'; } } } //finds the number of booked seats in the theather public int findBookedSeats() { i = 0; for (int r = 0; r < this.seats.length; r++)//itterates through the rows { for (int c = 0; c < this.seats[0].length; c++)//itterates through the cols { if (seats[r][c] == 'X')//Each booked seat we find increments our count { i++; } } } return i; } //finds the number of available seats in the theather public int findAvailableSeats() { i = 0; for (int r = 0; r < this.seats.length; r++)//itterates through the rows { for (int c = 0; c < this.seats[0].length; c++)//itterates through the cols { if (seats[r][c] == 'A')//Each available seat we find increments our count { i++; } } } return i; } //Books x amount of seats public void bookSeats(int x) { i = 0; for (int r = 0; r < this.seats.length; r++)//itterates through the rows { if (i >= x)//breaks out of the loop after we've booked all our seats break; for (int c = 0; c < this.seats[0].length; c++)//itterates through the cols { //checks if the seat is available 1st, and occupies it if (seats[r][c] == 'A') { seats[r][c] = 'X'; i++; } } } } //simply sets a certain seat to A, based on what the user enters public void cancelSeat(int x, int y) { seats[x][y] = 'A'; } //A simple for loop that prints our 2D array to the console public void printSeats() { for (int r = 0; r < this.seats.length; r++)//itterates through the rows //Rem, declare your variables { for (int c = 0; c < this.seats[0].length; c++)//itterates through the cols { System.out.print(seats[r][c] + " ");//Prints out each value at each index } System.out.println(); } } }

The above program represents a seating chart, a stands for Available, X stands for a taken seat. The class “PerformanceSeating” is able to fill seats, make them available and print a representation to console. It is also able to tell the user how many booked/available seats there are.

//Here we create a main method for testing our class //be sure to place your class and tester in the same folder/package //otherwise you might need to undergo extra steps public class PerformanceSeatingTest { public static void main(String[] args) { PerformanceSeating ps = new PerformanceSeating(5, 10); ps.printSeats(); System.out.println(); ps.bookSeats(10); ps.cancelSeat(0, 4); ps.printSeats(); System.out.println(); ps.bookSeats(21); ps.cancelSeat(0, 3); ps.cancelSeat(1, 8); ps.cancelSeat(2, 1); ps.printSeats(); System.out.println(); System.out.println("Available seats: " + ps.findAvailableSeats()); System.out.println("Booked seats: " + ps.findBookedSeats()); } }