In: Computer Science
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);
}
}
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