Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have been given a Football interface and the instructor asked me to create a c

ID: 3670606 • Letter: I

Question

I have been given a Football interface and the instructor asked me to create a class FootballScore that will implements the methods in the Football Interface. FootballScore should have a way of storing multiple game scores. You do not know how many will be stored and are not allowed to ask the user to enter the number of games. Game scores are entered in pairs (hometeam and visiting team). You can store these however you like. The scores will be treated as a pair of scores when you process these later.

All input and output must be done in the FootballTester. You must handle invalid input in FootballTester to prevent invalid entries from causing your program to crash.

Here is Football interface

public interface FootballInterface
{
       int getMaxScore();
       int getMinScore();
       double getAverage();
       int getCount();
       void enterScore(int homeScore, int visitorScore);
       String[] getGameScores();
}

Thanks for the help

Explanation / Answer

If you have any questions please comment on the answer

Score.java

//Class representing score of a game
public class Score {

   private int homeScore;
   private int visitingScore;
  
   public Score(int home,int visiting)
   {
       homeScore=home;
       visitingScore=visiting;
   }
  
   public int getHomeScore() {
       return homeScore;
   }
   public void setHomeScore(int homeScore) {
       this.homeScore = homeScore;
   }
   public int getVisitingScore() {
       return visitingScore;
   }
   public void setVisitingScore(int visitingScore) {
       this.visitingScore = visitingScore;
   }
}

FootballScore.java

import java.util.ArrayList;

public class FootballScore implements FootballInterface {

   // We dont know the number of games so we will use array list
   // If number of games would have been known we could have used a array
   private ArrayList<Score> scores = new ArrayList<Score>();
   private int count = 0;

   public int getMaxScore() {
       int max = -1;
       // enhanced for loop for iterating over the array list of scores
       for (Score s : scores) {
           if (s.getHomeScore() > max) {
               max = s.getHomeScore();
           }
           if (s.getVisitingScore() > max) {
               max = s.getVisitingScore();
           }
       }
       return max;
   }

   public int getMinScore() {
       int min = 100;
       // enhanced for loop for iterating over the array list of scores
       for (Score s : scores) {
           if (s.getHomeScore() < min) {
               min = s.getHomeScore();
           }
           if (s.getVisitingScore() < min) {
               min = s.getVisitingScore();
           }
       }
       return min;
   }

   public double getAverage() {
       int sum_score = 0;
       for (Score s : scores) {
           sum_score += s.getHomeScore() + s.getVisitingScore();
       }
       // You can use count here for total number of matches or you can also
       // get number of matches directly by arraylist size
       double average = sum_score / scores.size();
       return average;
   }

   public int getCount() {

       return count;
   }

   public void enterScore(int homeScore, int visitorScore) {
       Score s = new Score(homeScore, visitorScore);
       scores.add(s);
       count++;

   }

   public String[] getGameScores() {
       //allocating a string array of size equal to numner of games
       String[] s=new String[scores.size()];
       int i=0;
       for(Score ss:scores)
       {
           s[i++]="Home :"+ss.getHomeScore()+" Visiting :"+ss.getVisitingScore();;
       }
       return s;
   }

}

FootballTester.java

import java.io.*;
public class FootballTester {
  
   public static void main(String args[])throws IOException
   {
       InputStreamReader ir=new InputStreamReader(System.in);
       BufferedReader br=new BufferedReader(ir);
      
       System.out.println("press -1 to stop entering scores");
      
       FootballScore scores=new FootballScore();
       while(true)
       {
           System.out.println("Enter the score,first home then visiting team score");
           int homeScore=Integer.parseInt(br.readLine());
           int visitorScore=Integer.parseInt(br.readLine());
           if(homeScore==-1)
           {
               break;
           }
           if(homeScore>=0 && visitorScore>=0)
           {
               scores.enterScore(homeScore, visitorScore);
           }
           else
           {
               System.out.println("Enter a valid score");
           }
          
       }
      
       System.out.println("Total number of games "+scores.getCount());
       System.out.println("Highest score by a team is "+scores.getMaxScore());
       System.out.println("Minimum score by a team is "+scores.getMinScore());
       System.out.println("Average score of all games is "+scores.getAverage());
      
       String[] s=scores.getGameScores();
       for(int i=0;i<s.length;++i)
       {
           System.out.println(s[i]);
       }
   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote