I dont know where to start please help Java import java.io.*; import java.util.S
ID: 3810856 • Letter: I
Question
I dont know where to start please help Java
import java.io.*;
import java.util.Scanner;
import java.util.*;
public class lab12 {
/************************************************************************************************************
* The following method is not for you to modify.
* It is given to you because it is needed in the main
* method.
* It takes two integers, which represent a number of rows
* and a number of seats per row
* It generates and returns a 2D array of integers of size
* row x seats, which contains integer prices for each
* corresponding seat
*********************************************************/
public static int[][] loadInfo(int row, int seats) {
int[][] seatsPrices = new int[row][seats];
for (int i=0; i<row; i++) {
for (int j=0; j< seats; j++) {
seatsPrices[i][j]=(int) Math.floor(Math.random() * 101);
if (seatsPrices[i][j] < 30) seatsPrices[i][j]+=30;
System.out.print(seatsPrices[i][j] + " ");
}
System.out.println();
}
return seatsPrices;
}
/************************************************************************************************************
* Method: takeReservation
* Takes: a 2D array that contains seats (the number of
* rows and columns) and prices for each seat
* (the integer stored in each location of the array)
* Returns: nothing (but makes changes to the 2D array)
*
* What it does:
* It lets the user know how many rows of seats and seats per row there are
* so that the user knows what seats s/he can choose from
* It asks the user how many seat s/he wants to reserve
* It prompts the user to choose the number of seats s/he specified
* If all these seats are available,
* they are marked as reserved (by putting a 0 in the array
* as shown in the lab description), and
* the program prints out the transaction's total price
* and proposes to the user to make another reservation (proceeds if user agrees, exits otherwise)
* Otherwise,
* the program should let the user know that some or
* all seats are already reserved (and lists them), and
* if the user is willing to modify the request:
* ask again
* otherwise
* print out that no transaction took place
* exit
* Keep doing this until the user either decides not to
* modify the request or the request is valid
*********************************************************/
public static void takeReservation(int[][] seats) {
Scanner input = new Scanner(System.in);
System.out.println("Welcome to the Plaza Theater's seat reservation system!");
// your code goes here
}
/************************************************************************************************************
* Method: showAvailableSeats
* Takes: a 2D array that contains seats (the number of
* rows and columns) and prices for each seat
* (the integer stored in each location of the array)
* and 2 integers (min and max) that represent the lower
* and upper bound for the price of seats of interest
* Returns: nothing
*
* What it does:
* It prints out all the seats in the 2D array that are within the
* price range specified by [min, max]
* If there is no such seat, the user is told so.
*********************************************************/
public static void showAvailableSeats(int[][] seats, int min, int max) {
int count = 0;
for (int i = 0; i < seats.length; i++) {
for (int j = 0; j < seats[0].length; j++) {
// your code goes here
}
}
if (count == 0) System.out.println("It looks like there is no seat that matches your price requirements.");
}
/************************************************************************************************************
* Method: showAvailableSeats
* Takes: a 2D array that contains seats (the number of
* rows and columns) and prices for each seat
* (the integer stored in each location of the array)
* and 1 integer (row) that represent the row of the seats
* of interest
* Returns: nothing
*
* What it does:
* It prints out all the seats in the 2D array that are within the
* given row and available
* If there is no such seat, the user is told so.
*********************************************************/
public static void showAvailableSeats(int[][] seats, int row) {
int count = 0;
for (int j = 0; j < seats[0].length; j++) {
// your code goes here
}
if (count == 0) System.out.println("It looks like there is no seat available in row " + row + ".");
}
/************************************************************************************************************
* Method: copy (non recursive: using loops)
* Takes: a 2D array that contains seats (the number of
* rows and columns) and prices for each seat
* (the integer stored in each location of the array)
* Returns: a 2D array which is a replica of the input 2D array
*********************************************************/
public static int[][] copy(int[][] seats) {
// your code goes here
}
/************************************************************************************************************
* Method: copyR (recursive: no loop)
* Takes: a 2D array that contains seats (the number of
* rows and columns) and prices for each seat
* (the integer stored in each location of the array)
* Creates (but does not return): a 2D array which is a replica of the input 2D array
*
* copyR uses what is at times called an "auxiliary" method: copyRowR
* which copies a given row of the input of copyR into the expected output 2D array of copyR
*********************************************************/
public static void copyR(int[][] seats, int[][] result, int start) {
// your code goes here
}
/************************************************************************************************************
* Method: copyRowR (recursive: no loop)
* Takes: a 1D array that represents a row of the input of method copyR
* Creates (but does not return): a 1D array which is a replica of the input 1D array
*********************************************************/
public static void copyRowR(int[] seatsRow, int[] resultRow, int init) {
if (init >= seatsRow.length) return;
resultRow[init] = seatsRow[init];
copyRowR(seatsRow, resultRow, init+1);
}
/************************************************************************************************************
* Method: print
* Takes: a 2D array that contains seats (the number of
* rows and columns) and prices for each seat
* (the integer stored in each location of the array)
* Returns: nothing
* Prints the content of the input 2D array (row by row)
*********************************************************/
public static void print(int[][] A) {
// your code goes here
}
/***************************************************************************************************************
* This is the main method, from which all above methods are being used
* Do not modify this method.
***************************************************************************************************************/
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String answer;
int min, max, row;
// load a 2D array of prices
int[][] seatsAndPrices = loadInfo(5,10);
// copy seatsAndPrices in a backup 2D array in case we need to change reservations
//int[][] backUp = copy(seatsAndPrices);
int[][] backUp = new int[seatsAndPrices.length][seatsAndPrices[0].length];
copyR(seatsAndPrices, backUp, 0);
print(backUp);
// runs the method that shows seats within a given price range
System.out.println("Would you like to see the seats options within a chosen price range?");
answer = in.next();
if (answer.toLowerCase().compareTo("yes")==0) {
System.out.println("What is the max anount you are ready to pay for a seat?");
max = in.nextInt();
System.out.println("What is the lowest price you'd like to consider?");
min = in.nextInt();
showAvailableSeats(seatsAndPrices, min, max);
}
// runs the method that shows seats within a given row
System.out.println("Would you like to see the available seats in a given row?");
answer = in.next();
if (answer.toLowerCase().compareTo("yes")==0) {
System.out.println("What row are you interested in?");
row = in.nextInt();
showAvailableSeats(seatsAndPrices, row);
}
// runs the reservation method
takeReservation(seatsAndPrices);
}
}
Explanation / Answer
/*
*Thank you ,and the program assumes that the user always provides correct input as per the prompts
*feel free to comment for any doubts or changes in the code,All the Best :)
*/
import java.io.*;
import java.util.Scanner;
import java.util.*;
public class lab12 {
/************************************************************************************************************
* The following method is not for you to modify.
* It is given to you because it is needed in the main
* method.
* It takes two integers, which represent a number of rows
* and a number of seats per row
* It generates and returns a 2D array of integers of size
* row x seats, which contains integer prices for each
* corresponding seat
*********************************************************/
public static int[][] loadInfo(int row, int seats) {
int[][] seatsPrices = new int[row][seats];
for (int i=0; i<row; i++) {
for (int j=0; j< seats; j++) {
seatsPrices[i][j]=(int) Math.floor(Math.random() * 101);
if (seatsPrices[i][j] < 30) seatsPrices[i][j]+=30;
System.out.print(seatsPrices[i][j] + " ");
}
System.out.println();
}
return seatsPrices;
}
/************************************************************************************************************
* Method: takeReservation
* Takes: a 2D array that contains seats (the number of
* rows and columns) and prices for each seat
* (the integer stored in each location of the array)
* Returns: nothing (but makes changes to the 2D array)
*
* What it does:
* It lets the user know how many rows of seats and seats per row there are
* so that the user knows what seats s/he can choose from
* It asks the user how many seat s/he wants to reserve
* It prompts the user to choose the number of seats s/he specified
* If all these seats are available,
* they are marked as reserved (by putting a 0 in the array
* as shown in the lab description), and
* the program prints out the transaction's total price
* and proposes to the user to make another reservation (proceeds if user agrees, exits otherwise)
* Otherwise,
* the program should let the user know that some or
* all seats are already reserved (and lists them), and
* if the user is willing to modify the request:
* ask again
* otherwise
* print out that no transaction took place
* exit
* Keep doing this until the user either decides not to
* modify the request or the request is valid
*********************************************************/
public static void takeReservation(int[][] seats) {
Scanner input = new Scanner(System.in);
System.out.println("Welcome to the Plaza Theater's seat reservation system!");
// your code goes here
System.out.println("There are "
+ "Number of rows :"+seats.length+" "
+ "Number of seats per Row:"+seats[0].length+" ");
System.out.println("Please enter the number of seats to book:");
int noOfSeats = input.nextInt();
int seatNums[] =new int[noOfSeats];
String str ="";
while(!str.equalsIgnoreCase("exit"))
{
int selectedRow[]=new int[noOfSeats];
boolean notSelected=true;;
for(int i=0;i<seats.length&¬Selected;i++)
{
showAvailableSeats(seats, i);
int yesNo=0;
System.out.println("Enter 1 if you wish to select from this Row else Enter 0");
yesNo=input.nextInt();
if(yesNo==1){
for(int j=0;j<noOfSeats;j++)
{
selectedRow[j]=i+1;
System.out.println("Enter seat number "+(j+1)+" :");
int seatNum= input.nextInt()-1;
System.out.println("selected row"+(i+1)+" seat:"+(seatNum+1));
seatNums[j]=seatNum+1;
}
notSelected=false;
}
}
//making reservation
int sum =0;
for(int i=0;i<noOfSeats;i++)
{
sum=sum+seats[selectedRow[i]][seatNums[i]];
seats[selectedRow[i]][seatNums[i]]=0;
}
System.out.println("*************************************************");
print(seats);
System.out.println("*************************************************");
System.out.println("Transaction Reciept");
for(int i=0;i<noOfSeats;i++){
System.out.println("SEAT "+(i+1)+":Row:"+selectedRow[i]+":seat:"+(seatNums[i]));
}
System.out.println("total price of the tickets $"+sum);
System.out.println("Do you want to make another transaction,type : yes , "
+ "else type exit to quit ");
str =input.next();
}
}
/************************************************************************************************************
* Method: showAvailableSeats
* Takes: a 2D array that contains seats (the number of
* rows and columns) and prices for each seat
* (the integer stored in each location of the array)
* and 2 integers (min and max) that represent the lower
* and upper bound for the price of seats of interest
* Returns: nothing
*
* What it does:
* It prints out all the seats in the 2D array that are within the
* price range specified by [min, max]
* If there is no such seat, the user is told so.
*********************************************************/
public static void showAvailableSeats(int[][] seats, int min, int max) {
int count = 0;
for (int i = 0; i < seats.length; i++) {
for (int j = 0; j < seats[0].length; j++) {
// your code goes here
if(seats[i][j]>=min&&seats[i][j]<=max&&seats[i][j]!=0)
{
count++;
System.out.print(seats[i][j]+" ");
}
}
}
System.out.println();
if (count == 0) System.out.println("It looks like there is no seat that matches your price requirements.");
}
/************************************************************************************************************
* Method: showAvailableSeats
* Takes: a 2D array that contains seats (the number of
* rows and columns) and prices for each seat
* (the integer stored in each location of the array)
* and 1 integer (row) that represent the row of the seats
* of interest
* Returns: nothing
*
* What it does:
* It prints out all the seats in the 2D array that are within the
* given row and available
* If there is no such seat, the user is told so.
*********************************************************/
public static void showAvailableSeats(int[][] seats, int row) {
int count = 0;
for (int j = 0; j < seats[0].length; j++) {
if(seats[row][j]!=0){
count++;
System.out.print(seats[row][j]+" ");
}
}
System.out.println();
if (count == 0) System.out.println("It looks like there is no seat available in row " + row + ".");
}
/************************************************************************************************************
* Method: copy (non recursive: using loops)
* Takes: a 2D array that contains seats (the number of
* rows and columns) and prices for each seat
* (the integer stored in each location of the array)
* Returns: a 2D array which is a replica of the input 2D array
*********************************************************/
public static int[][] copy(int[][] seats) {
int[][] backUp = new int[seats.length][seats[0].length];
for(int i=0;i<seats.length;i++)
for(int j=0;j<seats[0].length;j++)
{
backUp[i][j] = seats[i][j];
}
return backUp;
}
/************************************************************************************************************
* Method: copyR (recursive: no loop)
* Takes: a 2D array that contains seats (the number of
* rows and columns) and prices for each seat
* (the integer stored in each location of the array)
* Creates (but does not return): a 2D array which is a replica of the input 2D array
*
* copyR uses what is at times called an "auxiliary" method: copyRowR
* which copies a given row of the input of copyR into the expected output 2D array of copyR
*********************************************************/
public static void copyR(int[][] seats, int[][] result, int start) {
// your code goes here
if (start >= seats.length) return;
result[start] = seats[start];
copyR(seats, result, start+1);
}
/************************************************************************************************************
* Method: copyRowR (recursive: no loop)
* Takes: a 1D array that represents a row of the input of method copyR
* Creates (but does not return): a 1D array which is a replica of the input 1D array
*********************************************************/
public static void copyRowR(int[] seatsRow, int[] resultRow, int init) {
if (init >= seatsRow.length) return;
resultRow[init] = seatsRow[init];
copyRowR(seatsRow, resultRow, init+1);
}
/************************************************************************************************************
* Method: print
* Takes: a 2D array that contains seats (the number of
* rows and columns) and prices for each seat
* (the integer stored in each location of the array)
* Returns: nothing
* Prints the content of the input 2D array (row by row)
*********************************************************/
public static void print(int[][] A) {
// your code goes here
System.out.println("Printing backup");
for(int i=0;i<A.length;i++)
{
for(int j=0;j<A[0].length;j++)
{
System.out.print(A[i][j]+" ");
}
System.out.println();
}
}
/***************************************************************************************************************
* This is the main method, from which all above methods are being used
* Do not modify this method.
***************************************************************************************************************/
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String answer;
int min, max, row;
// load a 2D array of prices
int[][] seatsAndPrices = loadInfo(5,10);
// copy seatsAndPrices in a backup 2D array in case we need to change reservations
int[][] backUp = copy(seatsAndPrices);
// int[][] backUp = new int[seatsAndPrices.length][seatsAndPrices[0].length];
copyR(seatsAndPrices, backUp, 0);
print(backUp);
// runs the method that shows seats within a given price range
System.out.println("Would you like to see the seats options within a chosen price range?");
answer = in.next();
if (answer.toLowerCase().compareTo("yes")==0) {
System.out.println("What is the max anount you are ready to pay for a seat?");
max = in.nextInt();
System.out.println("What is the lowest price you'd like to consider?");
min = in.nextInt();
showAvailableSeats(seatsAndPrices, min, max);
}
// runs the method that shows seats within a given row
System.out.println("Would you like to see the available seats in a given row?");
answer = in.next();
if (answer.toLowerCase().compareTo("yes")==0) {
System.out.println("What row are you interested in?");
row = in.nextInt();
showAvailableSeats(seatsAndPrices, row);
}
// runs the reservation method
takeReservation(seatsAndPrices);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.