Objectives Find partners for the class project. Practice implementing the Compar
ID: 3607517 • Letter: O
Question
Objectives Find partners for the class project. Practice implementing the Comparable interface, implementing abstract methods and extending abstract classes Attendance Quiz Please log on to culearn using one of the computer in the tutorial room and complete the attendance quiz. You can only access the quiz if you log in using one of the computers in the lab. You cannot use a laptop for this. This is a time limited quiz Be sure to do this as soon as you arrive. At the end of the tutorial a TA will assign you a grade, call it G, which is 0, 1 or 2, depending on the progress you make during the tutorial. If you spend your time reading mail and chatting with your friends you will receive 0. If you have completed the attendance quiz on time then G will be your tutorial grade. If you have not completed the attendance quiz on time, then your tutorial grade will be maxo, G-1/2). Each tutorial grade will therefore be one of 0, 0.5, 15 or 2 Project Teams You must form a team of four (4) students from your tutorial section Some teams of three will be allowed if the number of students in the tutorial is not a multiple of 4 Starting in tutorial 7, tutorials will be done as a team. Comparable Warm-up Create the Box class as defined by pablic elass Box implements ConparablecBex> Each box object should have a String attribute and boxes should be compared by the length of these Strings Override the toString method (inherited from Object) to simply display object's string attribute. A sample testing program is provided. It creates an aray of boxes, prints them, sorts them and then prints them again. The testing program should output before sorting [kItten, cat, dog, oh cow oxen] after sorting oh, cat, dog, cow,xen, kitten Change the strings array to be sure your code works cormectly with different inputsExplanation / Answer
Hello. I have an answer for you. But I should tell you the question lacks specification. In the question, it is asked to complete the ‘provided’ Team class. But the team class was not provided. But I understood that you want to implement the working of Comparable interface. So I have created a Team class according to my understanding
//Team.java
public class Team implements Comparable<Team> {
String name;
private int wins;
private int losses;
private int draws;
private int points;
public Team(String name,int wins,int losses,int draws) {
/**
* parameterized constructor
*/
this.name=name;
this.wins=wins;
this.losses=losses;
this.draws=draws;
points=(wins*2)+draws;
}
public Team() {
/**
* default constructor
*/
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getWins() {
return wins;
}
public void setWins(int wins) {
this.wins = wins;
}
public int getLosses() {
return losses;
}
public void setLosses(int losses) {
this.losses = losses;
}
public int getDraws() {
return draws;
}
public void setDraws(int draws) {
this.draws = draws;
}
public int getPoints() {
return points;
}
public int getGamesPlayed() {
return wins+losses+draws;
}
public void calculatePoints() {
points=(2*wins)+draws;
}
/**
* the following method compares two objects
*/
public int compareTo(Team t) {
if(this.getPoints()>t.getPoints()){
/**
* current team has more points than t
*/
return -1;
}else if(this.getPoints()<t.getPoints()){
/**
* current team has less points than t
*/
return 1;
} else{
/**
* equal points for both
*/
if(this.getGamesPlayed()>t.getGamesPlayed()){
/**
* current team has played more games than t
*/
return 1;
}else if(this.getGamesPlayed()<t.getGamesPlayed()){
/**
* current team has played less games than t
*/
return -1;
}else{
/**
* both teams have played same number of games
*/
if(this.getWins()>t.getWins()){
/**
* current team has more wins than t
*/
return -1;
}else if(this.getWins()<t.getWins()){
/**
* current team has less wins than t
*/
return 1;
}else{
/**
* both teams have same number of points, same number of games and same number of wins
*/
return 0;
}
}
}
}
@Override
public String toString() {
return name+"["+points+"]: "+wins+"/"+losses+"/"+draws;
}
}
//Test.java
import java.util.ArrayList;
import java.util.Collections;
public class Test {
public static void main(String[] args) {
/**
* defining some Team objects
*/
Team team1=new Team("Warriers",1, 2, 1);
Team team2=new Team("Red Bulls",5, 2, 0);
Team team3=new Team("Riders",3, 0, 0);
Team team4=new Team("Champs",1, 1, 1);
Team team5=new Team("Diamonds",5, 5, 2);
Team team6=new Team("Miners",4, 2, 1);
Team team7=new Team("Mutants",0, 3, 3);
Team team8=new Team("Uprise",2, 0, 1);
/**
* creating an array list and adding teams to it
*/
ArrayList<Team> list=new ArrayList<Team>();
list.add(team1);
list.add(team2);
list.add(team3);
list.add(team4);
list.add(team5);
list.add(team6);
list.add(team7);
list.add(team8);
/**
* sorting the list as per the requirement
*/
Collections.sort(list);
/**
* printing the team details in sorted order
*/
for (Team team : list) {
System.out.println(team.toString());
}
}
}
/*output*/
Diamonds[12]: 5/5/2
Red Bulls[10]: 5/2/0
Miners[9]: 4/2/1
Riders[6]: 3/0/0
Uprise[5]: 2/0/1
Champs[3]: 1/1/1
Warriers[3]: 1/2/1
Mutants[3]: 0/3/3
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.