85.3k views
0 votes
Write the constructor for the Theater class. The constructor takes three int parameters, representing the number of seats per row, the number of tier 1 rows, and the number of tier 2 rows, respectively. The constructor initializes the theaterSeats instance variable so that it has the given number of seats per row and the given number of tier 1 and tier 2 rows and all seats are available and have the appropriate tier designation. Row 0 of the theaterSeats array represents the row closest to the stage. All tier 1 seats are closer to the stage than tier 2 seats. Complete the Theater constructor. /** Constructs a Theater object, as described in part (a). * Precondition: seatsPerRow > 0; tier1Rows > 0; tier 2 Rows >= 0 */ public Theater(int seatsPerRow, int tier1Rows, int tier 2 Rows)

User Kolten
by
4.3k points

1 Answer

3 votes

Answer:

See explaination

Step-by-step explanation:

class Seat {

private boolean available;

private int tier;

public Seat(boolean isAvail, int tierNum)

{ available = isAvail;

tier = tierNum; }

public boolean isAvailable() { return available; }

public int getTier() { return tier; }

public void setAvailability(boolean isAvail) { available = isAvail; } }

//The Theater class represents a theater of seats. The number of seats per row and the number of tier 1 and tier 2 rows are

//determined by the parameters of the Theater constructor.

//Row 0 of the theaterSeats array represents the row closest to the stage.

public class Theater {

private Seat[][] theaterSeats; /** Constructs a Theater object, as described in part (a). * Precondition: seatsPerRow > 0; tier1Rows > 0; tier2Rows >= 0 */

public Theater(int seatsPerRow, int tier1Rows, int tier2Rows) {

theaterSeats= new Seat[tier1Rows+tier2Rows][seatsPerRow];

}

public boolean reassignSeat(int fromRow, int fromCol, int toRow, int toCol) {

if(theaterSeats[toRow][toCol].isAvailable()) {

int tierDestination =theaterSeats[toRow][toCol].getTier();

int tierSource =theaterSeats[fromRow][fromCol].getTier();

if(tierDestination<=tierSource) {

if(tierDestination==tierSource) {

if(fromRow<toRow) {

return false;

}else {

theaterSeats[toRow][toCol].setAvailability(false);

theaterSeats[fromRow][fromCol].setAvailability(true);

return true;

}

}

theaterSeats[toRow][toCol].setAvailability(false);

theaterSeats[fromRow][fromCol].setAvailability(true);

return true;

}else {

return false;

}

}else {

return false;

}

}

public static void main(String[] args) {

//Lets understand it with simple example

Theater t1 = new Theater(3,1,2);

//Our threater has 3 seat in each row and we have one tier 1 row and 1 tier 2 row

//total no of seat will be 9.

//Lets create our seat

t1.theaterSeats[0][0] = new Seat(true,1);

t1.theaterSeats[0][1] = new Seat(false,1);

t1.theaterSeats[0][2] = new Seat(true,1);

t1.theaterSeats[1][0] = new Seat(true,2);

t1.theaterSeats[1][1] = new Seat(true,2);

t1.theaterSeats[1][2] = new Seat(true,2);

t1.theaterSeats[2][0] = new Seat(false,2);

t1.theaterSeats[2][1] = new Seat(false,2);

t1.theaterSeats[2][2] = new Seat(true,2);

//Lets print out theater and see which seat is available or which is not

System.out.println("Theater===>");

for(int i=0;i<3;i++) {

for(int j=0;j<3;j++) {

System.out.print("["+i+"]"+"["+j+"] : "+t1.theaterSeats[i][j].isAvailable()+" ");

}

System.out.println();

}

System.out.println("(2,1) want to change seat to (0,0)");

System.out.println("["+2+"]"+"["+1+"]"+"===>"+"["+0+"]"+"["+0+"]");

t1.reassignSeat(2, 1, 0, 0);

for(int i=0;i<3;i++) {

for(int j=0;j<3;j++) {

System.out.print("["+i+"]"+"["+j+"] : "+t1.theaterSeats[i][j].isAvailable()+" ");

}

System.out.println();

}

}

}

User Syam Kallada
by
5.2k points