Question

In: Computer Science

Question: Write a program that prompts users to pick either a seat or a price. Mark...

Question:

Write a program that prompts users to pick either a seat or a price. Mark sold seats by changing the price to 0. When a user specifies a seat, make sure it is available. When a user specifies a price, find any seat with that price.

Code Provided:

(Please help me finish the line marked with "Complete this method", Please don't change other lines.)

Besides, if you could kindly mark each your line is for what purpose, I'll be very happy.

import java.util.Scanner;

public class SeatingChart
{
/**
Prints the price of seats in a grid like pattern.
@param seats a 2D array of prices
*/
public void printSeats(int[][] seats)
{
   System.out.print(" ");
   for (int j = 0; j < seats[0].length; j++)
{
System.out.print("s" + (j+1) + " ");
}
   System.out.println();
     
for (int i = 0; i < seats.length; i++)
{
   System.out.print("r" + (seats.length-i) + ":");
for (int j = 0; j < seats[i].length; j++)
{
System.out.printf("%3d", seats[i][j]);
}
System.out.println();
}
}

/**
Marks a seat with the price given to 0.
If there is no seat with the price, print an error message.
@param seats: the array of seat prices
@param price: the price to mark to zero
*/
public void sellSeatByPrice(int[][] seats, int price)
{
   // COMPLETE THIS METHOD
  
     
     
// System.out.println("Sorry, no seat found with that price.");
}

/**
Marks a seat based on a given row and seat number from input.
If seat or row numbers are invalid, print error messages.
If the seat is already occupied, print error message.
@param seats: the array of seat prices
@param rownum: row number
@param seatnum: seat number
*/
public void sellSeatByNumber(int[][] seats, int rownum, int seatnum)
{
   // COMPLETE THIS METHOD
  
     
     
//System.out.println("Sorry, seat already occupied.");
//System.out.println("Sorry, invalid seat number.");
//System.out.println("Sorry, invalid row.");
}

public static void main(String[] args)
{
// initial values come from problem description
int[][] seats = { { 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 } };

SeatingChart seating = new SeatingChart();
seating.printSeats(seats);

System.out.println("Pick by <s>eat or <p>rice or <q> to quit: ");
Scanner in = new Scanner(System.in);
String choice = in.next();
while (!choice.equals("q"))
{
if (choice.equals("s"))
{
   System.out.println("Enter the row number you want: ");
int rownum = in.nextInt();
System.out.println("Enter the seat number you want: ");
int seatnum = in.nextInt();
seating.sellSeatByNumber(seats, rownum, seatnum);
}
else
{
// pick by price
System.out.println("What price do you want to buy?");
int price = in.nextInt();
seating.sellSeatByPrice(seats, price);
}
seating.printSeats(seats);
System.out.println("Pick by <s>eat or <p>rice or <q> to quit: ");
choice = in.next();
}
  
in.close();
}
}

Solutions

Expert Solution

PLEASE GIVE THUMBS UP, THANKS
SAMPLE OUTPUT :

code :


/**
*
* @author VISHAL
*/
import java.util.Scanner;

public class SeatingChart
{
/**
Prints the price of seats in a grid like pattern.
@param seats a 2D array of prices
*/
public void printSeats(int[][] seats)
{
System.out.print(" ");
for (int j = 0; j < seats[0].length; j++)
{
System.out.print("s" + (j+1) + " ");
}
System.out.println();

for (int i = 0; i < seats.length; i++)
{
System.out.print("r" + (seats.length-i) + ":");
for (int j = 0; j < seats[i].length; j++)
{
System.out.printf("%3d", seats[i][j]);
}
System.out.println();
}
}

/**
Marks a seat with the price given to 0.
If there is no seat with the price, print an error message.
@param seats: the array of seat prices
@param price: the price to mark to zero
*/
public void sellSeatByPrice(int[][] seats, int price)
{
// COMPLETE THIS METHOD
boolean seatfound=false;
int row=0,col=0;
for(int i=0; i<seats.length && seatfound==false; i++)
{
for(int j=0; j<seats[0].length &&seatfound==false; j++)
{
if(seats[i][j]==price)
{
seatfound=true;
row=i;
col=j;
seats[row][col]=0;
break;
}
}
}
if(seatfound)
{
System.out.println("Congratulation, you got seat with row no :"+(seats.length-row)+" and seat no : "+(col+1));
}
else
{
System.out.println("Sorry, no seat found with that price.");   
}
}

/**
Marks a seat based on a given row and seat number from input.
If seat or row numbers are invalid, print error messages.
If the seat is already occupied, print error message.
@param seats: the array of seat prices
@param rownum: row number
@param seatnum: seat number
*/
public void sellSeatByNumber(int[][] seats, int rownum, int seatnum)
{
// COMPLETE THIS METHOD
if((rownum<1 || rownum>seats.length))
{
System.out.println("Sorry, invalid row.");
}
else if((seatnum<1 || seatnum>seats[0].length))
{
System.out.println("Sorry, invalid seat number.");
}
else
{
if(seats[seats.length-rownum][seatnum-1]==0)
{
System.out.println("Sorry, seat already occupied.");
}
else
{
seats[seats.length-rownum][seatnum-1]=0;
System.out.println("You got the Seat");
}
}

}

public static void main(String[] args)
{
// initial values come from problem description
int[][] seats = { { 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 } };

SeatingChart seating = new SeatingChart();
seating.printSeats(seats);

System.out.println("Pick by <s>eat or <p>rice or <q> to quit: ");
Scanner in = new Scanner(System.in);
String choice = in.next();
while (!choice.equals("q"))
{
if (choice.equals("s"))
{
System.out.println("Enter the row number you want: ");
int rownum = in.nextInt();
System.out.println("Enter the seat number you want: ");
int seatnum = in.nextInt();
seating.sellSeatByNumber(seats, rownum, seatnum);
}
else
{
// pick by price
System.out.println("What price do you want to buy?");
int price = in.nextInt();
seating.sellSeatByPrice(seats, price);
}
seating.printSeats(seats);
System.out.println("Pick by <s>eat or <p>rice or <q> to quit: ");
choice = in.next();
}
  
in.close();
}
}


Related Solutions

C++ Question: write a program that prompts the user for the length and width of a...
C++ Question: write a program that prompts the user for the length and width of a rectangle in inches.  The program then uses functions to compute the perimeter and area of the rectangle and to convert those to meters and square meters respectively. Sample output from one instance of the program is shown below: ```html Welcome to the Foot-To-Meter Rectangle Calculator ================================================= Enter the rectangle length in feet: 2 Enter the rectangle width in feet: 3 The rectangle dimensions are: 0.61...
Question: Write a program in python that reads in the file climate_data_2017_numeric.csv and prompts the user...
Question: Write a program in python that reads in the file climate_data_2017_numeric.csv and prompts the user to enter the name of a field (other than Date), and then outputs the highest and lowest values recorded in that field for the month of August. The file climate_data_2017_numeric.csv contains the following fields: Date Minimum temperature (C) Maximum temperature (C) Rainfall (mm) Speed of maximum wind gust (km/h) 9am Temperature (C) 9am relative humidity (%) 3pm Temperature (C) 3pm relative humidity (%) Expected...
Write a JAVA program that prompts a famer's market associate for the price of each item...
Write a JAVA program that prompts a famer's market associate for the price of each item purchased by the customer and calculate their total. Use a do while loop to allow for the associate to enter as many items as necessary. Use 0 as a sentinel. If the customer spends at least $50, they receive a 15% discount. If the customer qualifies for the discount, output a statement informing them of this as well as their new total with the...
write this program in C++ Write a program that prompts a user for three characters. The...
write this program in C++ Write a program that prompts a user for three characters. The program must make sure that the input is a number 10 - 100 inclusive. The program must re prompt the user until a correct input is entered. Finally output the largest and the lowest value. Example 1: Input : 10 Input : 20 Input : 30 The largest is 30. The lowest is 10. Example 2: Input : 100 Input : 50 Input :...
Please Write C++ PROGRAM : That will write a program that initially prompts the user for...
Please Write C++ PROGRAM : That will write a program that initially prompts the user for a file name. If the file is not found, an error message is output, and the program terminates. Otherwise, the program prints each token in the file, and the number of times it appeared, in a well formatted manner. To accomplish all this, do the following: - Open the file - the user must be prompted and a file name input. DO NOT hardcode...
Write a C++ Program Write a program that prompts the user to input a string. The...
Write a C++ Program Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. You must insert the following comments at the beginning...
Question 1: Write a Java program that prompts the user to input a file name (existing...
Question 1: Write a Java program that prompts the user to input a file name (existing text file), then calculate and display the numbers of lines in that file. Also calculate and display the length of the longest line in that file. For example, if the input file has the following lines: Hello This is the longest line Bye The output should be: The file has 3 lines. The longest line is line 2 and it has 24 characters. Test...
all python Question One [2 * 2.5] Write a program that prompts the user for two...
all python Question One [2 * 2.5] Write a program that prompts the user for two integers and then prints •The sum •The difference •The product •The average •The distance (absolute value of the difference) •The maximum (the larger of the two) •The minimum (the smaller of the two) Hint: Python defines max and min functions that accept a sequence of values, each separated with a comma. Write a program that prompts the user for a measurement in meters and...
Write a program that prompts the user to input a string. The program then uses the...
Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. You must insert the following comments at the beginning of your program and...
Write a C++ program that prompts the user for the radius of a circle and then...
Write a C++ program that prompts the user for the radius of a circle and then calls inline function circleArea to calculate the area of that circle. It should do it repeatedly until the user enters -1. Use the constant value 3.14159 for π Sample: Enter the radius of your circle (-1 to end): 1 Area of circle with radius 1 is 3.14159 Enter the radius of your circle (-1 to end): 2 Area of circle with radius 2 is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT