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
To understand the value of counting loops: Write a java program that implements matrix multiplication using...
To understand the value of counting loops: Write a java program that implements matrix multiplication using counting loop constructs. Then write the same program using only logical loops—for example, while loops. Write 2 classes for each program Sample Result is shown below: Enter the number of rows of matrix A: 2 Enter the number of columns of matrix A: 3 Enter the number of columns of matrix B: 3 Enter the number of columns of matrix B: 4 Enter matrix...
To understand the value of counting loops: Write a java program that implements matrix multiplication using...
To understand the value of counting loops: Write a java program that implements matrix multiplication using counting loop constructs. Then write the same program using only logical loops—for example, while loops. Sample Result is shown below: Enter the number of rows of matrix A: 2 Enter the number of columns of matrix A: 3 Enter the number of columns of matrix B: 3 Enter the number of columns of matrix B: 4 Enter matrix A; 1 2 3 4 5...
In Java, you can iterate an ArrayList in different ways. Write the following methods to print...
In Java, you can iterate an ArrayList in different ways. Write the following methods to print Integers in an ArrayList iterating in different ways: 1. // Using basic while / for loop void printArrayListBasicLoop(ArrayList<Integer> al); 2. // Using enhanced for loop (:) void printArrayListEnhancedLoop(ArrayList<Integer> al); 3. // Using basic for loop with iterator void printArrayListForLoopListIterator(ArrayList<Integer> al); 4. // Using basic while loop with iterator void printArrayListWhileLoopListIterator(ArrayList<Integer> al);
write a java programming using 2D arrey. The Lo Shu Magic Square is a grid with...
write a java programming using 2D arrey. The Lo Shu Magic Square is a grid with 3 rows and 3 columns. The Lo Shu Magic Square has the following properties: The grid contains the numbers 1 through 9 (each number only once) The sum of each row, each column and each diagonal are the same In a program you can simulate a magic square using a two-dimensional array. Write a method that accepts a two-dimensional array as an argument, and...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT