HELP WITH JAVA PROGRAM! My program: import java.util.*; public class Business{ p
ID: 662810 • Letter: H
Question
HELP WITH JAVA PROGRAM!
My program:
import java.util.*;
public class Business{
public static void main(String []args){
double[][] planeSeats = new double[20][6];
for(int i = 0; i < 20; i++){
for(int j = 0; j < 6; j++){
if(i > 4){
planeSeats[i][j] = 0.0;
}
else{
if(i <= 4 && j <= 3){
planeSeats[i][j] = 0.0;
}
}
}
}
for(int i = 0; i < 5; i++){
for(int j = 0; j < 4; j++){
System.out.print(" " + planeSeats[i][j]);
}
System.out.println("");
}
for(int i = 5; i < 15; i++){
for(int j = 0; j < 6; j++){
System.out.print(" " + planeSeats[i][j]);
}
System.out.println("");
}
int peanut = 0,snackbox = 0,softdrink = 0,cocktail = 0,beer = 0,wine = 0;
int peanut1 = 0,snackbox1 = 0,softdrink1 = 0,cocktail1 = 0,beer1 = 0,wine1 = 0;
while(true){
double total = 0.0;
Scanner sc = new Scanner(System.in);
System.out.println(" O)rder S)how Q)uit");
String input = sc.nextLine();
if(input.equalsIgnoreCase("O")){
System.out.println("What is your row number (1-20)");
int row = sc.nextInt();
System.out.println("What is your seat number(1-6)");
int seat = sc.nextInt();
System.out.println("Please choose a snack by number: 1) Peanuts(free) 2) Snack Box($5.95) 3) No thanks:");
int snack = sc.nextInt();
System.out.println("Please choose a drink by number: 1) Softdrink(free) 2) Cocktail($5.00) 3) Beer($4.00) 4)Wine($4.00) 5)No thanks:");
int drink = sc.nextInt();
if(snack == 1){
if(row < 5){
peanut++;
}
else{
peanut1++;
}
}
else if(snack == 2){
if(row < 5){
snackbox++;
}
else{
snackbox1++;
}
total = total + 5.95;
}
if(drink == 1){
if(row < 5){
softdrink++;
}
else{
softdrink1++;
}
}
else if(drink == 2){
if(row < 5){
cocktail++;
}
else{
cocktail1++;
}
total = total + 5.00;
}
else if(drink == 3){
if(row < 5){
beer++;
}
else{
beer1++;
}
total = total + 4.00;
}
else if(drink == 4){
if(row < 5){
wine++;
}
else{
wine1++;
}
total = total + 4.00;
}
planeSeats[row][seat] = total;
}
else if(input.equalsIgnoreCase("S")){
for(int i = 0; i < 5; i++){
for(int j = 0; j < 4; j++){
System.out.print(" " + planeSeats[i][j]);
}
System.out.println(" ");
}
for(int i = 5; i < 15; i++){
for(int j = 0; j < 6; j++){
System.out.print(" " + planeSeats[i][j]);
}
System.out.println(" ");
}
System.out.println("Total items ordered in first class is: ");
System.out.println("Peanuts: " + peanut);
System.out.println("Snack boxes: " + snackbox);
System.out.println("Soft drinks: " + softdrink);
System.out.println("Cocktail: " + cocktail);
System.out.println("Beer: " + beer);
System.out.println("Wine: " + wine);
System.out.println("Total items ordered in economy class is: ");
System.out.println("Peanuts: " + peanut1);
System.out.println("Snack boxes: " + snackbox1);
System.out.println("Soft drinks: " + softdrink1);
System.out.println("Cocktail: " + cocktail1);
System.out.println("Beer: " + beer1);
System.out.println("Wine: " + wine1);
}
else if(input.equalsIgnoreCase("Q")){
System.exit(0);
}
}
}
}
In an airplane, each passenger has a touch screen for ordering a drink and a snack. Some items are free and some are not. The system prepares two reports for speeding up service:
A list of how many drinks and snacks of each type must be prepared for the front and the rear of the plane.
Follow the design process that was described in this chapter to identify classes, and implement a program that simulates the system.
When I run...It puts the information in the wrong seat number
It is supposed to look like this:
A list of all seats, ordered by row, showing the charges that must be collected.A list of how many drinks and snacks of each type must be prepared for the front and the rear of the plane.
Follow the design process that was described in this chapter to identify classes, and implement a program that simulates the system.
When I run...It puts the information in the wrong seat number
It is supposed to look like this:
Explanation / Answer
The problem is at the line
planeSeats[row][seat] = total;
which should actually be
planeSeats[row-1][seat-1] = total;
because row and seat which you are entering are treated as indexes which is giving the incorrect output. Please replace the same and your program will work fine.
However there are few problems in your code which are as follows:-
1. If user will enter the row and seat number greater than 20 and 6 your program will give an exception.
2. If user will enter row number less than 6 and seat number greater than 4 your program won't display any message saying the seat doesn't exist.
3. If user will select economy seats, your program won't display them while displaying results because of the incorrect loop which is being used.
These are some of the problems which I noticed and hence written the code for solving each bug and exception which may or may not exist. Please refer the below code which will work fine for all scenarios and it will do perfect error handling also.
import java.util.*;
public class Business{
public static void main(String []args){
String[][] planeSeats = new String[20][6];
for(int i = 0; i < 20; i++){
for(int j = 0; j < 6; j++){
if(i < 5 && j > 3){
planeSeats[i][j] = "";
}
else{
planeSeats[i][j] = "0.0";
}
}
}
for(int i = 0; i < 20; i++){
for(int j = 0; j < 6; j++){
System.out.print(" " + planeSeats[i][j]);
}
System.out.println("");
}
int peanut = 0,snackbox = 0,softdrink = 0,cocktail = 0,beer = 0,wine = 0;
int peanut1 = 0,snackbox1 = 0,softdrink1 = 0,cocktail1 = 0,beer1 = 0,wine1 = 0;
Scanner sc = null;
while(true){
double total = 0.0;
int row = 0,seat = 0;
sc = new Scanner(System.in);
System.out.println(" O)rder S)how Q)uit");
String input = sc.nextLine();
if(input.equalsIgnoreCase("O")){
while(true){
if((row == 0 && seat == 0)){
System.out.println("What is your row number (1-20)");
row = sc.nextInt();
while(true){
if(row > 20){
System.out.println("You have entered the row number which is greater than 20. Please enter your row number again (1-20)");
row = sc.nextInt();
}
else{
break;
}
}
System.out.println("What is your seat number(1-6)");
seat = sc.nextInt();
while(true){
if(seat > 6){
System.out.println("You have entered the seat number which is greater than 6. Please enter your row number again (1-6)");
seat = sc.nextInt();
}
else{
break;
}
}
}
else if(row < 6 && seat > 4){
System.out.println("The row and seat which you entered doesn't exist. Please enter again");
System.out.println("What is your row number (1-20)");
row = sc.nextInt();
while(true){
if(row > 20){
System.out.println("You have entered the row number which is greater than 20. Please enter your row number again (1-20)");
row = sc.nextInt();
}
else{
break;
}
}
System.out.println("What is your seat number(1-6)");
seat = sc.nextInt();
while(true){
if(seat > 6){
System.out.println("You have entered the seat number which is greater than 6. Please enter your row number again (1-6)");
seat = sc.nextInt();
}
else{
break;
}
}
}
else{
break;
}
}
System.out.println("Please choose a snack by number: 1) Peanuts(free) 2) Snack Box($5.95) 3) No thanks:");
int snack = sc.nextInt();
System.out.println("Please choose a drink by number: 1) Softdrink(free) 2) Cocktail($5.00) 3) Beer($4.00) 4)Wine($4.00) 5)No thanks:");
int drink = sc.nextInt();
if(snack == 1){
if(row < 5){
peanut++;
}
else{
peanut1++;
}
}
else if(snack == 2){
if(row < 5){
snackbox++;
}
else{
snackbox1++;
}
total = total + 5.95;
}
if(drink == 1){
if(row < 5){
softdrink++;
}
else{
softdrink1++;
}
}
else if(drink == 2){
if(row < 5){
cocktail++;
}
else{
cocktail1++;
}
total = total + 5.00;
}
else if(drink == 3){
if(row < 5){
beer++;
}
else{
beer1++;
}
total = total + 4.00;
}
else if(drink == 4){
if(row < 5){
wine++;
}
else{
wine1++;
}
total = total + 4.00;
}
planeSeats[row-1][seat-1] = String.valueOf(total);
}
else if(input.equalsIgnoreCase("S")){
for(int i = 0; i < 20; i++){
for(int j = 0; j < 6; j++){
if(i < 5 && j > 3){
planeSeats[i][j] = "";
}
System.out.print(" " + planeSeats[i][j]);
}
System.out.println("");
}
System.out.println("Total items ordered in first class is: ");
System.out.println("Peanuts: " + peanut);
System.out.println("Snack boxes: " + snackbox);
System.out.println("Soft drinks: " + softdrink);
System.out.println("Cocktail: " + cocktail);
System.out.println("Beer: " + beer);
System.out.println("Wine: " + wine);
System.out.println("Total items ordered in economy class is: ");
System.out.println("Peanuts: " + peanut1);
System.out.println("Snack boxes: " + snackbox1);
System.out.println("Soft drinks: " + softdrink1);
System.out.println("Cocktail: " + cocktail1);
System.out.println("Beer: " + beer1);
System.out.println("Wine: " + wine1);
}
else if(input.equalsIgnoreCase("Q")){
System.exit(0);
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.