Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Im working on this coding assignment and i want to add a functionlaity in the co

ID: 3686545 • Letter: I

Question

Im working on this coding assignment and i want to add a functionlaity in the code.Please look at the bolded sentences in the code.

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

public class Activity2{

public static void main(String[] args) throws IOException {

String[][] seat = {{"10","10","10","10","10","10","10","10","10","10"},

   {"10","10","10","10","10","10","10","10","10","10"},

   {"10","10","10","10","10","10","10","10","10","10"},

   {"10","10","20","20","20","20","20","20","10","10"},

   {"10","10","20","20","20","20","20","20","10","10"},

   {"10","10","20","20","20","20","20","20","10","10"},

   {"20","20","30","30","40","40","30","30","20","20"},

   {"20","30","30","40","50","50","40","30","30","20"},

   {"30","40","50","50","50","50","50","50","40","30"}};

print(seat);

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter 1 to choose by seat or 2 to choose by price");

while(true) {

int ch = Integer.parseInt(br.readLine());

if (ch == 1) {

do {

System.out.println("Enter row:");

int r = Integer.parseInt(br.readLine());

while (!(r > 0 && r < 10)) {

System.out.println("Invalid..try again");

r = Integer.parseInt(br.readLine());

}

System.out.println("Enter seat:");

String col = br.readLine().trim().toUpperCase();

while (!(col.equals("A") || col.equals("B") || col.equals("C") || col.equals("D") || col.equals("E") ||

col.equals("F") || col.equals("G") || col.equals("H") || col.equals("I") || col.equals("J"))) {

System.out.println("Invalid..try again");

col = br.readLine();

}

String s = seat[9-r][getColumn(col)];

if(s.equals("XX")){

System.out.println("That seat is taken, please try again");

OVER HERE IF THE USER CHOOSES A SEAT WHICH IS ALREADY TAKEN I WANT TO GIVE THE OPTION "ENTER 1 TO CHOOSE BY SEAT OR 2 BY PRICE" AND THEN THE PROGRAM SHOULD GIVE THE ROW AND SEAT OPTION IF CHOICE IS 1 OR PRICE OPTION IF CHOICE IS 2 AND EXECUTE ACCORDINGLY LIKE ITS HAPPENING EVERYHWERE ELSE IN THE CODE.

continue;

}else {

seat[9-r][getColumn(col)] = "XX";

System.out.println("Your seat is booked!!");

print(seat);

System.out.println("Enter 1 to choose by seat or 2 to choose by price");

break;

}

}while (true);

} else if (ch == 2) {

do {

System.out.println("Enter a price(10,20,30,40,50):");

String price = br.readLine().trim();

while (!(price.equals("10") || price.equals("20") || price.equals("30") || price.equals("40") || price.equals("50"))) {

System.out.println("That is not a valid price, please enter(10,20,30,40,50):");

price = br.readLine();

}

boolean found = false;

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

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

String s = seat[i][j];

if((!found) && s.equals(price)){

seat[i][j] = "XX";

found = true;

print(seat);

System.out.println("Your seat is in row "+(9-i)+" seat "+((char)(65+j)));

//System.out.println("Enter 1 to choose by seat or 2 to choose by price");

break;

}

}

}

//System.out.println("No seat fount with that price.");

System.out.println("Enter 1 to choose by seat or 2 to choose by price");

break;

}while (true);

} else {

System.out.println("This is not a valid number, please enter either 1 or 2:");

}

}

}

public static void print(String[][] seat){

      

      

      

System.out.println(" A B C D E F G H I J");

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

System.out.print("Row " + (9 - i));

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

System.out.print(" "+seat[i][j]);

}

System.out.println("");

}

}

public static int getColumn(String col) {

switch (col) {

case "A":

return 0;

case "B":

return 1;

case "C":

return 2;

case "D":

return 3;

case "E":

return 4;

case "F":

return 5;

case "G":

return 6;

case "H":

return 7;

case "I":

return 8;

case "J":

return 9;

default:

return -1;

}

}

}

Explanation / Answer

Activity2.java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Activity2{

public static void main(String[] args) throws IOException {

String[][] seat = {{"10","10","10","10","10","10","10","10","10","10"},
{"10","10","10","10","10","10","10","10","10","10"},
{"10","10","10","10","10","10","10","10","10","10"},
{"10","10","20","20","20","20","20","20","10","10"},
{"10","10","20","20","20","20","20","20","10","10"},
{"10","10","20","20","20","20","20","20","10","10"},
{"20","20","30","30","40","40","30","30","20","20"},
{"20","30","30","40","50","50","40","30","30","20"},
{"30","40","50","50","50","50","50","50","40","30"}};

print(seat);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter 1 to choose by seat or 2 to choose by price");

while(true) {

int ch = Integer.parseInt(br.readLine());

if (ch == 1) {

do {
System.out.println("Enter row:");
int r = Integer.parseInt(br.readLine());
while (!(r > 0 && r < 10)) {
System.out.println("Invalid..try again");
r = Integer.parseInt(br.readLine());
}

System.out.println("Enter seat:");
String col = br.readLine().trim().toUpperCase();
while (!(col.equals("A") || col.equals("B") || col.equals("C") || col.equals("D") || col.equals("E") ||
col.equals("F") || col.equals("G") || col.equals("H") || col.equals("I") || col.equals("J"))) {
System.out.println("Invalid..try again");
col = br.readLine();
  
}

String s = seat[9-r][getColumn(col)];
if(s.equals("XX")){
System.out.println("That seat is taken, please try again");
//OVER HERE IF THE USER CHOOSES A SEAT WHICH IS ALREADY TAKEN I WANT TO GIVE THE OPTION "ENTER 1 TO CHOOSE BY SEAT OR 2 BY PRICE" AND THEN THE
//PROGRAM SHOULD GIVE THE ROW AND SEAT OPTION IF CHOICE IS 1 OR PRICE OPTION IF CHOICE IS 2 AND EXECUTE ACCORDINGLY
//LIKE ITS HAPPENING EVERYHWERE ELSE IN THE CODE.

  
System.out.println("Enter 1 to choose by seat or 2 to choose by price");
continue;
}else {
seat[9-r][getColumn(col)] = "XX";
System.out.println("Your seat is booked!!");
print(seat);
System.out.println("Enter 1 to choose by seat or 2 to choose by price");
break;
}
}while (true);


} else if (ch == 2) {

do {
System.out.println("Enter a price(10,20,30,40,50):");
String price = br.readLine().trim();
while (!(price.equals("10") || price.equals("20") || price.equals("30") || price.equals("40") || price.equals("50"))) {
System.out.println("That is not a valid price, please enter(10,20,30,40,50):");
price = br.readLine();
}

boolean found = false;
for(int i=0; i<9; i++){
for(int j=0; j<10 ; j++){
String s = seat[i][j];
if((!found) && s.equals(price)){
seat[i][j] = "XX";
found = true;
print(seat);
System.out.println("Your seat is in row "+(9-i)+" seat "+((char)(65+j)));
//System.out.println("Enter 1 to choose by seat or 2 to choose by price");
break;
}
}
}
//System.out.println("No seat fount with that price.");
System.out.println("Enter 1 to choose by seat or 2 to choose by price");
break;
}while (true);


} else {
System.out.println("This is not a valid number, please enter either 1 or 2:");
}
}

}

public static void print(String[][] seat){

System.out.println(" A B C D E F G H I J");
for(int i=0; i<9; i++) {
System.out.print("Row " + (9 - i));
for (int j = 0; j < 10; j++) {
System.out.print(" "+seat[i][j]);
}
System.out.println("");
}
}

public static int getColumn(String col) {

switch (col) {
case "A":
return 0;
case "B":
return 1;
case "C":
return 2;
case "D":
return 3;
case "E":
return 4;
case "F":
return 5;
case "G":
return 6;
case "H":
return 7;
case "I":
return 8;
case "J":
return 9;
default:
return -1;
}
}
}

output

Enter 1 to choose by seat or 2 to choose by price                                                                                                                               

1                                                                                                                                                                               

Enter row:                                                                                                                                                                      

1                                                                                                                                                                               

Enter seat:                                                                                                                                                                     

A                                                                                                                                                                               

That seat is taken, please try again                                                                                                                                            

Enter 1 to choose by seat or 2 to choose by price                                                                                                                               

Enter row

1                                                                                                                                                                               

Enter seat:                                                                                                                                                                     

A                                                                                                                                                                               

That seat is taken, please try again                                                                                                                                            

Enter 1 to choose by seat or 2 to choose by price                                                                                                                               

Enter row: