Question

In Java The world-renowned sloth cellist Lento Con Brio is planning to make a much-anticipated return...

In Java

The world-renowned sloth cellist Lento Con Brio is planning to make a much-anticipated return to the
concert stage after this whole coronavirus situation is over! The sloth strongly dislikes Ticketmaster
and its exorbitant fees, so you've been hired to write some custom ticketing software for the upcoming
concert.
The concert hall is a rectangular grid of seats. You want to divide the seats into three sections (premium,
standard, and economy) based on distance from the stage. Each section contains a certain number of
rows, and the tickets cost more as you get closer to the stage. The classi cations into premium, standard,
and economy rows are
exible { they can be changed to a ect how much revenue is generated from the
concert.
To keep track which tickets have been sold, you will use a 2D array of boolean values, where each
1
element represents one seat in the concert hall. A value of true indicates that the seat has been sold,
and a value of false indicates that the seat is still available.
Within your Lab7HW folder, create a new class named SlothConcert.
1. (6 pts) Within the SlothConcert class, write a method named showSeats that takes three param-
eters:
A 2D array of boolean values indicating which seats have been sold
How many row(s) should be classi ed as premium seats. These are the rst row(s) in the
array.
How many row(s) should be classi ed as standard seats. These are the next row(s) in the
array.
You may assume that the array is rectangular and non-empty, the number of premium and standard
rows are non-negative, and the combined number of premium and standard rows do not exceed the
number of rows in the array. (Note that although the number of economy rows is not speci ed, it
can be computed simply by subtracting the number of premium and standard rows from the total
number of rows.)
The showSeats method should show a text representation of the seats in the concert hall, one row
of seats per line. Use * to indicate a sold seat, P to indicate an unsold premium seat, S to indicate
an unsold standard seat, and E to indicate an unsold economy seat.
For example, suppose seats is the 2D boolean array f ffalse, false, trueg, ftrue, false,
trueg, ftrue, false, trueg, ftrue, true, falseg g. Then calling showSeats(seats, 1,
2) should print the following:
PP*
*S*
*S*
**E
2. (7 pts) Out of an abundance of caution, the concert hall has decided to institute a social distancing
policy at the event. Speci cally, every sold seat cannot have another sold seat immediately to the
left, right, ahead, or behind. Having another sold seat diagonally adjacent is OK, however. This
has the added bene t of not blocking anyone's line of sight to the stage { a stroke of genius, truly.
(Except for the fact that this drastically cuts into ticket revenue, but let's not worry about that
here.)
For example, this con guration of sold seats is OK (where * indicates a sold seat and - indicates
an empty seat):
--*-*
*--*-
-*--*
*-*--
However, this one is not OK due to the upper left corner:
*-*-*
*--*-
-*--*
*-*--
Within the SlothConcert class, write a method named isProperlyDistanced that takes a 2D
boolean array as a parameter. You may assume that the array is rectangular and non-empty. This
method should return whether or not the social distancing guidelines described above are met.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.*;
public class SlothConcert {
    public static void showSeats(boolean seats[][], int premium, int standard) {
        int total = seats.length;
        int economy = total - (premium + standard);
        for (int i = 0; i < premium; ++i) {
            for (int j = 0; j < seats[i].length; ++j) {
                if (seats[i][j] == true)
                    System.out.print("*");
                else
                    System.out.print("P");
            }
            System.out.println();
        }
        for (int i = premium; i < premium + standard; ++i) {
            for (int j = 0; j < seats[i].length; ++j) {
                if (seats[i][j] == true)
                    System.out.print("*");
                else
                    System.out.print("S");
            }
            System.out.println();
        }
        for (int i = premium + standard; i < total; ++i) {
            for (int j = 0; j < seats[i].length; ++j) {
                if (seats[i][j] == true)
                    System.out.print("*");
                else
                    System.out.print("E");
            }
            System.out.println();
        }
    }
    public static boolean isPossible(boolean seats[][], int x, int y) {
        int rows = seats.length;
        int cols = seats[0].length;
        boolean left = false, right = false, up = false, down = false;
        if (x - 1 >= 0 && x - 1 < rows) {
            up = seats[x - 1][y];
        }
        if (x + 1 >= 0 && x + 1 < rows) {
            down = seats[x + 1][y];
        }
        if (y - 1 >= 0 && y - 1 < cols) {
            left = seats[x][y - 1];
        }
        if (y + 1 >= 0 && y + 1 < cols) {
            right = seats[x][y + 1];
        }
        return (left || right || up || down);
    }
    public static boolean isProperlyDistanced(boolean seats[][]) {
        int rows = seats.length;
        int cols = seats[0].length;
        for (int i = 0; i < rows; ++i) {
            for (int j = 0; j < cols; ++j) {
                if (seats[i][j] == true) {
                    if (isPossible(seats, i, j))
                        return false;
                }
            }
        }
        return true;
    }
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int rows, cols;
        System.out.println("Enter number of rows and columns");
        rows = in.nextInt();
        cols = in.nextInt();
        boolean seats[][] = new boolean[rows][cols]; // 2D array to show booked and available seats
        System.out.println("Enter booked and available seats status");
        for (int i = 0; i < rows; ++i) {
            for (int j = 0; j < cols; ++j) {
                seats[i][j] = in.nextBoolean();
            }
        }
        int premium, standard;
        System.out.println("Enter number of premium and standard seats");
        premium = in.nextInt();
        standard = in.nextInt();
       
        showSeats(seats, premium, standard);
        System.out.println(isProperlyDistanced(seats));
    }

}

Add a comment
Know the answer?
Add Answer to:
In Java The world-renowned sloth cellist Lento Con Brio is planning to make a much-anticipated return...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT