Question

In: Computer Science

Having issues starting an assignment. Still learning so If you don't mind could you explain public...

Having issues starting an assignment. Still learning so If you don't mind could you explain

public Theater()

Initialize the 2-D array “seats”, with 3 rows and 4 columns. To assign the price
for each seat, you need to open and read from the file “seatPrices.txt”. The file
contains 12 doubles representing the price for each row. All seats in a given
row are the same price, but different rows have different prices. You also need
to initialize “totalSeats” to 0.

public void displayChart()

This method is required to print out the seating chart with costs of the seats
with a tab between columns in the same row and a newline between rows. So
the initial seating chart would be printed:
12.5 12.5 12.5 12.5
10.0 10.0 10.0 10.0
8.0 8.0 8.0 8.0

----

import java.util.Scanner;
import java.io.*;

public class Assignment8
{   public static void main(String[] args)throws IOException
   {
       Theater t = new Theater();
       char command;
       Scanner keyboard = new Scanner(System.in);

      
      
      
       // print the menu
       printMenu();
       do
       {
           // ask a user to choose a command
           System.out.println("\nPlease enter a command or type ?");
           command = keyboard.next().toLowerCase().charAt(0);
           switch (command)
           {
               case 'a': // display remaining seats
                   System.out.println();
                   t.displayChart();
           }/*                   break;
/*               case 'b': // Print total from sales so far
                   System.out.println("\nTotal: " + t.getTotal());
                   break;
               case 'c': // sell ticket

                   if (t.soldOut())
                       System.out.println("\nSorry we are sold out.");
                   else
                   {
                       System.out.print("\nEnter the row you want: ");
                       int row = keyboard.nextInt();
                       System.out.print("Enter the column you want: ");
                       int col = keyboard.nextInt();
                       if (t.sellTicket(row, col))
                           System.out.println("\nEnjoy the show!");
                       else
                           System.out.println("\nThe seat is sold and/or is invalid seat!");
                   }
                   break;
               case 'd': // print number of seats sold
                   System.out.println("\nNumber of seats sold: " + t.numSold());
                   break;

               case '?': // display menu
                   printMenu();
                   break;
               case 'q':   // quit
                   break;
               default:
                   System.out.println("Invalid input!");

           }

*/       } while (command != 'q');

   } //end of the main method

   // this method prints out the menu to a user
   public static void printMenu()
   {
       System.out.print("\nCommand Options\n"
           + "-----------------------------------\n"
           + "a: print seating chart\n"
           + "b: display total sales\n"
           + "c: sell ticket\n"
           + "d: display number of seats sold\n"
           + "?: display the menu again\n"
           + "q: quit this program\n\n");
   } // end of the printMenu method

}
-----

package assignment8;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class Theater
{
//a two dimensional double array to hold the prices of the seats
//double to hold the total amount of sales generated from the theater.
   private double seats[][];
   double totalSales=0;
  
   String fileName="seatPrices.txt";
   String seatPrices="";
  
   static final int NUM_ROWS=3;
   static final int NUM_COLUMNS=4;
  
  
/*Initialize the 2-D array “seats”, with 3 rows and 4 columns
To assign the price for each seat, you need to open and read from the file “seatPrices.txt”. The file
contains 12 doubles representing the price for each row.
All seats in a given row are the same price, but different rows have different prices
*/
   public Theater()
   {
   Double seats[][]= new Double[NUM_ROWS][NUM_COLUMNS];
   int totalSeats=0;
   Double temp[][]=seats;
  
  
   try
{
// Instantiate a FileReader object to open the input file
// Note: "filename" should match the input file you made in Step 4
          
FileReader fr = new FileReader("seatPrices.txt");
//FileReader fr = new FileReader("/autograder/submission/" + fileName);
          
// BufferedReader is for efficient reading of characters
BufferedReader bfReader = new BufferedReader(fr);


// As we have determined the number of lines in our file, we will
// use constants to define the loop conditions

for (int r = 0; r {
for (int c= 0; c < NUM_COLUMNS; c++)
{
// Read a line from the file
// invoke .readLine() on the BufferedReader bfReader
// and save the returned value to the element at array position (i, j)
// -->
    seatPrices= bfReader.readLine();
    temp[r][c]=Double.valueOf(seatPrices);
      
   
   
}
seats=temp;

}
  
bfReader.close();

}
catch (FileNotFoundException e)
{
System.err.println("File not found");
}
catch (IOException e)
{
System.err.println("I/O error occurs");
}
        
   }

   public void displayChart()
   {   
  
   for (int r = 0; r {
for (int c= 0; c < NUM_COLUMNS; c++)
{
   System.out.print(seats[r][c]+ "\t");
}
System.out.println();
   }

   }
}

-------

seatPrices.txt

12.50
12.50
12.50
12.50
10.00
10.00
10.00
10.00
8.00
8.00
8.00
8.00

Solutions

Expert Solution

I have provided a detailed comment in the program for explaining the Theather() constructor, but if you still find it confusing please feel free to comment which part you're not getting I will surely try my best to explain

Program

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

class Theater {
   // a two-dimensional double array to hold the prices of the seats
   // double to hold the total amount of sales generated from the theater.
   private double seats[][];
   double totalSales = 0;

   String fileName = "seatPrices.txt";
   String seatPrices = "";

   static final int NUM_ROWS = 3;
   static final int NUM_COLUMNS = 4;

   /*
   * Initialize the 2-D array “seats”, with 3 rows and 4 columns To assign the
   * price for each seat, you need to open and read from the file
   * “seatPrices.txt”. The file contains 12 doubles representing the price for
   * each row. All seats in a given row are the same price, but different rows
   * have different prices
   */
   public Theater() {

       // initializing the 2d array seats
       // you were creating new seats[][] instead of using global seats[][]
      
       //Double seats[][] = new Double[NUM_ROWS][NUM_COLUMNS]; old code
       seats = new double[NUM_ROWS][NUM_COLUMNS];//modified code

       // i dont know the purpose of his variable
       // as it is not being used anywhere
       // maybe its created for future use
       int totalSeats = 0;

       // there is no need to create a temporary array
       // for putting price in the seats array
       // you can directly use seats array
       /* Double temp[][] = seats; */

       try {

           // Instantiate a FileReader object to open the input file
           // Note: "filename" should match the input file you made in Step 4

           // instantiating fileReader for reading the streams of characters in the file
           FileReader fr = new FileReader("data.txt");// change the name of file accodingly

           // FileReader fr = new FileReader("/autograder/submission/" + fileName);

           // BufferedReader is for efficient reading of characters
           BufferedReader bfReader = new BufferedReader(fr);

           for (int r = 0; r < NUM_ROWS; r++) {// for rows
               seatPrices = bfReader.readLine();// reading the data from the file
               for (int c = 0; c < NUM_COLUMNS; c++) {// for columns
                   // converting the data to double and
                   // then puuting it to the specific array position provied by the loop
                   seats[r][c] = Double.valueOf(seatPrices);

               }
               // seats = temp;

           }

           // closing the bufferedReader for releasing the resources
           bfReader.close();

           // for handling exceptions
       } catch (FileNotFoundException e) {
           System.err.println("File not found");
       } catch (IOException e) {
           System.err.println("I/O error occurs");
       }

   }

   public void displayChart() {
       for (int r = 0; r < NUM_ROWS; r++) {
           for (int c = 0; c < NUM_COLUMNS; c++) {
               System.out.print(seats[r][c] + "\t");
           }
           System.out.println();
       }

   }
}

public class Test {
   public static void main(String[] args) throws IOException {
       Theater t = new Theater();
       char command;
       Scanner keyboard = new Scanner(System.in);

       // print the menu
       printMenu();
       do {
           // ask a user to choose a command
           System.out.println("\nPlease enter a command or type ?");
           command = keyboard.next().toLowerCase().charAt(0);
           switch (command) {
           case 'a': // display remaining seats
               System.out.println();
               t.displayChart();
           }
           /*
           * break; /* case 'b': // Print total from sales so far
           * System.out.println("\nTotal: " + t.getTotal()); break; case 'c': // sell
           * ticket
           *
           * if (t.soldOut()) System.out.println("\nSorry we are sold out."); else {
           * System.out.print("\nEnter the row you want: "); int row = keyboard.nextInt();
           * System.out.print("Enter the column you want: "); int col =
           * keyboard.nextInt(); if (t.sellTicket(row, col))
           * System.out.println("\nEnjoy the show!"); else
           * System.out.println("\nThe seat is sold and/or is invalid seat!"); } break;
           * case 'd': // print number of seats sold
           * System.out.println("\nNumber of seats sold: " + t.numSold()); break;
           *
           * case '?': // display menu printMenu(); break; case 'q': // quit break;
           * default: System.out.println("Invalid input!");
           *
           * }
           *
           */ } while (command != 'q');

   } // end of the main method

   // this method prints out the menu to a user
   public static void printMenu() {
       System.out.print("\nCommand Options\n" + "-----------------------------------\n" + "a: print seating chart\n"
               + "b: display total sales\n" + "c: sell ticket\n" + "d: display number of seats sold\n"
               + "?: display the menu again\n" + "q: quit this program\n\n");
   } // end of the printMenu method

}

Output

Input


Related Solutions

Since you are just starting out and still learning how to farm, your costs are higher...
Since you are just starting out and still learning how to farm, your costs are higher than those of your competitors. To make matters worse, economists predict a low price for your crop this year (but prices might be much better next year). You will be producing at a loss. How do you determine whether to produce or not? Draw a graph as part of your answer
The term "Big Data" is used so often today, but some still don't have a basic...
The term "Big Data" is used so often today, but some still don't have a basic understanding of the term. This discussion aims to provide a simple definition of the term that you can use in everyday discussion. Once you've read this posting, share with us your thoughts about big data. Is it good? Is it evil? What potential benefits do you see? Is there any ethical responsibility that goes along with use of this data? Happy learning and Posting!...
Identify the field and explain how you would run your remote learning classroom, keep in mind...
Identify the field and explain how you would run your remote learning classroom, keep in mind you have students that have some disabilities, some students who don't understand english, some students who may not have technology. (a paragraph)
What issues do you anticipate attorneys having with the e-Discovery process in general, and how could...
What issues do you anticipate attorneys having with the e-Discovery process in general, and how could a digital forensics professional help relieve those issues?
Hey! I'm having trouble answering this for my assignment. Thank you so much in advance. 1)...
Hey! I'm having trouble answering this for my assignment. Thank you so much in advance. 1) Which type of vessels, arteries or veins, has more muscle fibers? What is the functional significance of this? 2a) In general, we have no conscious control over smooth muscle or cardiac muscle function, whereas we can consciously control to some extent all skeletal muscles. Can you consciously control your breathing? What does this tell you about the muscle type of the diaphragm? 2b) What...
Could you please explain step by step how to resolve this problem? Please don't send me...
Could you please explain step by step how to resolve this problem? Please don't send me the one that is already posted, I want a new explanation. Thank you. At the end of the month, the M&M Company had completed Job Red and Job Yellow. The individual job cost sheets reveal the following information: Job Direct Materials Direct Labor Machine Hours Job Red $6,720 $2,016 24 Job Yellow 12,966 4,104 76 ​ Job Red produced 144 units, and Job Yellow...
(I already have part A but still still include it anyways so you can use its...
(I already have part A but still still include it anyways so you can use its data to solve for parts B and C. Chemical energy is released or absorbed from reactions in various forms. The most easily measurable form of energy comes in the form of heat, or enthalpy. The enthalpy of a reaction can be calculated from the heats of formation of the substances involved in the reaction: ΔH∘rxn=ΔH∘f(products)−ΔH∘f(reactants) Entropy change, ΔS∘, is a measure of the number...
​​​​​​This is an assignment that I'm having trouble figuring out in python: Modify Node class so...
​​​​​​This is an assignment that I'm having trouble figuring out in python: Modify Node class so it has a field called frequency and initialize it as one. Add a search function. If a number is in the list, return its frequency. Modify Insert function. Remove push, append, and insertAfter as one function called insert(self, data). If you insert a data that is already in the list, simply increase this node’s frequency. If the number is not in the list, add...
Some policy-makers have raised concerns that having early starting times for schools hurts child learning outcomes...
Some policy-makers have raised concerns that having early starting times for schools hurts child learning outcomes and long-term development due to sleep deprivation. For the 2012-2013 school year, Los Angeles Unified School Districts decides to change the high school start time from 7:15am to 8:30am in 21 of its high schools, but leaves the 7:15am start time in the other 44 high schools. a) You want to construct a differences-in-differences estimate of the effect of this policy on child test...
Explain using the concept of dynamic increasing returns and learning curve why Switzerland still thrives in...
Explain using the concept of dynamic increasing returns and learning curve why Switzerland still thrives in watch manufacturing even though a southeast Asian economy like that of Thailand could produce them more competitively. Do the results of the Ricardian model hold in this case?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT