gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 13:44:19 26 4
gpt4 key购买 nike

我正在学习 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com