java - 如何从具有数组列表的类调用另一个类的方法?

标签 java methods arraylist bluej

我正在学习 Java 的第一学期,我需要帮助从下面的 VotingMachine 类调用 Candidate 类的方法。投票机类已正确编译。感谢大家提供的任何帮助...... 梅赛德斯

import java.util.ArrayList;

/**
 * These are the fields for the Voting Machine Class.
 */
public class VotingMachine
{
    private ArrayList<String> candidateList;

    /**
     * The following constructor will establish the Candidate List
     */
    public VotingMachine()
    {
        candidateList = new ArrayList<String>();
    }

    /**
     * This constructor will store the Candidates for the Candidate List   
     */
    public void setCandidateList()
    {
        candidateList.add("Darnell Woffard");
        candidateList.add("Barack Obama");
        candidateList.add("Hillary Clinton");
    }    

    /**
     * This method will display the entire Candidate List.
     */
    public void printCandidateInfo()
    {
        for (int index=0; index < candidateList.size(); index++)
        {
            System.out.println(candidateList.get(index));
        }
    }

    /**
     * Method to the number of Candidates in the CandidateList Arraylist.
     */
    public int getNumberofFiles()
    {
        return candidateList.size();       
    }

   /**
    * Method to select one candidate by first providing an index number.
    */
   public void listFile(int index)
   {
       if(index >= 0 && index < candidateList.size()){
           String filename = candidateList.get(index);
           System.out.println(filename);
       }
   }

    /**
     * This method will enable a user to remove a candidate.
     */
    public void removeFile(int index)
    {
        if(index >= 0 && index < candidateList.size()){
            candidateList.remove(index);
        }
    }

    /**
     * This method will add a file to the Candidate List.
     * 
     */
    public void addCandidate(String filename)
    {
       candidateList.add(filename);
    }


//----------
//The Candidate Class:

public class Candidate{

    private String name;
    private char party;
    private String candidateList;
// Add fields
    /**
     * Fields
     * name - Candidate's name, stored in a String
     * party - Candidate's political party, stored in a char
     * as 'r' for Republican, 'd' for Democrat, and 'i' for Independent
     */

    /**
     * Constructor
     * 
     * @param anyName - caller inputs Candidate name
     * @param anyParty - caller inputs Candidate's party affiliation
     * stored as a char
     * chars are assigned with single quotes.
     */
    public Candidate(String anyName, char anyParty)
    {
        name = anyName;
        party = anyParty;   
    }

    /**
     * The method will enable method calls from the Voting Machine Class.
     */
    public void main(String candidateList)
    {
        VotingMachine votingMachine = new VotingMachine();
    }

            /**
             * This method will define the candidates party affiliation.
             * public char setParty()
             */


//Complete the three methods and their comments.    
    /**
     * Method to retrieve the Candidate's name for the caller.
     * public String getName(String anyName)
     * 
     */



    /**
     * Method to retrieve the Candidate's party for the caller.
     * 
     * @return
     */



    /**
     * Method to change the Candidate's party
     * 
     * @param 
     */

最佳答案

实际上我从中得到的是你正在尝试制作一台投票机。 VotingMachine 是这里的主要类,包含不同候选人的信息。所以我们将在votingMachine 类中创建candidate 对象。注意:当我们要创建一个java项目时,弄清楚它的主类和子类是什么,这意味着哪个依赖于哪个。在上面的例子中,类中有关联。首先声明一个ArrayList用于存储候选类的对象。如下所示。

private ArrayList<candidate> candidateList;

/**
 * The following constructor will establish the Candidate List
 */
public VotingMachine()
{
    candidateList = new ArrayList<String>();
}

现在为了在 ArrayList 中添加新的候选者,我已经修改了你的方法 setCandidate() 作为

public void addNewCandidate(String name, char partySymbol)
{
    candidate candid = new candidate(name, partySymbol);// this will call the candidate constructor
    candidateList.add(candid);//add that object in ArrayList


}    

由于ArrayList存储对象的引用,内置函数int get(int index)将返回对象的引用。要打印该对象的信息或者您可以说值,我们应该将函数定义为 getName()getParty() 。而不是这个System.out.println(candidateList.get(index));你应该打电话System.out.println(candidateList.get(index).getName());System.out.println(candidateList.get(index).getParty());在下面的方法中

public void printCandidateInfo()
{
    for (int index=0; index < candidateList.size(); index++)
    {
        System.out.println(candidateList.get(index));
    }
}

因此将候选类中的函数定义为

public String getName()
{
    return name;

}

/**
 * Method to retrieve the Candidate's party for the caller.
 * 
 * @return
 */
public char getParty()
{
    return party;

}

以下方法将打印引用信息而不是候选人信息,因此请按上述修改

public void listFile(int index)
{
      if(index >= 0 && index < candidateList.size()){
       String filename = candidateList.get(index);
       System.out.println(filename);
   }

}

因为我已经修改了它,

import java.util.ArrayList;

/**
* These are the fields for the Voting Machine Class.
*/

public class VotingMachine
{
private ArrayList<Candidate> candidateList;

/**
 * The following constructor will establish the Candidate List
 */
public VotingMachine()
{
    candidateList = new ArrayList<>();
}

/**
 * This method will store the Candidates for the Candidate List   
 */
public void addNewCandidate(String name, char partySymbol)
{
    Candidate candid = new Candidate(name, partySymbol);// this will call the candidate constructor
    candidateList.add(candid);//add that object in ArrayList
}    

/**
 * This method will display the entire Candidate List.
 */
public void printCandidateInfo()
{
    for (int index=0; index < candidateList.size(); index++)
    {
        System.out.print(candidateList.get(index).getName());
        System.out.println("  " + candidateList.get(index).getParty());
    }
}

/**
 * Method to the number of Candidates in the CandidateList Arraylist.
 */
public int getNumberofFiles()
{
    return candidateList.size();       
}

/**
* Method to select one candidate by first providing an index number.
*/
public void listFile(int index)
{
   System.out.print(candidateList.get(index).getName());
   System.out.println("  " + candidateList.get(index).getParty());
}

/**
 * This method will enable a user to remove a candidate.
 */
public void removeFile(int index)
{
    if(index >= 0 && index < candidateList.size()){
        candidateList.remove(index);
    }
}

}

在候选类中,我刚刚添加了上述getName()getParty()方法..

问候

关于java - 如何从具有数组列表的类调用另一个类的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20462267/

相关文章:

javascript - var functionName = function() {} vs function functionName() {}

java - 具体如何从ArrayList中提取元素呢?

java - 转换为分数

java - 比较两个 ArrayLists - 一个作为另一个的子集

java - 按某个键对 ArrayList<HashMap<String, String>> 进行分组

java - 检查链接的 URL 状态代码时无法将 HttpResponseCode 错误解析为类型

java - 上传我们游戏代码的网站

Java Instant Messenger 问题

java - 开发主要通过 Spring 和 Struts 调用存储过程的应用程序的最佳方法

c# - 多个 Controller 之间的通用方法