NO HANDWRITINGS. IN JAVA ONLY THIS IS MY 4TH TIME POSTING THIS QUESTION, PLEASE
ID: 3749268 • Letter: N
Question
NO HANDWRITINGS. IN JAVA ONLY
THIS IS MY 4TH TIME POSTING THIS QUESTION, PLEASE ANSWER ONLY IF YOU KNOW THE ANSWER.
Design a program in java that will allow a user to keep track of the status of a soccer game. You will design a SoccerGame class that will represent the state of the soccer game.
Its attributes are:
The name of the first team
The name of the second team
The score of the first team
The score of the second team
The status of the game (finished or in progress)
The constructor will pass in two team names and will create a SoccerGame object where the score of each team is 0 and the status of the game is in progress.
The methods for the SoccerGame class are:
Record a goal scored (add one to the team score)
Change status of the game as finished
Return the score of a team
Return the name of the team that is currently winning or has won the game
You will design a test class that allows the user to instantiate a SoccerGame object for a current game.
The user will enter the names of two teams and the program will start a soccerGame object. Then the program will allow the user to update the object as needed until the game ends.
The program will loop and allow the score to be updated each time a team scores a goal until the game has finished. At that time, the user will get the score of each team and then get the name of the team that has won the game.
The program will also change the status of the game to finished.
Test Cases:
Provide two test cases, presenting the inputs and required outputs for each case, and the resulting action of the program.
Program Files:
SoccerGame.java, TestSoccerGame.java
Explanation / Answer
----------------------------------------------------------------------------------------------------------------------------------
SoccerGame.java
/**
* The class will represent the state of the soccer game.
*
* @author
*/
//represents two status of team
enum Status {
InProgress, Finished;
}
public class SoccerGame {
//team names
String firstTeam;
String secondTeam;
//team score
int score1;
int score2;
//can be either 0->InProgress , 1->Finished
Status status;
/**
* It will create a SoccerGame object where the score of each team is 0 and
* the status of the game is in progress.
*
* @param firstTeam name of first team
* @param secondTeam name of second team
*/
public SoccerGame(String firstTeam, String secondTeam) {
this.firstTeam = firstTeam;
this.secondTeam = secondTeam;
score1 = 0;
score2 = 0;
status = Status.InProgress;
}
/**
* adds one to the team score
*
* @param team represents the team whose score ahs to be added
*/
public void recordGoal(int team) {
if (status == Status.Finished) {
System.out.println(" Game status:Finished .Cannot change score.");
}
else
{
if (team == 1) {
score1++;
}
else {
score2++;
}
}
}
/**
* Called when the game is over
*/
public void changeStatus() {
if (status == Status.Finished) {
System.out.println(" Game status:Finished already.Cannot change status.");
} else {
status = Status.Finished;
}
print();
}
/**
* prints the current status of game
*/
public void print() {
System.out.println("************Soccer Game**************");
System.out.println("Team 1 :" + firstTeam + " Goal:" + score1);
System.out.println("Team 2 :" + secondTeam + " Goal:" + score2);
if (status == Status.Finished) {
if (score1 > score2) {
System.out.println(firstTeam + " Wins!!!");
} else if (score2 > score1) {
System.out.println(secondTeam + " Wins!!!");
} else {
System.out.println("Match drawn!!!");
}
} else if (score1 > score2) {
System.out.println(firstTeam + " Leading!!!");
} else if (score1 == score2) {
System.out.println("Match draw:equal winning chance for both");
} else {
System.out.println(secondTeam + " Leading!!!");
}
System.out.println("*******************************************");
}
}
--------------------------------------------------------------------------------------------------------------------------------------
TestSoccerGame.java
import java.util.Scanner;
/**
* Tests the SoccerGame class
*
* @author
*/
public class TestSoccerGame {
public static void main(String args[]) {
String team1, team2;
int choice;
Scanner in = new Scanner(System.in);
System.out.println("Enter name of two teams:");
System.out.print("Team 1:");
team1 = in.nextLine();
System.out.print(" Team 2:");
team2 = in.nextLine();
SoccerGame game = new SoccerGame(team1, team2);//creates new object
while (true)//loops till user enters 5
{
System.out.println(" 1.Record Goal :" + team1);
System.out.println("2.Record Goal :" + team2);
System.out.println("3.Print current status");
System.out.println("4.Change Status Spa");
System.out.println("5.Exit");
System.out.print("Enter your Choice(1-5):");
choice = in.nextInt();
switch (choice) {
case 1:
game.recordGoal(1);
break;
case 2:
game.recordGoal(2);
break;
case 3:
game.print();
break;
case 4:
game.changeStatus();
break;
case 5:
System.exit(0);
break;
default:
System.out.println("Enter correct choice(1-5)");
}
}
}
}
--------------------------------------------------------------------------------------------------------
Test 1
run:
Enter name of two teams:
Team 1:Spain
Team 2:Portugal
1.Record Goal :Spain
2.Record Goal :Portugal
3.Print current status
4.Change Status
5.Exit
Enter your Choice(1-5):1
1.Record Goal :Spain
2.Record Goal :Portugal
3.Print current status
4.Change Status
5.Exit
Enter your Choice(1-5):1
1.Record Goal :Spain
2.Record Goal :Portugal
3.Print current status
4.Change Status
5.Exit
Enter your Choice(1-5):1
1.Record Goal :Spain
2.Record Goal :Portugal
3.Print current status
4.Change Status
5.Exit
Enter your Choice(1-5):2
1.Record Goal :Spain
2.Record Goal :Portugal
3.Print current status
4.Change Status
5.Exit
Enter your Choice(1-5):3
************Soccer Game**************
Team 1 :Spain Goal:3
Team 2 :Portugal Goal:1
Spain Leading!!!
*******************************************
1.Record Goal :Spain
2.Record Goal :Portugal
3.Print current status
4.Change Status
5.Exit
Enter your Choice(1-5):1
1.Record Goal :Spain
2.Record Goal :Portugal
3.Print current status
4.Change Status
5.Exit
Enter your Choice(1-5):2
1.Record Goal :Spain
2.Record Goal :Portugal
3.Print current status
4.Change Status
5.Exit
Enter your Choice(1-5):1
1.Record Goal :Spain
2.Record Goal :Portugal
3.Print current status
4.Change Status
5.Exit
Enter your Choice(1-5):4
************Soccer Game**************
Team 1 :Spain Goal:5
Team 2 :Portugal Goal:2
Spain Wins!!!
*******************************************
1.Record Goal :Spain
2.Record Goal :Portugal
3.Print current status
4.Change Status
5.Exit
Enter your Choice(1-5):1
Game status:Finished .Cannot change score.
1.Record Goal :Spain
2.Record Goal :Portugal
3.Print current status
4.Change Status
5.Exit
Enter your Choice(1-5):3
************Soccer Game**************
Team 1 :Spain Goal:5
Team 2 :Portugal Goal:2
Spain Wins!!!
*******************************************
1.Record Goal :Spain
2.Record Goal :Portugal
3.Print current status
4.Change Status
5.Exit
Enter your Choice(1-5):5
BUILD SUCCESSFUL (total time: 44 seconds)
Test2
run:
Enter name of two teams:
Team 1:MohanBegan
Team 2:KinghtRiders
1.Record Goal :MohanBegan
2.Record Goal :KinghtRiders
3.Print current status
4.Change Status Spa
5.Exit
Enter your Choice(1-5):1
1.Record Goal :MohanBegan
2.Record Goal :KinghtRiders
3.Print current status
4.Change Status Spa
5.Exit
Enter your Choice(1-5):1
1.Record Goal :MohanBegan
2.Record Goal :KinghtRiders
3.Print current status
4.Change Status Spa
5.Exit
Enter your Choice(1-5):2
1.Record Goal :MohanBegan
2.Record Goal :KinghtRiders
3.Print current status
4.Change Status Spa
5.Exit
Enter your Choice(1-5):2
1.Record Goal :MohanBegan
2.Record Goal :KinghtRiders
3.Print current status
4.Change Status Spa
5.Exit
Enter your Choice(1-5):3
************Soccer Game**************
Team 1 :MohanBegan Goal:2
Team 2 :KinghtRiders Goal:2
Match draw:equal winning chance for both
*******************************************
1.Record Goal :MohanBegan
2.Record Goal :KinghtRiders
3.Print current status
4.Change Status Spa
5.Exit
Enter your Choice(1-5):1
1.Record Goal :MohanBegan
2.Record Goal :KinghtRiders
3.Print current status
4.Change Status Spa
5.Exit
Enter your Choice(1-5):2
1.Record Goal :MohanBegan
2.Record Goal :KinghtRiders
3.Print current status
4.Change Status Spa
5.Exit
Enter your Choice(1-5):2
1.Record Goal :MohanBegan
2.Record Goal :KinghtRiders
3.Print current status
4.Change Status Spa
5.Exit
Enter your Choice(1-5):4
************Soccer Game**************
Team 1 :MohanBegan Goal:3
Team 2 :KinghtRiders Goal:4
KinghtRiders Wins!!!
*******************************************
1.Record Goal :MohanBegan
2.Record Goal :KinghtRiders
3.Print current status
4.Change Status Spa
5.Exit
Enter your Choice(1-5):5
BUILD SUCCESSFUL (total time: 42 seconds)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.