In: Computer Science
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
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