Create a Java program to simulate some 2 team game. Ask the user to input the fo
ID: 3847273 • Letter: C
Question
Create a Java program to simulate some 2 team game. Ask the user to input the following information:
gN - Game Name
t1N - Team 1 Name
t2N - Team 2 Name
nP - # of periods (quarters, innings, periods)
sV – points per score - choose one, even if game has multiple scores (i.e. football)
sN – Score Name
pN – Period Name
The program should work for any game such as football, baseball, hockey, basketball, soccer, etc.
Use appropriate variable names and data types for these inputs.
Also create variables to accumulate the scores for each respective team, such as team1Score, team2Score.
Input data for the 7 variables from the user.
Set up a counter controlled loop to go through the periods from 1..nP.
Tally the points for each team, then report the results, along with the winner, or if it were a tie. Create data validation loops for the 2 numeric inputs (range check for 1 through 7).
Provide 3 sets of sample output. Store the files in a .zip (not a .7zip or .rar). Here is an example of Sample Output:
Please Enter Game Name: Football
Please Enter Football Team 1 Name: Dolphins
Please Enter Football Team 2 Name: Chargers
What is a score in Football called? Touchdown
How many points per Touchdown in Football? 7
What is a period in Football called? Quarter
How many Quarters in Football? 4
Quarter #1:
How many Touchdowns for Dolphins? 2
How many Touchdowns for Chargers? 1
Quarter #2:
How many Touchdowns for Dolphins? 0
How many Touchdowns for Chargers? 1
Quarter #3:
How many Touchdowns for Dolphins? 0
How many Touchdowns for Chargers? 2
Quarter #4:
How many Touchdowns for Dolphins? 3
How many Touchdowns for Chargers? 0
Football Game Results:
------------------------------
Dolphins scored 5 Touchdowns for a score of 35
Chargers scored 4 Touchdowns for a score of 28
Dolphins Win by 7 points!
Explanation / Answer
Here is the java code for the program. Sample outputs will follow the code.
import java.util.Scanner;
import java.lang.*;
import java.io.*;
public class test
{
public static void main (String[] args) throws java.lang.Exception
{
String game_name, team1, team2, score_name, period_name, points, period_no;
Scanner in = new Scanner(System.in);
System.out.print("Please enter Game name:");
game_name = in.nextLine();
System.out.print("Please Enter "+game_name+" Team 1 Name:");
team1 = in.nextLine();
System.out.print("Please Enter "+game_name+" Team 2 Name:");
team2 = in.nextLine();
System.out.print("What is a score "+game_name+" called?");
score_name = in.nextLine();
System.out.print("How many points per "+score_name+" in Football?");
points = in.nextLine();
System.out.print("What is a period in "+game_name+" called?");
period_name = in.nextLine();
System.out.print("How many "+period_name+" in "+game_name+"?");
period_no = in.nextLine();
int period_no_int = Integer.parseInt(period_no);
int points_int = Integer.parseInt(points);
int forteam1, forteam2, team1total=0, team2total=0, winby, periodnumber;
for(int i=0;i<period_no_int;i++)
{
periodnumber=i+1;
System.out.println(period_name+" #"+periodnumber);
System.out.print("How many"+score_name+" for "+team1+"?");
forteam1 = in.nextInt();
System.out.print("How many"+score_name+" for "+team2+"?");
forteam2 = in.nextInt();
team1total+=forteam1;
team2total+=forteam2;
}
int finalscore1 = team1total*points_int;
int finalscore2 = team2total*points_int;
System.out.println(game_name+" Game Result:");
System.out.println("-----------------------------------");
System.out.println(team1+" scored "+team1total+" "+score_name+" for a score of"+finalscore1);
System.out.println(team2+" scored "+team2total+" "+score_name+" for a score of"+finalscore2);
if(finalscore1>finalscore2)
{
winby = finalscore1-finalscore2;
System.out.println(team1+" Win by "+winby+" points!");
}
else
{
winby = finalscore2-finalscore1;
System.out.println(team2+" Win by "+winby+" points!");
}
}
}
Sample Output #1
Please enter Game name:Hockey
Please Enter Hockey Team 1 Name:India
Please Enter Hockey Team 2 Name:Pakistan
What is a score Hockey called?Goals
How many points per Goals in Football?1
What is a period in Hockey called?Halfs
How many Halfs in Hockey?2
Halfs #1
How manyGoals for India?3
How manyGoals for Pakistan?1
Halfs #2
How manyGoals for India?5
How manyGoals for Pakistan?4
Hockey Game Result:
-----------------------------------
India scored 8 Goals for a score of8
Pakistan scored 5 Goals for a score of5
India Win by 3 points!
Sample Output #2
Please enter Game name:Basketball
Please Enter Basketball Team 1 Name:Jordans
Please Enter Basketball Team 2 Name:Supergiants
What is a score Basketball called?Baskets
How many points per Baskets in Football?4
What is a period in Basketball called?Halfs
How many Halfs in Basketball?2
Halfs #1
How manyBaskets for Jordans?30
How manyBaskets for Supergiants?22
Halfs #2
How manyBaskets for Jordans?28
How manyBaskets for Supergiants?19
Basketball Game Result:
-----------------------------------
Jordans scored 58 Baskets for a score of 232
Supergiants scored 41 Baskets for a score of 164
Jordans Win by 68 points!
Sample Output #3
Please enter Game name:Soccer
Please Enter Soccer Team 1 Name:Phantoms
Please Enter Soccer Team 2 Name:Knicks
What is a score Soccer called?Touchdown
How many points per Touchdown in Football?3
What is a period in Soccer called?Quarter
How many Quarter in Soccer?4
Quarter #1
How manyTouchdown for Phantoms?10
How manyTouchdown for Knicks?9
Quarter #2
How manyTouchdown for Phantoms?8
How manyTouchdown for Knicks?10
Quarter #3
How manyTouchdown for Phantoms?8
How manyTouchdown for Knicks?29
Quarter #4
How manyTouchdown for Phantoms?19
How manyTouchdown for Knicks?2
Soccer Game Result:
-----------------------------------
Phantoms scored 45 Touchdown for a score of 135
Knicks scored 50 Touchdown for a score of 150
Knicks Win by 15 points!