help Chapter 5, PP 7 Java Project Name: IC17_FootballGame Create a class named F
ID: 3935892 • Letter: H
Question
help
Chapter 5, PP 7 Java Project Name: IC17_FootballGame
Create a class named FootballGame that contains information about a game played between two teams. The FootballGame class has the following instance variables (a.k.a. fields or data):
homeTeam (String)
visitorTeam (String)
homeScore (int)
visitorScore (int)
The FootballGame class will have methods to:
Create a new FootballGame (given a homeTeam and visitorTeam only - score should be 0 and 0) [parameterized constructor]
getHomeScore()
getVisitorScore()
scoreTouchdown(String teamName) - adds 6 points to the appropriate team (home or visitor). Returns true if the teamName equals either homeTeam or visitorTeam , false otherwise.
scoreExtraPoint(String teamName) - adds 1 point to the appropriate team (home or visitor). Returns true if the teamName equals either homeTeam or visitorTeam , false otherwise.
scoreConversion(String teamName) - adds 2 points to the appropriate team (home or visitor). Returns true if the teamName equals either homeTeam or visitorTeam , false otherwise.
scoreFieldGoal(String teamName) - adds 3 points to the appropriate team (home or visitor). Returns true if the teamName equals either homeTeam or visitorTeam , false otherwise.
scoreSafety(String teamName) - adds 2 points to the appropriate team (home or visitor). Returns true if the teamName equals either homeTeam or visitorTeam , false otherwise.
equals - method to check if one FootballGame is the same as another by comparing all instance variables for equality.
toString - method to turn a FootballGame into a string for display, e.g. display as "FootballGame [LA Rams=6, NY Giants=0 ~~~ Home Team is Winning]" (assume the LA Rams were the home team in this game)
Below is a class diagram showing the fields and methods of the FootballGame class.
After you complete the class, please create a driver class (called FootballGameDemo) that tests the FootballGame class just created. For example, the demo should print:
Please enter home team name: LA Rams
Please enter visitor team name: NY Giants
Enter Scoring Event (or -1 to end game):
1) To score touchdown
2) To score extra point
3) To score conversion
4) To score field goal
5) To score safety
>>1
Enter Team:
1) For LA Rams (home)
2) For NY Giants (visitor)
>> 1
FootballGame [LA Rams=6, NY Giants=0 ~~~ Home Team is Winning]
Enter Scoring Event (or -1 to end game):
1) To score touchdown
2) To score extra point
3) To score conversion
4) To score field goal
5) To score safety
>>1
Enter Team:
1) For LA Rams (home)
2) For NY Giants (visitor)
>> 2
FootballGame [LA Rams=6, NY Giants=6 ~~~ It's a Tie]
Enter Scoring Event (or -1 to end game):
1) To score touchdown
2) To score extra point
3) To score conversion
4) To score field goal
5) To score safety
>>4
Enter Team:
1) For LA Rams (home)
2) For NY Giants (visitor)
>> 2
FootballGame [LA Rams=6, NY Giants=9 ~~~ Visiting Team is Winning]
Explanation / Answer
import java.util.Scanner;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Surya
*/
public class FootballGame {
String homeTeam,visitorTeam;//declaring variables//
int homeScore=0,visitorScore=0;
boolean scoreTouchdown(String teamName)//method which adds 6 points to the appropriate team (home or visitor).
{
if(teamName.equals(homeTeam))
{
homeScore += 6;
return true;
}
else if(teamName.equals(visitorTeam))
{
visitorScore +=6;
return true;
}
else return false;
}
boolean scoreExtraPoint(String teamName)//method which adds 1 point to the appropriate team (home or visitor).
{
if(teamName.equals(homeTeam))
{
homeScore += 1;
return true;
}
else if(teamName.equals(visitorTeam))
{
visitorScore +=1;
return true;
}
else return false;
}
boolean scoreConversion(String teamName)//method which adds 2 points to the appropriate team (home or visitor).
{
if(teamName.equals(homeTeam))
{
homeScore += 2;
return true;
}
else if(teamName.equals(visitorTeam))
{
visitorScore +=2;
return true;
}
else return false;
}
boolean scoreFieldGoal(String teamName)//method which adds 3 points to the appropriate team (home or visitor).
{
if(teamName.equals(homeTeam))
{
homeScore += 3;
return true;
}
else if(teamName.equals(visitorTeam))
{
visitorScore +=3;
return true;
}
else return false;
}
boolean scoreSafety(String teamName)//method which adds 2 points to the appropriate team (home or visitor).
{
if(teamName.equals(homeTeam))
{
homeScore += 2;
return true;
}
else if(teamName.equals(visitorTeam))
{
visitorScore +=2;
return true;
}
else return false;
}
void display()//method to display output...scores..
{
String s="FootballGame["+homeTeam+"="+homeScore+","+visitorTeam+"="+visitorScore+"~~~";
String h="Home team is Wining]",v="Visitor team is Wining]",t="It's a Tie]";
System.out.print(s);
if(homeScore>visitorScore)System.out.println(h);
else if(homeScore<visitorScore)System.out.print(v);
else System.out.print(t);
}
public static void main(String argv[])
{
//testing methods through user inputs...
System.out.print("Please Enter Home team name:");
Scanner sc = new Scanner(System.in);
FootballGame f = new FootballGame();
f.homeTeam= sc.nextLine();
System.out.print("Please Enter Visitor team name:");
f.visitorTeam=sc.nextLine();
int c=-2;
while(true)
{
System.out.print(" Enter Scoring Event (or -1 to end game): 1) To score touchdown 2) To score extra point 3) To score conversion 4) To score field goal 5) To score safety Enter ur choice:-");
c=sc.nextInt();
if(c==-1)break;
else if(c==1){
System.out.print("Enter Team: 1)for"+f.homeTeam+"(Home) 2)for"+f.visitorTeam+"(visitor)");
c=sc.nextInt();
if(c==1)
f.scoreTouchdown(f.homeTeam);
else f.scoreTouchdown(f.visitorTeam);
f.display();
}
else if(c==2){
System.out.print("Enter Team: 1)for"+f.homeTeam+"(Home) 2)for"+f.visitorTeam+"(visitor)");
c=sc.nextInt();
if(c==1)
f.scoreExtraPoint(f.homeTeam);
else f.scoreExtraPoint(f.visitorTeam);
f.display();
}
else if(c==3){
System.out.print("Enter Team: 1)for"+f.homeTeam+"(Home) 2)for"+f.visitorTeam+"(visitor)");
c=sc.nextInt();
if(c==1)
f.scoreConversion(f.homeTeam);
else f.scoreConversion(f.visitorTeam);
f.display();
}
else if(c==4){
System.out.print("Enter Team: 1)for"+f.homeTeam+"(Home) 2)for"+f.visitorTeam+"(visitor)");
c=sc.nextInt();
if(c==1)
f.scoreFieldGoal(f.homeTeam);
else f.scoreFieldGoal(f.visitorTeam);
f.display();
}
else if(c==5){
System.out.print("Enter Team: 1)for"+f.homeTeam+"(Home) 2)for"+f.visitorTeam+"(visitor)");
c=sc.nextInt();
if(c==1)
f.scoreSafety(f.homeTeam);
else f.scoreSafety(f.visitorTeam);
f.display();
}
}
if(f.homeScore>f.visitorScore)
{
System.out.println(" Home Team"+f.homeTeam+" wins...");
}
else if(f.homeScore<f.visitorScore)
{
System.out.println(" Visitor team"+f.visitorTeam+" wins....");
}
else System.out.println(" It's a Tie... ");
}
}
output:---
run:
Please Enter Home team name:LA Rams
Please Enter Visitor team name:NY Giants
Enter Scoring Event (or -1 to end game):
1) To score touchdown
2) To score extra point
3) To score conversion
4) To score field goal
5) To score safety
Enter ur choice:-1
Enter Team:
1)forLA Rams(Home)
2)forNY Giants(visitor)1
FootballGame[LA Rams=6,NY Giants=0~~~Home team is Wining]
Enter Scoring Event (or -1 to end game):
1) To score touchdown
2) To score extra point
3) To score conversion
4) To score field goal
5) To score safety
Enter ur choice:-1
Enter Team:
1)forLA Rams(Home)
2)forNY Giants(visitor)2
FootballGame[LA Rams=6,NY Giants=6~~~It's a Tie]Enter Scoring Event (or -1 to end game):
1) To score touchdown
2) To score extra point
3) To score conversion
4) To score field goal
5) To score safety
Enter ur choice:-4
Enter Team:
1)forLA Rams(Home)
2)forNY Giants(visitor)2
FootballGame[LA Rams=6,NY Giants=9~~~Visitor team is Wining]Enter Scoring Event (or -1 to end game):
1) To score touchdown
2) To score extra point
3) To score conversion
4) To score field goal
5) To score safety
Enter ur choice:--1
Visitor teamNY Giants wins....
BUILD SUCCESSFUL (total time: 1 minute 41 seconds)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.