Question

The school needs a program to organize where the musicians will stand when they play at...

The school needs a program to organize where the musicians will stand when they play at away games. Each away stadium is different, so when they arrive the conductor gets the following information from the local organizer:

  • The number of rows they have to stand on. The maximum number of rows is 10. The rows are labelled with capital letters, 'A', 'B', 'C', etc.
  • For each row, the number of positions in the row. The maximum number of positions is 8. The positions are numbered with integers, 1, 2, 3, etc.

The conductor then starts assigning people to positions, but is constrained by weight limits: Musicians, fully clothed and holding their instruments, weigh from 45kg to 200kg, and the total weight of a row may not exceed 100kg per position (e.g., a row with 5 positions may not have more than 500kg of musicians on it). The conductor wants a program that allows musicians to be added and removed from positions, while ensuring the constraints are all met. At any stage the conductor wants to be able to see the current assignment - the weight in each position (0kg for vacant positions) and the total & average weight for each row.

The program must be menu driven, with options to:

  • Add a musician (by weight) to a vacant position.
  • Remove a musician from an occupied position.
  • Print the current assignment.
  • Exit (so the musicians can start playing)

The program must be reasonably idiot proof:

  • Menu options must be accepted in upper and lower case.
  • Row letters must be accepted in upper and lower case.
  • All input must be checked to be in range, and if not the user must be asked to input again.
  • You may assume that numeric input will be syntactically correct.

Here's what a sample run should look like (with the keyboard input shown in italics) ...

Welcome to the Band of the Hour
-------------------------------
Please enter number of rows               : 11
ERROR: Out of range, try again            : 3
Please enter number of positions in row A : -4
ERROR: Out of range, try again            : 3
Please enter number of positions in row B : 4
Please enter number of positions in row C : 5

(A)dd, (R)emove, (P)rint,          e(X)it : p

A:   0.0   0.0   0.0                                [    0.0,     0.0]
B:   0.0   0.0   0.0   0.0                          [    0.0,     0.0]
C:   0.0   0.0   0.0   0.0   0.0                    [    0.0,     0.0]

(A)dd, (R)emove, (P)rint,          e(X)it : a
Please enter row letter                   : Q
ERROR: Out of range, try again            : B
Please enter position number (1 to 4)     : 6
ERROR: Out of range, try again            : 1
Please enter weight (45.0 to 200.0)       : 22
ERROR: Out of range, try again            : 222
ERROR: Out of range, try again            : 180
****** Musician added.

(A)dd, (R)emove, (P)rint,          e(X)it : p

A:   0.0   0.0   0.0                                [    0.0,     0.0]
B: 180.0   0.0   0.0   0.0                          [  180.0,    45.0]
C:   0.0   0.0   0.0   0.0   0.0                    [    0.0,     0.0]

(A)dd, (R)emove, (P)rint,          e(X)it : A
Please enter row letter                   : b
Please enter position number (1 to 4)     : 1
ERROR: There is already a musician there.

(A)dd, (R)emove, (P)rint,          e(X)it : A
Please enter row letter                   : B
Please enter position number (1 to 4)     : 2
Please enter weight (45.0 to 200.0)       : 150
****** Musician added.

(A)dd, (R)emove, (P)rint,          e(X)it : p

A:   0.0   0.0   0.0                                [    0.0,     0.0]
B: 180.0 150.0   0.0   0.0                          [  330.0,    82.5]
C:   0.0   0.0   0.0   0.0   0.0                    [    0.0,     0.0]

(A)dd, (R)emove, (P)rint,          e(X)it : a
Please enter row letter                   : b
Please enter position number (1 to 4)     : 3
Please enter weight (45.0 to 200.0)       : 175
ERROR: That would exceed the average weight limit.

(A)dd, (R)emove, (P)rint,          e(X)it : a
Please enter row letter                   : b
Please enter position number (1 to 4)     : 4
Please enter weight (45.0 to 200.0)       : 55
****** Musician added.

(A)dd, (R)emove, (P)rint,          e(X)it : p

A:   0.0   0.0   0.0                                [    0.0,     0.0]
B: 180.0 150.0   0.0  55.0                          [  385.0,    96.3]
C:   0.0   0.0   0.0   0.0   0.0                    [    0.0,     0.0]

(A)dd, (R)emove, (P)rint,          e(X)it : r
Please enter row letter                   : b
Please enter position number (1 to 4)     : 3
ERROR: That position is vacant.

(A)dd, (R)emove, (P)rint,          e(X)it : r
Please enter row letter                   : b
Please enter position number (1 to 4)     : 1
****** Musician removed.

(A)dd, (R)emove, (P)rint,          e(X)it : p

A:   0.0   0.0   0.0                                [    0.0,     0.0]
B:   0.0 150.0   0.0  55.0                          [  205.0,    51.3]
C:   0.0   0.0   0.0   0.0   0.0                    [    0.0,     0.0]

(A)dd, (R)emove, (P)rint,          e(X)it : q
ERROR: Invalid option, try again          : P

A:   0.0   0.0   0.0                                [    0.0,     0.0]
B:   0.0 150.0   0.0  55.0                          [  205.0,    51.3]
C:   0.0   0.0   0.0   0.0   0.0                    [    0.0,     0.0]

(A)dd, (R)emove, (P)rint,          e(X)it : X

This is what I have so far...

import java.util.Scanner;

//=============================================================================

public class BandOfTheHour {

//-----------------------------------------------------------------------------

private static Scanner keyboard = new Scanner(System.in);

private static final int MAX_NUMBER_ROWS = 10;

private static final int MAX_NUMBER_POS = 8;

//-----------------------------------------------------------------------------

public static void main(String[] args) {

  

  

char[] rowLabel = {'A','B','C','D','E','F','G','H','I','J'};

int[] posNumber = {1,2,3,4,5,6,7,8};

int numberOfRows;

int numberOfPos=0;

int [][]bandRows;

  

//----Get data

System.out.println("Welcome to the Band of the hour");

System.out.println("-------------------------------");

System.out.print("Please enter number of rows:");

numberOfRows = keyboard.nextInt();

while(numberOfRows > MAX_NUMBER_ROWS){

System.out.print ("ERROR: Out of range, try again:");

numberOfRows = keyboard.nextInt();

}

bandRows = new int [numberOfRows][];

  

for (int i=0; i<numberOfRows; i++){

do{

System.out.println("Please enter number of positions in row " + rowLabel[i] + " :");

numberOfPos = keyboard.nextInt();

if (numberOfPos <=0 || numberOfPos > MAX_NUMBER_POS){

System.out.print ("ERROR: Out of range, try again:");

}

}

while

(numberOfPos <=0 || numberOfPos > MAX_NUMBER_POS);

   bandRows[i]= new int [numberOfPos];

}

System.out.println("(A)add, (R)emove, (P)rint, e(X)it: “);

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//BandOfTheHour.java

import java.util.Scanner;

public class BandOfTheHour {

      

       private static Scanner keyboard = new Scanner(System.in);

       private static final int MAX_NUMBER_ROWS = 10;

       private static final int MAX_NUMBER_POS = 8;

       public static void display(double bandRows[][])

       {

             char row = 'A';

             for(int i=0;i<bandRows.length;i++)

             {

                    System.out.print(row+":\t");

                    double sum = 0;

                   

                    for(int j=0;j<bandRows[i].length;j++)

                    {

                           System.out.printf("%.1f ",bandRows[i][j]);

                           sum += bandRows[i][j];    

                    }

                   

                    System.out.printf("\t[ %.1f, %.1f]\n",sum, sum/bandRows[i].length);

                    row++;

             }

       }

      

       public static double getTotalWeight(double bandRows[][], int row)

       {

             double sum = 0;

             for(int i=0;i<bandRows[row].length;i++)

                    sum += bandRows[row][i];

             return sum;

       }

      

       public static void main(String[] args) {

            

             int numberOfRows;

             int numberOfPos=0;

             String choice;

             String rowLetter;

             int pos;

             double weight;

             double [][]bandRows;

             String validRows = "";

             int index;

             //----Get data

             System.out.println("Welcome to the Band of the hour");

             System.out.println("-------------------------------");

             System.out.print("Please enter number of rows:");

             numberOfRows = keyboard.nextInt();

             while(numberOfRows > MAX_NUMBER_ROWS){

                    System.out.print ("ERROR: Out of range, try again: ");

                    numberOfRows = keyboard.nextInt();

             }

            

             keyboard.nextLine();

             bandRows = new double [numberOfRows][];

            

             char row = 'A';

             for(int i=0;i<numberOfRows;i++)

             {

                    validRows += row+"";

                    row++;

             }

            

            

             for (int i=0; i<numberOfRows; i++){

                    System.out.print("Please enter number of positions in row " + validRows.charAt(i) + " :");

                    numberOfPos = keyboard.nextInt();

                    while(numberOfPos <=0 || numberOfPos > MAX_NUMBER_POS){

                           System.out.print ("ERROR: Out of range, try again:");

                           numberOfPos = keyboard.nextInt();

                    }

                    keyboard.nextLine();

                    bandRows[i]= new double [numberOfPos];

                    row++;

             }

            

            

             for(int i=0;i<bandRows.length;i++)

             {

                    for(int j=0;j<bandRows[i].length;j++)

                           bandRows[i][j] = 0;

             }

            

            

             do {

                    System.out.print("\n(A)add, (R)emove, (P)rint, e(X)it: ");

                    choice = keyboard.nextLine();

                    while((!choice.equalsIgnoreCase("a")) && (!choice.equalsIgnoreCase("r")) && (!choice.equalsIgnoreCase("p")) && (!choice.equalsIgnoreCase("x")))

                    {

                           System.out.print("ERROR: Invalid option, try again : ");

                           choice = keyboard.nextLine();

                    }

                    switch(choice.toUpperCase())

                    {

                    case "A":

                                 System.out.print("Please enter row letter : ");

                                 rowLetter= keyboard.nextLine();

                                 index = validRows.indexOf(rowLetter.toUpperCase());

                                 while(rowLetter.length() > 1 || index == -1)

                                 {

                                        System.out.print("ERROR: Out of range, try again : ");

                                        rowLetter= keyboard.nextLine();

                                        index = validRows.indexOf(rowLetter.toUpperCase());

                                 }

                                

                                 System.out.print("Please enter position number (1 to "+bandRows[index].length+") : ");

                                 pos = keyboard.nextInt();

                                 while(pos < 1 || pos > bandRows[index].length)

                                 {

                                        System.out.print("ERROR: Out of range, try again : ");

                                        pos = keyboard.nextInt();

                                 }

                                 keyboard.nextLine();

                                 if(bandRows[index][pos-1] != 0)

                                 {

                                        System.out.println("ERROR: There is already a musician there.");

                                        break;

                                 }

                                

                                 System.out.print("Please enter weight (45.0 to 200.0) : ");

                                 weight = keyboard.nextDouble();

                                 boolean weightLimitExceeded = false;

                                 while( (weight < 45) || (weight > 200) || ((getTotalWeight(bandRows,index) + weight) > (bandRows[index].length*100)))

                                 {

                                        if((weight < 45) || (weight > 200))

                                        {

                                               System.out.print("ERROR: Out of range, try again : ");

                                               weight = keyboard.nextDouble();

                                        }

                                        else

                                        {

                                               System.out.println("ERROR: That would exceed the average weight limit.");

                                               weightLimitExceeded = true;

                                               break;

                                        }

                                 }

                                 keyboard.nextLine();

                                 if(!weightLimitExceeded)

                                 {

                                        bandRows[index][pos-1] = weight;

                                        System.out.println("****** Musician added.");

                                 }

                                 break;

                                

                    case "R":

                           System.out.print("Please enter row letter : ");

                           rowLetter= keyboard.nextLine();

                           index = validRows.indexOf(rowLetter.toUpperCase());

                           while(rowLetter.length() > 1 || index == -1)

                           {

                                 System.out.print("ERROR: Out of range, try again : ");

                                 rowLetter= keyboard.nextLine();

                                 index = validRows.indexOf(rowLetter.toUpperCase());

                           }

                          

                           System.out.print("Please enter position number (1 to "+bandRows[index].length+") : ");

                           pos = keyboard.nextInt();

                           while(pos < 1 || pos > bandRows[index].length)

                           {

                                 System.out.print("ERROR: Out of range, try again : ");

                                 pos = keyboard.nextInt();

                           }

                           keyboard.nextLine();

                           if(bandRows[index][pos-1] != 0)

                           {

                                 bandRows[index][pos-1] = 0;

                                 System.out.println("****** Musician removed.");

                           }else

                                 System.out.println("ERROR: That position is vacant.");

                           break;

                          

                    case "P":

                           display(bandRows);

                           break;

                          

                    case "X":

                           break;

                                

                    }

             }while(!choice.equalsIgnoreCase("X"));

       }

}

//end of BandOfTheHour.java

Output:

Add a comment
Know the answer?
Add Answer to:
The school needs a program to organize where the musicians will stand when they play at...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Introduction Write a MIPS program to allow a user to play a simple variation of the...

    Introduction Write a MIPS program to allow a user to play a simple variation of the game BINGO. Your program should display the following game board and the player wants to choose a location to mark BINGO A position can be marked by entering its column letter (eit B', 'I', 'N' 'G', or CO and row number (either 1, 2, 3, 4, or 5). An 'X' should mark the positions already marked. An underscore, e C 1 should represent unmarked...

  • Program is in C++, program is called airplane reservation. It is suppose to display a screen...

    Program is in C++, program is called airplane reservation. It is suppose to display a screen of seating chart in the format 1 A B C D E F through 10. I had a hard time giving the seats a letter value. It displays a correct screen but when I reserve a new seat the string seats[][] doesn't update to having a X for that seat. Also there is a file for the struct called systemUser.txt it has 4 users...

  • Professor Dolittle has asked some computer science students to write a program that will help him...

    Professor Dolittle has asked some computer science students to write a program that will help him calculate his final grades. Professor Dolittle gives two midterms and a final exam. Each of these is worth 100 points. In addition, he gives a number of homework assignments during the semester. Each homework assignment is worth 100 points. At the end of the semester, Professor Dolittle wants to calculate the median score on the homework assignments for the semester. He believes that the...

  • In this project, you will write a complete program that allows the user to play a...

    In this project, you will write a complete program that allows the user to play a game of Mastermind against the computer. A Mastermind game has the following steps: 1. The codebreaker is prompted to enter two integers: the code length n, and the range of digits m. 2. The codemaker selects a code: a random sequence of n digits, each of which is in the range [0,m-1]. 3. The codebreaker is prompted to enter a guess, an n-digit sequence....

  • I have already finished most of this assignment. I just need some help with the canMove...

    I have already finished most of this assignment. I just need some help with the canMove and main methods; pseudocode is also acceptable. Also, the programming language is java. Crickets and Grasshoppers is a simple two player game played on a strip of n spaces. Each space can be empty or have a piece. The first player plays as the cricket pieces and the other plays as grasshoppers and the turns alternate. We’ll represent crickets as C and grasshoppers as...

  • 14.3: More Sentences Write a program that allows a user to enter a sentence and then...

    14.3: More Sentences Write a program that allows a user to enter a sentence and then the position of two characters in the sentence. The program should then report whether the two characters are identical or different. When the two characters are identical, the program should display the message: <char> and <char> are identical! Note that <char> should be replaced with the characters from the String. See example output below for more information. When the two characters are different, the...

  • In C++ There is modification to the original assignment - mainly the input/output. Instead of having...

    In C++ There is modification to the original assignment - mainly the input/output. Instead of having the players enter the coordinates , the players will enter a number (1 - 9) instead. The game board should be displayed as: 1 2 3 4 5 6 7   8   9 If player O enters a 5, the game board will become: 1 2 3 4 O 6 7   8   9 ================================================================================= If a player entered a character other than 1 - 9,...

  • 6.15 Program 6: Using Arrays to Count Letters in Text 1. Introduction In this program, you...

    6.15 Program 6: Using Arrays to Count Letters in Text 1. Introduction In this program, you will practice working with arrays. Your program will read lines of text from the keyboard and use an array to track the number of times each letter occurs in the input text. You will use the contents of this array to generate a histogram (bar graph) indicating the relative frequencies of each letter entered. Test cases are available in this document. Remember, in addition...

  • Write a program that writes a series of random numbers to a file. Each random number...

    Write a program that writes a series of random numbers to a file. Each random number should be in the range of 1 through 500 inclusive. 1.Use a file called randoms.txt as a input file and output file a. If you go to open the file and its not there then ask the user to create the file     and then quit 2. Ask the user to specify how many random numbers the file will hold. a.Make sure that the...

  • In C Program This program will store the roster and rating information for a soccer team. There w...

    In C Program This program will store the roster and rating information for a soccer team. There will be 3 pieces of information about each player: Name: string, 1-100 characters (nickname or first name only, NO SPACES) Jersey Number: integer, 1-99 (these must be unique) Rating: double, 0.0-100.0 You must create a struct called "playData" to hold all the information defined above for a single player. You must use an array of your structs to to store this information. You...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT