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...
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...
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...
Write a program in C that prompts the user for a number of seconds and then...
Write a program in C that prompts the user for a number of seconds and then converts it to h:m:s format. Example: 5000 seconds should display as 1:23:20 (1 hour, 23 minutes, 20 seconds.) Test with several values between about 100 seconds and 10,000 seconds. use unint and remainders for this and keep it as simple as possible.
C++ Program: Write a program that prompts the user for two numbers and stores them in...
C++ Program: Write a program that prompts the user for two numbers and stores them in signed integers. The program should then add those two numbers together and store the result in a signed integer and display the result. Your program should then multiply them by each other and store the result in another integer and display the result. Then do the same but with dividing the first number by the second. Display an error message to the screen if...
Write a Java program that prompts the user to input a word (String). The program must...
Write a Java program that prompts the user to input a word (String). The program must print the reversed word with all consecutive duplicate characters removed. The program must contain the following classes: - The StackX class (you can use the Java Stack class). - The Reverse class which must contain a private data field called “word” of type string, a constructor, and a void method called revNoDup(). The revNoDup() method must reverse the word and remove the consecutive duplicate...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT