In: Computer Science
Create a class Team to hold data about a college sports team. The Team class holds data fields for college name (such as Hampton College), sport (such as Soccer), and team name (such as Tigers). Include a constructor that takes parameters for each field, and get methods that return the values of the fields. Also include a public final static String named MOTTO and initialize it to Sportsmanship! Save the class in Team.java. Create a UML class diagram as well.
Write an application named TestTeam with the main method which declares and instantiates three Team objects with different values for their fields. Accept field values as a user input (utilizing JOptionPane) for each of team fields and assign those to proper instances of Team class (three sets of three input strings will be needed to fill all fields in each of three Team objects). Add a displayTeams method which displays all the data, including the motto, for each instantiated Team objects. When doing so use JOptionPane again and format the output in orderly fashion, one team data per a separate line. Save the class as TestTeam .java. Create a UML class diagram as well.
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 or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
// Team.java
public class Team {
// attributes and constants
private String collegeName;
private String sport;
private String teamName;
public final static String MOTTO = "Sportsmanship!";
// constructor taking values for all fields
public Team(String collegeName, String sport, String teamName) {
this.collegeName = collegeName;
this.sport = sport;
this.teamName = teamName;
}
// getter methods
public String getCollegeName() {
return collegeName;
}
public String getSport() {
return sport;
}
public String getTeamName() {
return teamName;
}
}
// TestTeam.java
import javax.swing.JOptionPane;
public class TestTeam {
// method to display details of an array of teams
static void displayTeams(Team teams[]) {
String data = "";
// appending all team info to a string
for (int i = 0; i < teams.length; i++) {
data += "College Name: " + teams[i].getCollegeName() + ", Sport: "
+ teams[i].getSport() + ", Team Name: "
+ teams[i].getTeamName() + ", MOTTO: " + teams[i].MOTTO
/* note: MOTTO can be called by Team.MOTTO or teams[i].MOTTO*/
+ "\n";
}
// displaying using JOptionPane window
JOptionPane.showMessageDialog(null, data.trim());
}
public static void main(String[] args) {
//creating an array of 3 Teams
Team teams[] = new Team[3];
//looping and filling with 3 Team objects
for (int i = 0; i < teams.length; i++) {
//getting inputs for current team
String college = JOptionPane
.showInputDialog("Enter college name for team " + (i + 1));
String sport = JOptionPane.showInputDialog("Enter sport for team "
+ (i + 1));
String teamName = JOptionPane
.showInputDialog("Enter team name for team " + (i + 1));
//creating a team and adding to array
teams[i] = new Team(college, sport, teamName);
}
//displaying teams
displayTeams(teams);
}
}
/*UML , INPUT&OUTPUT*/