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 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.
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; }...
Print the following two patterns using nested loops. Using java. Pattern 1 13579 13579 13579 13579...
Print the following two patterns using nested loops. Using java. Pattern 1 13579 13579 13579 13579 Pattern 2 #####1 ### #12 ###123 ##1234 #12345
JAVA 6.8 PRACTICE: Loops*: Biggest difference Write a program that outputs the biggest difference (absolute value)...
JAVA 6.8 PRACTICE: Loops*: Biggest difference Write a program that outputs the biggest difference (absolute value) between any successive pair of numbers in a list. Such a list might represent daily stock market prices or daily temperatures, so the difference represents the biggest single-day change. The input is the list size, followed by the numbers. If the input is 5 60 63 68 61 59, the output is 7. Hints: Declare a variable for the current number, and another for...
Using Loops for the Hotel Occupancy calculator. You will write a program that calculates the occupancy...
Using Loops for the Hotel Occupancy calculator. You will write a program that calculates the occupancy of a hotel. Rules: 1# The hotel must have more than 2 floors and less than or equal 5 floors. 2# Each floor in the hotel can have a different number of rooms on the floor. 3# You must set the number of occupied rooms. Again, there must less rooms occupied than the number of rooms. 4# Using the total number of rooms and...
Write a program In python of Jupiter notebook (using loops) that prompts the user to enter...
Write a program In python of Jupiter notebook (using loops) that prompts the user to enter his/her favorite English saying, then counts the number of vowels in that (note that the user may type the saying using any combination of upper or lower case letters). Example: Enter your favorite English saying: Actions speak LOUDER than words. Number of wovels: 10
Write a java code that first discards as many whitespace characters as necessary until the first...
Write a java code that first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value. The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function. If the first sequence of non-whitespace...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT