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.”...
in java Write a Java Program that displays a menu with five different options: 1. Lab...
in java Write a Java Program that displays a menu with five different options: 1. Lab Test Average Calculator 2. Dice Roll 3. Circle Area Calculator 4. Compute Distance 5. Quit The program will display a menu with each of the options above, and then ask the user to enter their choice. There is also a fifth option to quit, in which case, the program will simply display a goodbye message. Based on the user’s choice, one of the options...
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...
For this assignment you will write a Java program using a loop that will play a...
For this assignment you will write a Java program using a loop that will play a simple Guess The Number game. Create a new project named GuessANumber and create a new Java class in that project named GuessANumber.java for this assignment. The program will randomly generate an integer between 1 and 200 (including both 1 and 200 as possible choices) and will enter a loop where it will prompt the user for a guess. If the user has guessed the...
• This lab, you will write a Java program to determine if a given Sudoku puzzle...
• This lab, you will write a Java program to determine if a given Sudoku puzzle is valid or not. • You determine if the puzzle is complete and valid, incomplete, or is invalid. • A puzzle is a 2-dimensional array 9x9 array. Each element contains the numbers 1 – 9. A space may also contain a 0 (zero), which means the spot is blank. • If you don’t know how a Sudoku Puzzle works, do some research, or download...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT