2013-12-09 25 views
0

我在Java的第一個學期,我需要幫助從下面的VotingMachine類調用方法到候選類。投票機類正在編譯。感謝大家的幫助,您可以提供.... 奔馳如何從帶有數組列表的類調用另一個類的方法?

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

大量的工作在這一個xD 首先:setCandidateList從不被調用 - 它應該從構造函數中調用。爲了將來的參考:如果它不是setter/getter模式的setter(不過你可以在以後學習,也可以在什麼時候使用,何時不使用:))。第二:看起來你不應該存儲候選人的名字,而是實例化候選人並將他們放入後備列表private ArrayList candidateList;因此,重新構造您的構造函數以接受分隔符上的'candidateList'拆分,構造'Candidate's – chzbrgla

回答

1

其實我從這個得到的是你正試圖使投票機。投票機是這裏的主要課程,有不同的候選人的信息。所以我們會把候選人作爲投票機的對象。注意:當我們要製作一個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()方法..

問候

相關問題