Question

In: Computer Science

Assignment Purpose The purpose of this lab is to write a well commented java program that...

Assignment Purpose

The purpose of this lab is to write a well commented java program that demonstrates the use of two dimensional arrays, input validation, and methods. (Write by Java Code, Need Comment)

Instructions

  1. A theater seating chart is implemented as a two-dimensional array of ticket prices, like this:

Seat Ticket Price

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 40 50 50 50 50 50 50 40 20

80 50 50 80 80 80 80 50 50 30

Seating Arrangement: Seats No.

1 2 3 4 5 6 7 8 9 10

11 12 13 14 15 16 17 18 19 20

21 22 23 24 25 26 27 28 29 30

31 32 33 34 35 36 37 38 39 40

41 42 43 44 45 46 47 48 49 50

51 52 53 54 55 56 57 58 59 60

61 62 63 64 65 66 67 68 69 70

71 72 73 74 75 76 77 78 79 80

81 82 83 84 85 86 87 88 89 90

  1. Write a program that prompts the user to pick a price. When a user specifies a price, make sure it is available. Mark sold seats by changing the price to 0. You will write at least 3 methods.
    1. One method that reads, validates and returns the price.
    2. The other that checks whether a seat is available that price.
    3. The third method prints a confirmation if the seat is available or prints a message saying “A seat at this price is not available. Good Bye.”

Hint: Basically, you will search the 2D array for the input price, and as soon as you find a matching price, you will replace the price at that location with a 0.

Sample output 1

Please pick a price or press Q to quit: 80

      Checking for the availability……

      Your seat is confirmed! Your seat number is 81. Enjoy your movie

Please pick a price or press Q to quit: 80

      Checking for the availability……

      No seat at this price is available. Sorry!

Please pick a price or press Q to quit: Q
     Thank you for using our online ticketing service. Goodbye!

Sample output 2

Please pick a price or press Q to quit: 10

      Checking for the availability……

      Your seat is confirmed! Your seat number is 1. Enjoy your movie

Please pick a price or press Q to quit: 20

      Checking for the availability……

      Your seat is confirmed! Your seat number is 33. Enjoy your movie

Please pick a price or press Q to quit: 20

      Checking for the availability……

      Your seat is confirmed! Your seat number is 34. Enjoy your movie

      Please pick a price or press Q to quit: 200

      Please pick a valid price. Valid prices are $10, $20, $30, $40, $50, and $80

Please pick a price or press Q to quit: Q

Thank you for using our online ticketing service. Goodbye!

Solutions

Expert Solution

Code to copy along with screenshots of code and output are provided.
Please refer to screenshots to understand the indentation of code.
If you have any doubts or issues. Feel free to ask in comments
Please give this answer a like, or upvote. This will be very helpful for me.
================================================================

Screenshots of Code :

Screenshots of Output :

Code to copy:

import java.util.Scanner;


public class Program {
  
  
   // declaring a 2d array to store prices of seats
   static int[][] seatPrices=
       {
          
           {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, 40, 50, 50, 50, 50, 50, 50, 40, 20},
           {80, 50, 50, 80, 80, 80, 80, 50, 50, 30}
  
   };

   // function to read price, validate and return price
   static int readPrice()
   {
      
       // decalring string to store choice
       String choice;
       // creating scanner object to take input from user
       Scanner sc = new Scanner(System.in);
       System.out.print("Please pick a price or press Q to quit: ");
       // taking input
       choice = sc.next();
      
       if(choice.equals("q") || choice.equals("Q"))
       {
           // if user enters 'q' or 'Q'
           System.out.println("Thank you for using our online ticketing service. Goodbye!");
           // return -1
           return -1;
       }
       else if(choice.equals("10") || choice.equals("20") || choice.equals("30") || choice.equals("50") || choice.equals("80") )
       {
           // if user enters valid price, then return as Integer
           return Integer.parseInt(choice);
          
       }
       else
       {
       // in case of invalid input
           System.out.println("Please pick a valid price. Valid prices are $10, $20, $30, $50, and $80 ");
           // return 0
           return 0;
       }
  
  
   }
  
   // function to check availability of seat
   static int seatAvailablity(int price)
   {
      
      
       System.out.println("Checking for the availability……");
      
       // iterating through 2d array using for loop
       for(int i = 0;i<9; i++)
       {
           for(int j = 0;j<10;j++)
           {
               if(price == seatPrices[i][j])
               {
               // if seat found then set value of that seat to 0
                   seatPrices[i][j] = 0;
                   // and return seat number
                   return (i*10)+(j+1)   ;
               }
           }
       }
       // otherwise return -1 (if no seat found)
       return -1;
   }
  
   // function to print confirmation of seat
   static void printConfirmation(int seatNumber)
   {
       if(seatNumber == -1)
       {
           // if seatNumber is -1
           System.out.println("No seat at this price is available. Sorry");
       }
       else
       {
           // otherwise print confirmation of seat
           System.out.println("Your seat is confirmed! Your seat number is "+ seatNumber + ". Enjoy your movie");
       }
   }
  
  
  
   // main method
public static void main(String args[])
{
// integer variable to hold price and seat number
   int price;
   int seatNumber;
  
   // infinite while loop
   while(true)
   {
      // calling readPrice() function and storing price into price variable
       price = readPrice();
     
   if(price == -1)
   {
       // if -1 , means user entered q , so break
       break;
   }
   else if(price == 0)
   {
       // price = 0 , means user entered invalid choice, do nothing
   }
   else
   {
       // else call seatAvailibilty and store seatNumber to seatNumber varaible
       seatNumber = seatAvailablity(price);
       // call prinConfirmation
       printConfirmation(seatNumber);
     
   }
      
   }
     
  
  
}
}

==================XXXXX=====================================



Related Solutions

Assignment Purpose The purpose of this lab is to write a well commented java program that...
Assignment Purpose The purpose of this lab is to write a well commented java program that demonstrates the use of one dimensional arrays and methods.(Need Comment, Write by Java Code) Instructions Write a method rotateArray that is passed to an array, x, of integers (minimum 7 numbers) and an integer rotation count, n. x is an array filled with randomly generated integers between 1 and 100. The method creates a new array with the items of x moved forward by...
Assignment Purpose The purpose of this lab is to write a well commented java program that...
Assignment Purpose The purpose of this lab is to write a well commented java program that demonstrates the use and re-use of methods with input validation. Instructions It is quite interesting that most of us are likely to be able to read and comprehend words, even if the alphabets of these words are scrambled (two of them) given the fact that the first and last alphabets remain the same. For example, “I dn'ot gvie a dman for a man taht...
Assignment Purpose The purpose of this lab is to write a well commented java program that...
Assignment Purpose The purpose of this lab is to write a well commented java program that demonstrates the use of one dimensional arrays and methods. Instructions Write a method rotateArray that is passed to an array, x, of integers (minimum 7 numbers) and an integer rotation count, n. x is an array filled with randomly generated integers between 1 and 100. The method creates a new array with the items of x moved forward by n Elements that are rotated...
Assignment Purpose The purpose of this lab is to write a well commented java program that...
Assignment Purpose The purpose of this lab is to write a well commented java program that demonstrates the use of one dimensional arrays and methods. Instructions Write a method rotateArray that is passed to an array, x, of integers (minimum 7 numbers) and an integer rotation count, n. x is an array filled with randomly generated integers between 1 and 100. The method creates a new array with the items of x moved forward by n Elements that are rotated...
Assignment Purpose The purpose of this lab is to write a well-commented java program that demonstrates...
Assignment Purpose The purpose of this lab is to write a well-commented java program that demonstrates the use of loops, and generation of random integers. Instructions You are taking some time off from your paint business and currently are on vacation in Bahamas. You decide to write a Java program that generates 10 random numbers between 1 and 20 (all integers). You cannot use arrays (even if you know what they are) to store these numbers. It then picks up...
Assignment Purpose Write a well commented java program that demonstrates the use and re-use of methods...
Assignment Purpose Write a well commented java program that demonstrates the use and re-use of methods with input validation. Instructions It is quite interesting that most of us are likely to be able to read and comprehend words, even if the alphabets of these words are scrambled (two of them) given the fact that the first and last alphabets remain the same. For example, “I dn'ot gvie a dman for a man taht can olny sepll a wrod one way.”...
Write a well-commented java program that demonstrates the use of loops, and generation of random integers...
Write a well-commented java program that demonstrates the use of loops, and generation of random integers (Code will be written in Dr. Java) You are taking some time off from your paint business and currently are on vacation in Bahamas. You decide to write a Java program that generates 10 random numbers between 1 and 20 (all integers). You cannot use arrays (even if you know what they are) to store these numbers. It then picks up the largest number...
***This is done with Java programming*** Write a well-documented (commented) program, “ISBN,” that takes a 9-digit...
***This is done with Java programming*** Write a well-documented (commented) program, “ISBN,” that takes a 9-digit integer as a command-line argument, computes the checksum, and prints the ISBN number. You should use Java’s String data type to implement it. The International Standard Book Number (ISBN) is a 10-digit code that uniquely specifies a book. The rightmost digit is a checksum digit that can be uniquely determined from the other 9 digits, from the condition that d1 + 2d2 +3d3 +...
Lab Assignment Write a Java program that implements a queue in a hospital. I want your...
Lab Assignment Write a Java program that implements a queue in a hospital. I want your program to ask the user to enter the number of patients then enter the patient number starting from 110 till the end of the queue then print number of patients waiting in the queue. Suppose you have a queue D containing the numbers (1,2,3,4,5,6,7,8), in this order. Suppose further that you have an initially empty Stack S. Give a code fragment that uses S,...
Program in Java using Inheritence The purpose of this assignment is to practice OOP programming covering...
Program in Java using Inheritence The purpose of this assignment is to practice OOP programming covering Inheritance. Core Level Requirements (up to 6 marks) The scenario for this assignment is to design an online shopping system for a local supermarket (e.g., Europa Foods Supermarket or Wang Long Oriental Supermarket). The assignment is mostly concentrated on the product registration system. Design and draw a UML diagram, and write the code for the following classes: The first product category is a fresh...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT