Package Name: hmwk07 Source File Name: (Submit zip of these) SeatChart.java Seat
ID: 3588296 • Letter: P
Question
Package Name: hmwk07
Source File Name:
(Submit zip of these) SeatChart.java
SeatChartTest.java
This program will let keep a seating chart. There are 4 rows and 6 seats per row.
Your program will:
• Read a person’s name. Then read the row (1 to 4) and column/seat in the row (1 to 6).
o You should use a two-dimensional array to keep a map of the seating chart. Make the elements be the names assigned to each seat.
• When the user enters “exit” for the name, stop reading,
• Then print the contents of the two-dimensional map of the seating chart.
• Also print the number of unassigned (empty) and assigned seats
• Finally, print a sorted list of all of the names in the seating chart
NOTE – make it easy for the user. The user should be allowed to enter row 1 to mean the first row. The same is true for column. Do not make the user use 0 to represent the first row or column.
SeatChart class will maintain all of the information about the two-dimensional array. It should look something like:
public class SeatChart {
// Declare the array or ArrayList
// for two-dimensional seating chart
// May want another array/ArrayList to remember
// all names (for last method)
public SeatChart()
{
// Add what you need here
// Initialize all seats to "......." (represents empty seat)
}
public void printSeatingChart()
{
// Add code to print the two-dimensional array/ArrayList
// Print each name 10 characters wide
}
public void addPerson(int row, int column, String name)
{
// Put name in the correct element in 2-dimensional array
// May want to also add name to the names list (extra array)
}
public int getEmpty() ()
{
// Calculate and return the number of unassigned seats
}
public int getAssigned() ()
{
// Calculate and return the number of assigned seats
}
public void printNames() ()
{
// Print all of the names (not ".......") in sorted order
// - one per line
// NOTES:
// If you use an ArrayList for names (called names)
// then you can sort the Strings in the ArrayList with:
// Collections.sort(names);
// You’ll need to import java.util.Collections;
}
}
SeatChartTest class will contain main(). It should do all of the user-prompting/reading and call SeatChart methods.
Requirements:
• Put System.out.println() after each line of input. This will help you match the Test Program and make it easier to use it.
Sample Run #1: (the highlighted text is what the user types)
Name? Bob
Row Col? 2 4
Name? Jack
Row Col? 3 1
Name? Ann
Row Col? 1 5
Name? exit
....... ....... ....... ....... Ann .......
....... ....... ....... Bob ....... .......
Jack ....... ....... ....... ....... .......
....... ....... ....... ....... ....... .......
Empty chairs 21
Assigned chairs 3
Ann
Bob
Jack
Extra Notes:
• Did you correctly name the package/folder?
• Did you correctly name the class/file?
• Did you include comments?
Explanation / Answer
SeatChart.java
public class SeatChart {
// Declare the array or ArrayList
// for two-dimensional seating chart
// May want another array/ArrayList to remember
// all names (for last method)
String names[][] = new String[4][6];
public SeatChart() {
// Add what you need here
// Initialize all seats to "......." (represents empty seat)
for (int i = 0; i < names.length; i++) {
for (int j = 0; j < names[0].length; j++) {
names[i][j] = "........";
}
}
}
public void printSeatingChart() {
// Add code to print the two-dimensional array/ArrayList
// Print each name 10 characters wide
for (int i = 0; i < names.length; i++) {
for (int j = 0; j < names[0].length; j++) {
System.out.printf("%10s", names[i][j]);
}
System.out.println();
}
}
public void addPerson(int row, int column, String name) {
// Put name in the correct element in 2-dimensional array
// May want to also add name to the names list (extra array)
names[row][column] = name;
}
public int getEmpty() {
int count = 0;
// Calculate and return the number of unassigned seats
for (int i = 0; i < names.length; i++) {
for (int j = 0; j < names[0].length; j++) {
if (names[i][j].equals("........")) {
count++;
}
}
}
return count;
}
public int getAssigned() {
// Calculate and return the number of assigned seats
int count = 0;
// Calculate and return the number of unassigned seats
for (int i = 0; i < names.length; i++) {
for (int j = 0; j < names[0].length; j++) {
if (!names[i][j].equals("........")) {
count++;
}
}
}
return count;
}
public void printNames() {
// Print all of the names (not ".......") in sorted order
// - one per line
// NOTES:
// If you use an ArrayList for names (called names)
// then you can sort the Strings in the ArrayList with:
// Collections.sort(names);
// You’ll need to import java.util.Collections;
String temp;
//Sort the array by city name or column 3
for (int i = 0; i < names.length; i++) {
for (int j = 1; j < names[0].length; j++) {
if (names[i][j - 1].compareTo(names[i][j]) > 0) {
temp = names[i][j - 1];
names[i][j - 1] = names[i][j];
names[i][j] = temp;
}
}
}
for (int i = 0; i < names.length; i++) {
for (int j = 1; j < names[0].length; j++) {
if (!names[i][j].equals("........"))
System.out.println(names[i][j]);
}
}
}
}
____________________
SeatChartTest.java
import java.util.Scanner;
public class SeatChartTest {
public static void main(String[] args) {
//Declaring variables
String name = "";
int row, col;
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
//Getting the input entered by the user until user enters exit
SeatChart seatc = new SeatChart();
System.out.print("Name? ");
name = sc.next();
System.out.print("Row Col? ");
row = sc.nextInt();
while (true) {
col = sc.nextInt();
seatc.addPerson(row - 1, col - 1, name);
System.out.print("Name? ");
name = sc.next();
if (name.equalsIgnoreCase("Exit"))
break;
System.out.print("Row Col? ");
row = sc.nextInt();
}
seatc.printSeatingChart();
int emptyChairs = seatc.getEmpty();
int assignedChairs = seatc.getAssigned();
System.out.println("Empty chairs :" + emptyChairs);
System.out.println("Assigned chairs :" + assignedChairs);
seatc.printNames();
}
}
____________________
Output:
Name? Bob
Row Col? 2 4
Name? Jack
Row Col? 3 1
Name? Ann
Row Col? 1 5
Name? exit
........ ........ ........ ........ Ann ........
........ ........ ........ Bob ........ ........
Jack ........ ........ ........ ........ ........
........ ........ ........ ........ ........ ........
Empty chairs :21
Assigned chairs :3
Ann
Bob
Jack
_____________Could you rate me well.Plz .Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.