Question

In: Computer Science

using java LO: (Apply) Students will write loops that iterate until a condition is met. LO:...

using java

LO: (Apply) Students will write loops that iterate until a condition is met.

LO: (Analyze) Students will identify edge cases from a problem statement.


In this game of volleyball, two teams compete to be the first team to score 21 points. However, to win the game, a team must also lead by two points.

For example, if team A is tied with team B (20-20) and then scores, they will only be ahead by one point (21-20). Then, if team B scores a point, the game will be tied again (21-21). Then, if either team can score two consecutive points, they will win.

Write a program that reads data about volleyball matches from standard input and then determines the outcome of the game. If the input word is "A" it means that team A scored a point. If the input word is "B" it means that team B scored a point. If the input word is "end" it means that the game ended. Calculate the scores and announce the team that won or that the game was a draw. Display the game result in this format:

Team A won! (21-14)

starter code :

import java.util.Scanner;

/*
* Determines the outcome of a volleyball game.
*/
public class Volleyball {
public static void main(String[] args) {



System.exit(0);
}
}

Solutions

Expert Solution

I will build a console application for this problem statement using Eclipse, you can use any IDE of your choice, the code must behave the same.

The source code is as follows,

import java.util.Scanner;

public class Volleyball {

    //declaration and initialisation of the variables that are later 
    //required in the entire lifespan of the application.

        int scoreA = 0;
        int scoreB = 0;
        int endScore = 21;
        boolean inputFlag = true;

        //add score method

        public void addScore(String team) {
                if(team.equalsIgnoreCase("A")) {
                        scoreA +=1;
                        checkDues(scoreA,scoreB);
                } else if (team.equalsIgnoreCase("B")) {
                        scoreB +=1;
                        checkDues(scoreA,scoreB);
                } else if (team.equalsIgnoreCase("end")) {
                        inputFlag = false;
                        System.out.println("Game Ended "+"Team A - "+ scoreA+ "Team B - "+scoreB);
                }
                compareScore(scoreA,scoreB,endScore);
        }

        //Check dues method

        public void checkDues(int scrA,int scrB) {
                if((scrA >= 20) && (scrB >= 20)) {
                        if((scrA == scrB)) {
                                endScore +=1;
                        }
                }
        }
        public void compareScore(int scrA,int scrB, int endScore) {     
                if((scrA == endScore) && (scrB < endScore)) {
                        System.out.println("Team A won! ("+scrA+"-"+scrB+")");
                        inputFlag = false;
                } else if((scrB == endScore) && (scrA < endScore)) {
                        System.out.println("Team B won! ("+scrA+"-"+scrB+")");
                        inputFlag = false;
                }
        }

     // main method is the entry point of the java application

        public static void main(String[] args) {
                
                Scanner input = new Scanner(System.in);

      // vb is an instance(object)of class Vollyball.
                Volleyball vb = new Volleyball(); 

                while(vb.inputFlag) {
                        System.out.println("please input the scored team");
                        String inputTeam = input.nextLine();
                        vb.addScore(inputTeam);

                        //(use the below code for debugging):
                        //System.out.println("Scores"+ vb.scoreA+","+vb.scoreB);
                } 

        }

}

Output screenshot:

From Question:

LO: (Apply) Students will write loops that iterate until a condition is met.

ANS: Here in the code you can see a while loop which is continuesly checking "inputFlag" when the game ends the "inputFlag" is set to "false" and the while loop breaks.

LO: (Analyze) Students will identify edge cases from a problem statement.

ANS: Edge cases are as follows

  1. Team A winning.
  2. Team B winning.
  3. Manual entry of "end" .
  4. Dues - special case in vollyball game.

Related Solutions

Write a java program that will ask the user to enter integers (use loops) until -100...
Write a java program that will ask the user to enter integers (use loops) until -100 is entered. The program will find and display the greatest, the smallest, the sum and the average of the entered numbers. also write a separate driver class which will be used to run and display the greatest, the smallest, the sum and the average of the entered numbers. Thanks!!
write a java code program using loops to compute the sum of the 30 terms of...
write a java code program using loops to compute the sum of the 30 terms of the series below. Find sum of all integers between 100 and 200 which are divisible by 9 or 13 Write a program to find the sum of the first 8 terms of the series 1 + 11 + 111 + 1111 + . . . Output: Term Ratio Sum
Write a program in java which is in main and uses no classes, methods, loops or...
Write a program in java which is in main and uses no classes, methods, loops or if statements Ask the user to enter their first name Ask the user to enter their last name Print their initials followed by periods (ie. Clark Kent = C. K.) Print the number of letters in their last name
C++ please 1. Write a do while loop that continually loops until the user enters a...
C++ please 1. Write a do while loop that continually loops until the user enters a single digit between 0 and 9. There should only be one prompt for the user to enter a number inside the loop. 2. Write a simple 4 function calculator using a switch statement. The program should switch on the operator: +, -, *, /. The user should enter two numbers (any type is fine) and then enter an operator and the program should perform...
Write a Java program that uses nested for loops to print a multiplication table as shown...
Write a Java program that uses nested for loops to print a multiplication table as shown below.   Make sure to include the table headings and separators as shown.   The values in the body of the table should be computed using the values in the heading   e.g. row 1 column 3 is 1 times 3.
Write a java program to solve Towers of Hanoi with the condition that there are "m"...
Write a java program to solve Towers of Hanoi with the condition that there are "m" number of rods and "n" number of disks. Where m >= 3 and n >=1.
In Java: Write a program that prompts the user to enter a positive number until the...
In Java: Write a program that prompts the user to enter a positive number until the user input a negative number. In the end, the program outputs the sum of all the positive numbers. You should use do-while loop (not while, not for). You cannot use break or if statement in the lab. Hint: if the program adds the last negative number to your sum, you can subtract the last number from the sum after the loop.
write JAVA code with the following condition Write the pseudocode for a new data type MyStack...
write JAVA code with the following condition Write the pseudocode for a new data type MyStack that implements a stack using the fact that you have access to a queue data structure with operations enqueue(), dequeue(), isEmpty(). Remember that every stack should have the operations push() and pop(). Hint: use two queues, one of which is the main one and one is temporary. Please note that you won’t be able to implement both push() and pop() in constant time. One...
java code Question 4: Iterating with loops You can process the list using a For loop....
java code Question 4: Iterating with loops You can process the list using a For loop. The variable in the For loop becomes the index value for each of the items in the loop. Everything you do to an element inside the loop gets done for the entire list. Examine the following loops. Then use what you know to write the following loops. int[] numbers = new int[100]; for(int i = 0; i < numbers.length; i++){ numbers[i] = i; }...
JAVA Program Write a program that prompts the user for data until the user wishes to...
JAVA Program Write a program that prompts the user for data until the user wishes to stop (you must have a while loop) (You must read in at least 8 to 10 sets of voter data using dialog boxes) The data to read in is: The registration of the voter (Democrat, Republican or other) The gender of the voter The Presidential candidate the voter is choosing (Trump or Biden) Which candidate has done better to manage the economy? (Trump or...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT