Please write code that will look like the following picture. Correctly use the \
ID: 3916632 • Letter: P
Question
Please write code that will look like the following picture. Correctly use the "Prime the Read" method for inputting data before a sentinel loop. You also must use ArrayList and the provided Team class which I will include below(i.e. ArrayList<Team>). Any help will be much appreciated as I'm struggling to so this myself.
/*
* The Team Class contains two data elements: the name of the team and
* the number of wins. The class has a toString method which must be
* written by as part of Project6.
* To use this class, you can drag and drop it into the default package
* in your Eclipse installation, or create a new class named Team and
* copy/paste the contents of this file into that new class.
*/
public class Team implements Comparable<Team> {
// _-v-_-v-_-v-_-v-_-v-_-v-_-v-_-v-_-v-_-v-_-v-_-v-_-v-_-v-_-v-_-v-_-v-_
// CS 1323: Implement this method
/**
* This is the method you will fix to allow you to return the contents of
* this Team item in the nice format: "name: winCount". The current
* implementation will allow the program to compile, but it returns nothing.
*
* For example: "Sooners: 3"
*
* @return the formatted String that can then be output to the console.
*/
public String toString() {
return "";
}
// _-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_
// Data fields
private String name;
private int winCount;
// Constructors ---------------------------------------
/*
* I have implemented many constructors to accommodate all of the different
* design choices. You will likely only use one of these in your code.
*/
Team() {
name = "Sooners";
winCount = 1;
}
Team(String inputName) {
name = inputName;
winCount = 1;
}
Team(String inputName, int inputWinCount) {
name = inputName;
winCount = inputWinCount;
}
// ----------------------------------------------------
// Getters and Setters (aka Accessors and Mutators) ===
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name
* the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the winCount
*/
public int getWinCount() {
return winCount;
}
/**
* @param winCount
* the winCount to set
*/
public void setWinCount(int winCount) {
this.winCount = winCount;
}
/**
* Increments the winCount variable by one for this Team
*/
public void incrementWinCount() {
winCount++;
}
/**
* This method allows you to check to see if this Team object has the same
* name as another Team object.
*
* This method allows you to use the contains method in ArrayList to see
* if any element in an array list has the same name as a specific Team.
*
* @param o
* the other Team being compared to.
*/
@Override
public boolean equals(Object o) {
return name.equals(((Team) o).name);
}
/**
* This method allows you to check to see if this Team object has the same
* name as another Team object
*
* @param otherTeam
* one team
*/
public boolean sameName(Team otherTeam) {
return name.equals(otherTeam.name);
}
/**
* This method allows you to check to see if this Team object has the same
* name as another Team object
*
* @param team1
* one team
* @param team2
* the other team
*/
public static boolean sameName(Team team1, Team team2) {
return team1.name.equals(team2.name);
}
/**
* This method allows you to sort an ArrayList of Team items using
* Collections.sort
*
* @param o
* the other Team being compared to.
* @return -1 if this Team item should come first, +1 if this Team item
* should come after the other, and 0 if this Team item is
* equivalent to the other.
*/
@Override
public int compareTo(Team o) {
if (this.winCount < o.winCount) {
return -1;
} else if (this.winCount > o.winCount) {
return 1;
}
return 0;
}
}
Explanation / Answer
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks
Note: I didn’t understand what you mean by “Correctly use the "Prime the Read" “, I have defined a method to get input from the user, if you are looking for some other methods which you have, just let me know.
// Team.java
public class Team implements Comparable<Team> {
/**
*
* This is the method you will fix to allow you to return the contents of
*
* this Team item in the nice format: "name: winCount". The current
*
* implementation will allow the program to compile, but it returns nothing.
*
*
*
* For example: "Sooners: 3"
*
*
*
* @return the formatted String that can then be output to the console.
*/
public String toString() {
return name + ": " + winCount;
}
// _-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_
// Data fields
private String name;
private int winCount;
// Constructors ---------------------------------------
/*
*
* I have implemented many constructors to accommodate all of the different
*
* design choices. You will likely only use one of these in your code.
*/
Team() {
name = "Sooners";
winCount = 1;
}
Team(String inputName) {
name = inputName;
winCount = 1;
}
Team(String inputName, int inputWinCount) {
name = inputName;
winCount = inputWinCount;
}
// ----------------------------------------------------
// Getters and Setters (aka Accessors and Mutators) ===
/**
*
* @return the name
*/
public String getName() {
return name;
}
/**
*
* @param name
*
* the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
*
* @return the winCount
*/
public int getWinCount() {
return winCount;
}
/**
*
* @param winCount
*
* the winCount to set
*/
public void setWinCount(int winCount) {
this.winCount = winCount;
}
/**
*
* Increments the winCount variable by one for this Team
*/
public void incrementWinCount() {
winCount++;
}
/**
*
* This method allows you to check to see if this Team object has the same
*
* name as another Team object.
*
*
*
* This method allows you to use the contains method in ArrayList to see
*
* if any element in an array list has the same name as a specific Team.
*
*
*
* @param o
*
* the other Team being compared to.
*/
@Override
public boolean equals(Object o) {
return name.equals(((Team) o).name);
}
/**
*
* This method allows you to check to see if this Team object has the same
* name as another Team object
*
* @param otherTeam
* one team
*/
public boolean sameName(Team otherTeam) {
return name.equals(otherTeam.name);
}
/**
*
* This method allows you to check to see if this Team object has the same
*
* name as another Team object
*
* @param team1
* one team
* @param team2
* the other team
*/
public static boolean sameName(Team team1, Team team2) {
return team1.name.equals(team2.name);
}
/**
*
* This method allows you to sort an ArrayList of Team items using
* Collections.sort
*
* @param o
*
* the other Team being compared to.
*
* @return -1 if this Team item should come first, +1 if this Team item
*
* should come after the other, and 0 if this Team item is
*
* equivalent to the other.
*/
@Override
public int compareTo(Team o) {
if (this.winCount < o.winCount) {
return -1;
} else if (this.winCount > o.winCount) {
return 1;
}
return 0;
}
}
//Test.java (with main method)
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Test {
static Scanner scanner = new Scanner(System.in);
/**
* method to prompt the user to enter a team name and return it
*/
static String getInput() {
System.out.print("Which team just won (x to exit): ");
String input = scanner.nextLine();
return input;
}
public static void main(String[] args) {
/**
* defining an array list of teams
*/
ArrayList<Team> teamList = new ArrayList<Team>();
String input = "";
/**
* looping until user wishes to quit
*/
do {
/**
* getting team name
*/
input = getInput();
if (!input.equalsIgnoreCase("x")) {
/**
* creating a Team object
*/
Team team = new Team(input);
/**
* if the team is already on the list, incrementing win count
*/
if (teamList.contains(team)) {
teamList.get(teamList.indexOf(team)).incrementWinCount();
} else {
/**
* doesnt exist, so adding the team to the list
*/
teamList.add(team);
}
}
} while (!input.equalsIgnoreCase("x"));
/**
* checking if team list size is 0
*/
if (teamList.size() == 0) {
System.out.println("No data input");
} else {
/**
* sorting by win count (ascending order)
*/
Collections.sort(teamList);
System.out.println("Final Tally:");
/**
* displaying teams based on wincount
*/
for (Team t : teamList) {
System.out.println(t);
}
/**
* winning team will be the last team on the sorted list
*/
Team winner = teamList.get(teamList.size() - 1);
System.out.println("The winner is " + winner.getName() + " with "
+ winner.getWinCount() + " win(s)");
}
}
}
/*OUTPUT*/
Which team just won (x to exit): Sooners
Which team just won (x to exit): Bears
Which team just won (x to exit): Bears
Which team just won (x to exit): Sooners
Which team just won (x to exit): Bears
Which team just won (x to exit): Predators
Which team just won (x to exit): x
Final Tally:
Predators: 1
Sooners: 2
Bears: 3
The winner is Bears with 3 win(s)