简体   繁体   English

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

[英]How do I call methods from a class with an arraylist to another class?

I'm in my first semester of Java and I need help in calling methods from the VotingMachine class below to the Candidate Class. 我上Java的第一学期,需要帮助来调用从下面的VotingMachine类到Candidate类的方法。 The Voting Machine class is compiling properly. 投票机类正在正确编译。 Thank you all for any help you can provide.... Mercedes 谢谢大家提供的任何帮助。

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 
     */

Actually what i got from this is you are trying to make a voting machine. 实际上,我从中得到的是您正在尝试制造投票机。 VotingMachine is the main class here having info of different candidates. 投票机是这里的主要班级,拥有不同候选人的信息。 so we will make object of candidate in votingMachine class. 因此,我们将在votingMachine类中成为候选人的对象。 Note: when we are supposed to make a java project, figure out what is it main class and subclass that means which depends on which. 注意:当我们应该创建一个Java项目时,请弄清它是什么主类和子类,这意味着哪个依赖于哪个。 in the above example There is association in the classes. 在上面的示例中,类中存在关联。 First of all declare an ArrayList for storing objects of candidate class. 首先声明一个ArrayList用于存储候选类的对象。 as shown below. 如下所示。

private ArrayList<candidate> candidateList;

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

now for adding new candidate in the ArrayList I have modified your method setCandidate() as 现在为在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


}    

As ArrayList stores references of objects, the built-in function int get(int index) will return the reference of the object. 当ArrayList存储对象的引用时,内置函数int get(int index)将返回对象的引用。 to print the info of that object or you can say values, we should define a function as getName() and getParty() . 要打印该对象的信息或可以说出值,我们应该将一个函数定义为getName()getParty() instead of this System.out.println(candidateList.get(index)); 而不是这个System.out.println(candidateList.get(index)); you should call System.out.println(candidateList.get(index).getName()); 您应该调用System.out.println(candidateList.get(index).getName()); and System.out.println(candidateList.get(index).getParty()); System.out.println(candidateList.get(index).getParty()); in the following method 在以下方法中

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

so define functions in candidate class as 所以在候选类中定义函数为

public String getName()
{
    return name;

}

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

}

the following method will print the reference not the info of candidate, so modify it as described above 以下方法将打印参考而不是候选者的信息,因此如上所述进行修改

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

} }

as i have modified it, 当我修改它时,

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);
    }
}

} }

in candidate class i have just added the above mentioned getName() and getParty() methods.. 在候选类中,我刚刚添加了上面提到的getName()getParty()方法。

regards 问候

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从另一个类调用 ArrayList 方法? - How do I call an ArrayList method from another class? 在 Java 中调用另一个类中的 ArrayList 的方法 - Call for methods of an ArrayList in another class in Java 您如何从另一个类中调用另一个类中创建的类实例的方法? - How do you call the methods of a class instance that was created in another class, from another class? 我们能否将 Arraylist 从 class 调用到另一个 class,并且可以被“另一个”class 中的所有方法使用? 无 static - Java - Can we call a Arraylist from a class to another class, and can be used by all methods in the 'another' class? No static - Java 如何在Java中将数组列表从一个类复制到另一个类? - How do I copy an arraylist from one class to another in Java? 如何从另一个类访问这个 ArrayList? - How do I access this ArrayList from another class? 如何将对象从 ArrayList 移动到另一个类中的 RecyclerView? - How do I move Objects from an ArrayList to a RecyclerView in another Class? 如何从一个类的一个arrayList到另一个类的arraylist获取一定数量的对象? - How do I get a certain number of objects from one arrayList in one class to an arraylist in another class? Java:如何在静态方法中创建对象,还从另一个类调用方法? - Java: How do i create objects in a static method and also call for methods from another class? 如何在android中将arraylist变量从一个类调用到另一个类 - How to call the arraylist variable from one class to another class in android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM