Syntax Killer Syntax Killer - 1 year ago 64
Java Question

how can I improve my code for this task

I am newbie to object orientated programming and trying to construct something which resembles a basic vote counter which should take an int parameter that represents a choice of two candidates and print the election results to the terminal window. albeit (the votes attributable to each candidate and the total votes cast)

The method I am looking for should also return a string that gives information on the success or failure of casting the vote.”your vote has been cast” “invalid choice, no vote cast"

I have created a class and the constructors and also implemented some basic get methods.

I am wondering how I should go about achieving this objective albeit through a conditional statement or using some sort of advanced method.

any help in terms of the syntax or wider approach would be appreciated.

public class VoteCounter {
private String candidate1;
private String candidate2;
private int candidate1Votes;
private int candidate2Votes;
private boolean completed;

public VoteCounter(String candidate1, String candidate2) {
this.candidate1 = candidate1;
this.candidate2 = candidate2;
this.candidate1Votes = 0;
this.candidate2Votes = 0;
this.completed = false;

public VoteCounter() {

public String getCandidate1 () {
return this.candidate1;

public String getCandidate2 () {
return this.candidate2;

public Boolean getCompleted () {
return this.completed;

public void setCompleted (boolean completed) {
this.completed = completed;

Answer Source

Something like this?

private String vote(int choice)
  if(choice == 1)
  else if(choice == 2)
    return "invalid choice, no vote cast";
  return "your vote has been cast";
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download