Question

In: Computer Science

(Implementing Interfaces) Implement the following interface and classes: a. Interface IsCompetition, which has no constants and...

(Implementing Interfaces) Implement the following interface and classes:
a. Interface IsCompetition, which has no constants and two methods (see the classes below for details about what these methods should do):
i. getSummary(), ii. getCompetitors()

b. Class Game: A game object is associated with a home team, away team, home score, away score, and a date of competition. It should only be created using the following constructor:
Game(String HTeam, String ATeam, int HScore, int AScore, LocalDate date).
A game is also a competition, with getSummary printing which team beat the other and by how many points (you can assume no ties) and getCompetitors printing the two team names separated by a comma and followed by a newline.

c. Class Season: A season consists of one or more games. It’s up to you what data structure you want to hold the games (an ArrayList should be fine). It has a zero-parameter constructor, and thus games must be added with the mutator below:
addGame(Game newGame): adds newGame to the existing season games.
A season is also a competition, with getSummary printing the following details:
i. The earliest and latest dates (games may be added out of chronological order!)
ii. The lowest and highest scores of the season
Likewise, getCompetitors should print a list of all of the teams who competed in the season (duplicates allowed), separated by a comma, followed by a newline.

Make sure you include simple comments for each class and method definition.

For the Season class, let getSummary also print the name of the teams that won the most and fewest games, alongside their win/loss totals.
For the Season class, force getCompetitors to only include each unique team in the list of printed names once and only once.

Solutions

Expert Solution

Please find the complete code below,

IsCompetition.java

public interface IsCompetition {
    public void getSummary();
    public void getCompetitors();
}

Game.java

import java.time.LocalDate;

public class Game implements IsCompetition {

    private String HTeam;
    private String ATeam;
    private int HScore;
    private int AScore;
    private LocalDate date;

    // Constructor as required
    public Game(String HTeam, String ATeam, int HScore, int AScore, LocalDate date) {
        this.HTeam = HTeam;
        this.ATeam = ATeam;
        this.HScore = HScore;
        this.AScore = AScore;
        this.date = date;
    }

    @Override
    public void getSummary() {
        if(HScore < AScore) System.out.println(ATeam +" beats "+ HTeam+" by "+(AScore-HScore)+" points");
        else System.out.println(HTeam +" beats "+ ATeam+" by "+(HScore-AScore)+" points");
    }

    @Override
    public void getCompetitors() {
        System.out.println(HTeam+" , "+ATeam);
    }

    // Getter and setter method to access private variables out of the class
    public String getHTeam() {
        return HTeam;
    }

    public void setHTeam(String HTeam) {
        this.HTeam = HTeam;
    }

    public String getATeam() {
        return ATeam;
    }

    public void setATeam(String ATeam) {
        this.ATeam = ATeam;
    }

    public int getHScore() {
        return HScore;
    }

    public void setHScore(int HScore) {
        this.HScore = HScore;
    }

    public int getAScore() {
        return AScore;
    }

    public void setAScore(int AScore) {
        this.AScore = AScore;
    }

    public LocalDate getDate() {
        return date;
    }

    public void setDate(LocalDate date) {
        this.date = date;
    }
}

Season.java

import java.time.LocalDate;
import java.util.*;

public class Season implements IsCompetition{

    private final List<Game> games;

    public Season(){
        games = new ArrayList<>();
    }

    public void addGame(Game newGame){
        games.add(newGame);
    }

    @Override
    public void getSummary() {
        if(games.isEmpty()){
            System.out.println("No games played till now.");
            return;
        }
        LocalDate earliestDate = null, latestDate = null;
        int lowestScore = Integer.MAX_VALUE, highestScore = Integer.MIN_VALUE;
        for(Game game : games){
            if(earliestDate == null) earliestDate = game.getDate();
            else if(game.getDate().isBefore(earliestDate)) earliestDate = game.getDate();

            if(latestDate == null) latestDate = game.getDate();
            else if(game.getDate().isAfter(latestDate)) latestDate = game.getDate();

            lowestScore = Math.min(lowestScore, Math.min(game.getAScore(), game.getHScore()));
            highestScore = Math.max(highestScore, Math.max(game.getAScore(), game.getHScore()));
        }
        System.out.println("Earliest Date : "+ earliestDate.toString());
        System.out.println("Latest Date : "+ latestDate.toString());

        System.out.println("Lowest Score : "+ lowestScore);
        System.out.println("Highest Score : "+ highestScore);

        Map<String, WinLoss> winsAndLoses = new HashMap<>();
        for(Game game : games){
            WinLoss teamA = winsAndLoses.getOrDefault(game.getATeam(), new WinLoss());
            WinLoss teamH = winsAndLoses.getOrDefault(game.getHTeam(), new WinLoss());
            if(game.getHScore() < game.getAScore()){
                teamA.setWins(teamA.getWins() + 1);
                teamH.setLoss(teamH.getLoss() + 1);
            }
            else {
                teamH.setWins(teamH.getWins() + 1);
                teamA.setLoss(teamA.getLoss() + 1);
            }
            winsAndLoses.put(game.getATeam(), teamA);
            winsAndLoses.put(game.getHTeam(), teamH);
        }
        String mostWinTeamName="", mostLossTeamName="";
        int mostWins = 0, mostLoss = 0;
        for(Map.Entry<String, WinLoss> entry : winsAndLoses.entrySet()){
            WinLoss winLoss = entry.getValue();
            if(winLoss.getWins() > mostWins){
                mostWins = winLoss.getWins();
                mostWinTeamName = entry.getKey();
            }
            if(winLoss.getLoss() > mostLoss){
                mostLoss = winLoss.getLoss();
                mostLossTeamName = entry.getKey();
            }
        }
        System.out.println("TeamName with most number of wins : "+mostWinTeamName+". Won "+mostWins+" games.");
        System.out.println("TeamName with most number of loss : "+mostLossTeamName+". Lost "+mostLoss+" games.");
    }

    @Override
    public void getCompetitors() {
        Set<String> includedTeams = new HashSet<>();
        for(Game game : games){
            if(includedTeams.contains(game.getATeam()) || includedTeams.contains(game.getHTeam())) continue;
            game.getCompetitors();
            includedTeams.add(game.getATeam());
            includedTeams.add(game.getHTeam());
        }
    }

    private static class WinLoss{
        private int wins;
        private int loss;
        public WinLoss(){
            wins = loss = 0;
        }

        public void setLoss(int loss) {
            this.loss = loss;
        }

        public int getLoss() {
            return loss;
        }

        public void setWins(int wins) {
            this.wins = wins;
        }

        public int getWins() {
            return wins;
        }
    }
}

If you have any doubts ask in the comments. Don't forget to upvote the solution.


Related Solutions

My class PayCalculator isn't implementing the interface Constants, it did this before but when I saved...
My class PayCalculator isn't implementing the interface Constants, it did this before but when I saved it went nuts. I was wondering how to fix it? import java.text.DecimalFormat; public class PayCalculator implements Constants {       private String employeeName;    private int reportID;    private double hourlyWage;    private static int ID = 0;    private static int reportIDGenerator = 1000;    public int[] overtimes = HOURS_WORKED; public PayCalculator(String name) {    public PayCalculator() {    this.reportID = reportIDGenerator;   ...
Use inheritance to implement the following classes: A: A Car that is a Vehicle and has...
Use inheritance to implement the following classes: A: A Car that is a Vehicle and has a name, a max_speed value and an instance variable called the number_of_cylinders in its engine. Add public methods to set and get the values of these variables. When a car is printed (using the toString method), its name, max_speed and number_of_cylinders are shown. B: An Airplane that is also a vehicle and has a name, a max_speed value and an instance variable called the...
What are abstract classes? What are interfaces? What is the difference?
What are abstract classes? What are interfaces? What is the difference?
Make a class diagram (static structure with classes, interfaces and their relationship). It should include classes,...
Make a class diagram (static structure with classes, interfaces and their relationship). It should include classes, major attributes, major methods, class associations and multiplicities. The details of the project and its functional requirements are: Develop a sentiment analysis system for product rating. It is an e-commerce web application. The main objective of this sentiment analysis system is to understand the hidden sentiments of customers in feedback and comments and analyze their product rating patterns. When registered customers (they can create...
This activity will require you to apply your knowledge of interfaces – including the Comparable interface...
This activity will require you to apply your knowledge of interfaces – including the Comparable interface – in order to leverage the capabilities of the ArrayList data structure. --------- You may work individually or with a partner, but be sure that each student submits their own assignment in Canvas (and leave a comment with your partner's name if you worked with one). --------- Description: Interfaces are used to make a Class implement a behavior or set of behaviors. Recall the...
(Java) Please describe how API's can be created using abstract classes, interfaces and regular classes.
(Java) Please describe how API's can be created using abstract classes, interfaces and regular classes.
7. What are abstract classes and interfaces in Java? What are they used for, and what...
7. What are abstract classes and interfaces in Java? What are they used for, and what are they comprised of? Clearly explain the difference between the two.
In this assignment will demonstrate your understanding of the following: 1. C++ classes; 2. Implementing a...
In this assignment will demonstrate your understanding of the following: 1. C++ classes; 2. Implementing a class in C++; 3. Operator overloading with chaining; 4. Preprocessor directives #ifndef, #define, and #endif; 5. this – the pointer to the current object. In this assignment you will implement the Date class and test its functionality. Consider the following class declaration for the class date: class Date { public: Date(); //default constructor; sets m=01, d=01, y =0001 Date(unsigned m, unsigned d, unsigned y);...
What are similarities and differences between abstract classes and interfaces? What are the default modifiers of...
What are similarities and differences between abstract classes and interfaces? What are the default modifiers of their methods and variables? What are allowed modifiers of their methods and variables? Essay Question
Behold, the power of interfaces! On this homework problem you'll implement a completely generic version of...
Behold, the power of interfaces! On this homework problem you'll implement a completely generic version of an algorithm to find the maximum of an array. Unlike in the past, when our algorithm only worked for int[] or double[], this version will work on any Java objects that are comparable, specifically any Java object that implements the Comparable interface. Create a public class named Max with a single class method named max. max should accept an array of Objects that implement...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT