【问题标题】:Created a method to calculate Team with most wins and winning percentage创建了一种计算获胜次数最多的团队和获胜百分比的方法
【发布时间】:2014-04-13 22:06:01
【问题描述】:

我正在第二学期的 Java 课程中学习如何使用链接列表。对于我们的程序,我们应该编写一个类来封装足球队输赢的概念。

我需要帮助的部分是创建一个方法,该方法返回获胜次数最多的 3 支球队的昵称,另一个根据获胜百分比返回 5 支最佳球队的昵称。

团队课

public class Team
{
    private int GamesWon;
    private int GamesLost;
    private String Team;

    public Team (int i, int j, String k)
    {
        GamesWon = i;
        GamesLost = j;
        Team = k;
    }

    public int getGamesWon()
    {
        return GamesWon;
    }
    public int getGamesLost()
    {
        return GamesLost;
    }
    public String getTeam()
    {
        return Team;
    }
    public void setGamesWon(int i)
    {
        GamesWon = i;
    }
    public void setGamesLost(int j)
    {
        GamesLost = j;
    }
    public void setTeam(String k)
    {
        Team = k;
    }
    public String toString()
    {
        return("Team: " + Team + "\tGamesWon: " + GamesWon + "\tGamesLost: " + GamesLost);
    }
}

团队节点类

public class TeamNode 
{
    private Team team;
    private TeamNode next;

public TeamNode()
{
    team = null;
    next = null;
}

public TeamNode(Team t)
{
    setTeam(t);
    next = null;
}


public Team getTeam()
{
    return team;
}

public TeamNode getNext()
{
    return next;
}

public void setTeam(Team t)
{
    team = t;
}

public void setNext(TeamNode tn)
{
    next = tn;
}



}

ShellLinkedList

public abstract class ShellLinkedList
{
    protected TeamNode head;
    protected int numberOfItems;

public ShellLinkedList()
{
    head = null;
    numberOfItems = 0;
}

public int getNumberOfItems()
{
    return numberOfItems;
}

public boolean isEmpty()
{
    return (numberOfItems == 0);
}
public String toString()
{
    String listString = "";
    TeamNode current = head;
    while (current != null)
    {
        listString += current.getTeam().toString() + "\n";
    current = current.getNext();
    }
    return listString;
}

}

DataStructureExcetion(错误类)

public class DataStructureException extends Exception
{
public DataStructureException(String s)
{
    super(s);
}
}

团队链表类

public class TeamLinkedList extends ShellLinkedList
{
public TeamLinkedList()
{
    super();
}
public void insert(Team t)
{
    TeamNode tn = new TeamNode(t);
    tn.setNext(head);
    head = tn;
    numberOfItems++;
}
public Team delete(String searchName)
                        throws DataStructureException
{
    TeamNode current = head;
    TeamNode previous = null;
    while (current != null && current.getTeam().getTeam() != searchName)
    {
        previous = current;
        current = current.getNext();
    }

    if (current == null)
        throw new DataStructureException(searchName + " not found: cannot be deleted");
    else
    {
        if (current == head)
            head = head.getNext();
        else
            previous.setNext(current.getNext());
            numberOfItems--;
        return current.getTeam();
    }
}
}

客户端类

public class TeamLinkedListTest 
{
    public static void main(String [] args)
    {

        Team t1 = new Team (10, 0, "Patriots" );
        Team t2 = new Team (9, 1, "49ners");
        Team t3 = new Team (8, 2, "Chargers");
        Team t4 = new Team (7, 3, "Bengals");
        Team t5 = new Team (6, 4,"Jets");
        Team t6 = new Team (5, 5, "Raiders");
        Team t7 = new Team (4, 6, "Seahawks");
        Team t8 = new Team (3, 7, "Cardinals");
        Team t9 = new Team (2, 8, "Texans");
        Team t10 = new Team(1, 9, "Dolphins");

        TeamLinkedList Team = new TeamLinkedList();
        System.out.println("Number of items in the list: " + Team.getNumberOfItems() + "\n" + Team.toString());

        Team.insert(t1);
        System.out.println("Number of items in the list: " + Team.getNumberOfItems() + "\n" + Team.toString());

        Team.insert(t2);
        System.out.println("Number of items in the list: " + Team.getNumberOfItems() + "\n" + Team.toString());

        Team.insert(t3);
        System.out.println("Number of items in the list: " + Team.getNumberOfItems() + "\n" + Team.toString());

        Team temp;

        try
        {
            temp = Team.delete("Bengals");
            System.out.println("Team deleted: " + temp);
        }
        catch(DataStructureException dse1)
        {
            System.out.println(dse1.getMessage() + "\n");
        }

        try
        {
            temp = Team.delete("Patriots");
            System.out.println("Deleted " + temp);
            System.out.println("Number of items in the list: " + Team.getNumberOfItems() + "\n" + Team.toString());

            temp = Team.delete("49ners");
            System.out.println("Deleted " + temp);
            System.out.println("Number of items in the list: " + Team.getNumberOfItems() + "\n" + Team.toString());

            temp = Team.delete("Chargers");
            System.out.println("Deleted " + temp);
            System.out.println("Number of items in the list: " + Team.getNumberOfItems() + "\n" + Team.toString());

            temp = Team.delete("Patriots");
            System.out.println("Deleted " + temp);
            System.out.println("Number of items in the list: " + Team.getNumberOfItems() + "\n" + Team.toString());
        }
        catch(DataStructureException dse2)
        {
            System.out.println(dse2.getMessage());
        }

    }
}

【问题讨论】:

  • 你有什么问题?
  • 我需要帮助的部分是创建一个方法,该方法返回获胜次数最多的 3 支球队的昵称,另一个根据获胜百分比返回 5 支最佳球队的昵称。
  • 我不知道如何编写这些方法
  • 请删除所有对回答您的问题不重要的代码。要回答,您只需要查看您的链表,并拥有一个数组列表或等效数据结构来存储最佳团队。您试图回答什么问题?
  • @JeremyD 我不知道从哪里开始,因为我说过我是 LinkedLists 的新手。我应该在哪个类中创建这个方法?

标签: java class linked-list


【解决方案1】:

简短的回答。你可以使用Collections.sort

长(呃)答案。在您的 Team 类中,您必须重写 compareTo 方法

class Team implements Comparable<Team> {
private gamesWon

//contructor/methods go here

@Override
public int compareTo(Team t) {
    int wins = t.gamesWon;
    if (this.gamesWon > wins) {
        return 1;
    } else if (this.gamesWon == wins) {
        return 0;
    } else {
        return -1;
    }
}

之后你像这样在你的链表上调用排序方法 Collections.sort(myLinkedList)

最后你可以打印出前三个节点,因为它们现在已经排序了

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-22
    • 1970-01-01
    • 2019-07-10
    • 1970-01-01
    • 2019-06-06
    • 2020-06-27
    • 2017-10-09
    相关资源
    最近更新 更多