Debug this program public class Main { public static void main (String[] args) {
ID: 3908349 • Letter: D
Question
Debug this program public class Main { public static void main (String[] args) { Scanner scan=new Scanner(System.in); Seat[][]array=new Seat[7][4]; Seat desiredSeat; int count=0; int row; String name; for (int i = 0; i<7; i++){ array[i][0]=new Seat("A"); array[i][1]=new Seat("B"); array[i][2]=new Seat("C"); array[i][3]=new Seat("D"); } do{ do{ printArray(array); System.out.println("Which place do you want to take: "); row=scan.nextInt(); name=scan.next(); }while(row>7); for(int i=0;i<4;i++){ if(array[row-1][i].getName().equalsIgnoreCase(name)){ if(array[row-1][i].getVisible()==false){ System.out.println("The seat is already taken, please select another one."); } else{ array[row-1][i].setVisible(false); count++; } } } }while(count!=28); printArray(array); System.out.println("All seats have been sold!!!"); } public static void printArray(Seat[][] a){ for(int i=0;i<7;i++){ System.out.print(i+1+" "); for(int j=0;j<4;j++){ System.out.print(a[i][j]+" "); } System.out.println(); } } }
Explanation / Answer
Hello there. You wanted to debug your code, but you didn’t specify what to debug. Mainly you didn’t provide the Seat class. So I created one myself according to my understanding. Made some minor changes in Main class, and everything works as you needed. But there’s one more thing I noticied. Currently the main loop run for a long time (to fill all 28 seats). There should be an option to let the user choose to continue booking more seats or not. Anyway I didn’t change it, if you want to change, please mention in the comments. Also if this is not what you are looking for, drop a comment, I’ll help. Thanks.
// Main.java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Seat[][] array = new Seat[7][4];
Seat desiredSeat;
int count = 0;
int row;
String name;
for (int i = 0; i < 7; i++) {
array[i][0] = new Seat("A");
array[i][1] = new Seat("B");
array[i][2] = new Seat("C");
array[i][3] = new Seat("D");
}
do {
do {
printArray(array);
System.out.println("Which place do you want to take: ");
row = scan.nextInt();
name = scan.next();
} while (row > 7);
for (int i = 0; i < 4; i++) {
if (array[row - 1][i].getName().equalsIgnoreCase(name)) {
if (array[row - 1][i].getVisible() == false) {
System.out
.println("The seat is already taken, please select another one.");
} else {
array[row - 1][i].setVisible(false);
count++;
}
}
}
/**
* suggestion: there should be another prompt here to ask user if he
* wants to continue booking more seats, or else you have to enter
* all 28 values
*/
} while (count != 28);
printArray(array);
if (count == array.length) {
System.out.println("All seats have been sold!!!");
}
}
public static void printArray(Seat[][] a) {
for (int i = 0; i < 7; i++) {
System.out.print(i + 1 + " ");
for (int j = 0; j < 4; j++) {
System.out.print(a[i][j] + " ");
}
System.out.println();
}
}
}
// Seat.java
public class Seat {
//attributes
private String name;
private boolean visible;
//constructor
public Seat(String name) {
this.name = name;
visible = true;//default visibility
}
//getters and setters
public String getName() {
return name;
}
public void setVisible(boolean b) {
visible = b;
}
public boolean getVisible() {
return visible;
}
@Override
public String toString() {
//returning seat name
return name;
}
}
/*OUTPUT (partial)*/
1 A B C D
2 A B C D
3 A B C D
4 A B C D
5 A B C D
6 A B C D
7 A B C D
Which place do you want to take:
1 A
1 A B C D
2 A B C D
3 A B C D
4 A B C D
5 A B C D
6 A B C D
7 A B C D
Which place do you want to take:
1 A
The seat is already taken, please select another one.
1 A B C D
2 A B C D
3 A B C D
4 A B C D
5 A B C D
6 A B C D
7 A B C D
Which place do you want to take:
5 D
1 A B C D
2 A B C D
3 A B C D
4 A B C D
5 A B C D
6 A B C D
7 A B C D
Which place do you want to take:
5 D
The seat is already taken, please select another one.
1 A B C D
2 A B C D
3 A B C D
4 A B C D
5 A B C D
6 A B C D
7 A B C D
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.