Java

Lydia99
Fall2019Lab4.pdf

CS1341 - Lab 4 Programming Assignment

Arrays, Strings, more practice with multiple methods

Assignment Due Saturday, October 26th, 2019 at 6:00 AM Uploaded to Canvas

This assignment will require a Pre-Lab and a Lab.

GRADING: Comment your program to explain your steps. Each program should compile without errors

and should run to produce outputs described. The following points will be discounted if the related

element is missing or incorrect:

• Output formatting monetary amounts (dollar sign and 2 places after the decimal point) [2

points each]

• Proper names for classes, variables, and methods [1 point each]

• No Comments [5 points]

• Program doesn't compile [5 points for each minor error up to 5 errors provided that after

fixing the errors the program compiles. If the program does not compiler after the 5 errors

are fixed, partial credit will be given not to exceed 50 points]

• Source code (java file) missing [ 20 points]

• Executable (class file) missing [20 points]

• Both java file and class file missing [100 points]

• Missing method where a method is required [5 points each]

• Missing loop where loop is required [5 points each]

• Missing array where array is required [10 points each]

• Logical errors in calculation [5 points each]

Submit the java and class files via Canvas (as a single zip-file). Include a comment block at the top of

each Java file that includes your name, student id number, and “Lab 4-Fall 2019”.

Pre-Lab (10 POINTS)

Write a Java program called VolumeOfCones which does the following:

In the main method:

1. Use a for loop which repeats five times. Within the loop,

a. prompt the user to enter the radius of the cone and store the radius in a double array

called radiusArray

b. prompt the user to enter the corresponding height of the cone and store the height in a double array called heightArray

2. Call the method getVolumeArray with the following header and body:

public static double[] getVolumeArray(double[] radiusArray, double[]

heightArray)

a. Within a for loop, calculate the volume of each cone using the values stored in the radiusArray and heightArray, and store the volume in a double array called

volumeArray. Hint:

b. Return the volumeArray at the end of the method. 3. Use a for loop to display the volume of each of the five cones.

Bring the working VolumeOfCones.java and VolumeOfCones.class files to your lab session for pre-lab credit.

Sample output (user input is highlighted in yellow) Enter the radius of cone 1: 3.0

Enter the height of cone 1: 3.5

Enter the radius of cone 2: 10.0

Enter the height of cone 2: 5.0

Enter the radius of cone 3: 5.0

Enter the height of cone 3: 10.0

Enter the radius of cone 4: 25.5

Enter the height of cone 4: 25.5

Enter the radius of cone 5: 100.25

Enter the height of cone 5: 600.5

The volume of cone 1 with radius 3.0000 and height 3.5000 is: 32.9867

The volume of cone 2 with radius 10.0000 and height 5.0000 is: 523.5988

The volume of cone 3 with radius 5.0000 and height 10.0000 is: 261.7994

The volume of cone 4 with radius 25.5000 and height 25.5000 is: 17363.9753

The volume of cone 5 with radius 100.2500 and height 600.5000 is: 6319902.7040

Lab (90 POINTS)

Write a Java program called AirplaneSeatReservation which allows a user to reserve seats given a seating

chart. The program will update and display the seating chart as the user selects seats, displays subtotals

for the reservation, and finally displays the receipt including taxes when the user is done with all the seat

selection. You must use arrays, loops, and methods in your solution as described below.

main() method

1. Create 6 arrays which contain the seats (String arrays) and their prices (double arrays) as

follows:

rowOneSeats

“1A” “1B” “1C” rowTwoSeats

“2A” “2B” “2C”

rowThreeSeats

“3A” “3B” “3C”

rowOnePrices

35.00 15.00 45.00

rowTwoPrices

30.00 12.00 40.00

rowThreePrices

25.00 10.00 35.00

2. Create a total variable of type double which will keep track of the total so far

3. Print a welcome message: “Reserve seats for your upcoming flight”

4. Call the display() method to display the airplane seating chart

5. Ask the user how many seats they want to reserve and store the value in an int variable called

numSeats

6. Then, in a for loop which will iterate numSeats times, do the following:

a. Call the getRow() method to return the row number from which the user wants to

reserve a seat

b. Check if the row is 1, 2, or 3, and call the makeReservation() method passing it the

correct row and prices arrays. The method should return the price of the seat after the

reservation is made which will be added to the total variable.

c. Display the total so far.

7. Lastly, when the loop is done, call the display() method followed by a call to the

printReceipt() method to print the receipt

display() method

1. Method header

public static void display(String[] rowOneSeats, String[]

rowTwoSeats, String[] rowThreeSeats, double[] rowOnePrices,

double[] rowTwoPrices, double[] rowThreePrices)

2. Display the first part of the plane (highlighted in green) then call the method

printRowSeats() followed by printRowPrices() for all the rows and their prices.

Include the separating lines (highlighted in blue) between each set of method calls.

SEATING CHART

-----------------

/ \

/ \

/ \

| |

| |

| |

| 1A 1B 1C |

|$35.00 $15.00 $45.00 |

| |

| 2A 2B 2C |

|$30.00 $12.00 $40.00 |

| |

| 3A 3B 3C |

|$25.00 $10.00 $35.00 |

| |

printRowSeats() and printRowPrices() methods

1. Method headers

public static void printRowSeats(String[] rowSeats)

public static void printSectionPrices(double[] rowPrices)

2. Display the rows and prices, respectively, using a for loop in each method. Here’s the first row and

its prices as an example:

| 1A 1B 1C |

|$35.00 $15.00 $45.00 |

getRow() method

1. Method header

public static int getRow()

2. Ask the user which row they want to choose from and return the number representing the row.

The user will type their choice as a String. Use the next() method from the Scanner class

to read the user input and store it in a String variable. Then determine the number equivalent

to the String. Note: The user may enter their choice using a combination of upper case and lower

case letters. Make sure you handle this appropriately in your code.

makeReservation() method

1. Method header

public static double makeReservation(String[] rowSeats, double[]

prices, int row)

2. Display the row and prices for the selected row by calling printRowSeats() and

printRowPrices()

3. Ask the user which seat they want and store the user’s response in a String variable called

seatLetter

4. Create an int variable called seatNum and assign it a value as follows:

a. If the user enters an A, seatNum should be set to 0

b. If the user enters a B, seatNum should be set to 1

c. If the user enters a C, seatNum should be set to 2

Note: The user may enter their choice using a upper case and lower case letters. Make sure you

handle this appropriately in your code.

5. Display the user’s selection (row + seat letter. Ex: 1A) and the price. Hint: display the seat letter

as an upper case letter regardless of user input as follows: seatLetter.toUpperCase()

6. Call updateSeatingChart() method to display an “X” in place of the selected seat

7. Print the updated row with prices by calling printRowSeats() and printRowPrices()

8. Return the price of the selected seat using seatNum as the index

updateSeatingChart() method

1. Method header

public static void updateSeatingChart(String[] rowSeats, int

seatNum)

2. Set the correct seat in the array to “X”

printReceipt() method

1. Method header

public static void printReceipt(int numSeats, double total)

2. Calculate taxes and total including taxes

3. Print the receipt as follows:

Here's your receipt:

~~~~~~~~~~~~~~~~~~~~

Subtotal: $160.00

Taxes: $19.38

====================

Total: $173.20

~~~~~~~~~~~~~~~~~~~~

Here’s a complete run of the program. Sample Output (user input is highlighted in yellow):

Reserve seats for your upcoming flight

SEATING CHART

-----------------

/ \

/ \

/ \

| |

| |

| |

| 1A 1B 1C |

|$35.00 $15.00 $45.00 |

| |

| 2A 2B 2C |

|$30.00 $12.00 $40.00 |

| |

| 3A 3B 3C |

|$25.00 $10.00 $35.00 |

| |

How many seats do you want to reserve? 5

Enter the row for the seat you want to reserve: (One/Two/Three) one

=======================

| 1A 1B 1C |

|$35.00 $15.00 $45.00 |

=======================

Which seat do you want? (A/B/C) a

The seat you selected is: 1A

The price of the seat is: $35.00

Updated row chart:

=======================

| X 1B 1C |

|$35.00 $15.00 $45.00 |

=======================

Your subtotal is: $35.00

-----

Enter the row for the seat you want to reserve: (One/Two/Three) two

=======================

| 2A 2B 2C |

|$30.00 $12.00 $40.00 |

=======================

Which seat do you want? (A/B/C) b

The seat you selected is: 2B

The price of the seat is: $12.00

Updated row chart:

=======================

| 2A X 2C |

|$30.00 $12.00 $40.00 |

=======================

Your subtotal is: $47.00

-----

Enter the row for the seat you want to reserve: (One/Two/Three) Two

=======================

| 2A X 2C |

|$30.00 $12.00 $40.00 |

=======================

Which seat do you want? (A/B/C) C

The seat you selected is: 2C

The price of the seat is: $40.00

Updated row chart:

=======================

| 2A X X |

|$30.00 $12.00 $40.00 |

=======================

Your subtotal is: $87.00

-----

Enter the row for the seat you want to reserve: (One/Two/Three) thREE

=======================

| 3A 3B 3C |

|$25.00 $10.00 $35.00 |

=======================

Which seat do you want? (A/B/C) c

The seat you selected is: 3C

The price of the seat is: $35.00

Updated row chart:

=======================

| 3A 3B X |

|$25.00 $10.00 $35.00 |

=======================

Your subtotal is: $122.00

-----

Enter the row for the seat you want to reserve: (One/Two/Three) ONE

=======================

| X 1B 1C |

|$35.00 $15.00 $45.00 |

=======================

Which seat do you want? (A/B/C) B

The seat you selected is: 1B

The price of the seat is: $15.00

Updated row chart:

=======================

| X X 1C |

|$35.00 $15.00 $45.00 |

=======================

Your subtotal is: $137.00

-----

SEATING CHART

-----------------

/ \

/ \

/ \

| |

| |

| |

| X X 1C |

|$35.00 $15.00 $45.00 |

| |

| 2A X X |

|$30.00 $12.00 $40.00 |

| |

| 3A 3B X |

|$25.00 $10.00 $35.00 |

| |

Here's your receipt:

~~~~~~~~~~~~~~~~~~~~

Subtotal: $137.00

Taxes: $11.30

====================

Total: $148.30

~~~~~~~~~~~~~~~~~~~~