Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Expand
titleDel 1 - VoterCounter(10 %)


Expand
titleSkjelett til del 1


Code Block
languagejava
package del1;

public class VoterCounter {

	// Add any needed fields here
	public static final String TIE_RESULT = "TIE";

	/**
	 * Register a candidate to the poll
	 * 
	 * @param candidate the new candidate to add
	 */
	public void addCandidate(String candidate) {
		// TODO
	}

	/**
	 * Vote on a given candidate
	 * 
	 * @param candidate the candidate to vote on
	 * 
	 * @throws IllegalArgumentException if the candidate is not registered
	 */
	public void castVote(String candidate) {
		// TODO
	}

	/**
	 * Prints all the results in an arbitrary order. The results should be on the
	 * format {candidate name}-{number of votes for the candidate} with each
	 * candidate on a new line
	 * 
	 * Example output with two candidates, "Candidate1" with 7 votes, and
	 * "Candidate2" with 6 votes
	 * 
	 * Candidate1-7 
	 * Candidate2-6
	 */
	public String getFormattedResults() {
		// TODO
		return null;
	}

	/**
	 * Returns the number of votes a candidate has received
	 * 
	 * @param candidate the candidate to get number of votes for
	 * @return the number of votes the candidate has received. Returns null if the
	 *         candidate is not registered
	 */
	public Integer getNumberOfVotes(String candidate) {
		// TODO
		return 0;
	}

	/**
	 *
	 * @return the name of the candidate who won the election. If two or more
	 *         candidates got the same number of maximum votes, return the
	 *         TIE_RESULT field. Return null if there are no candidates.
	 */
	public String getWinner() {
		// TODO
		return null;
	}

	public static void main(String[] args) {
		String candidate1 = "CANDIDATE1";
		String candidate2 = "CANDIDATE2";
		VoterCounter counter = new VoterCounter();
		counter.addCandidate(candidate1);
		counter.addCandidate(candidate2);
		// Should print 0
		System.out.println(counter.getNumberOfVotes(candidate1));
		counter.castVote(candidate1);
		// Should print 1;
		System.out.println(counter.getNumberOfVotes(candidate1));
		// Should print CANDIDATE 1
		System.out.println(counter.getWinner());
		counter.castVote(candidate2);
		// Should print TIE
		System.out.println(counter.getWinner());
	}
}


Utfyll klassen VoterCounter, og alle metodene i denne klassen. Legg til egne nødvendige felt. VoterCounter skal ha oversikt over kandidater som stiller til valg, og antall stemmer de har fått.

  • addCandidate(String candidateName) - Registrerer en kandidat en kan stemme på
  • castVote(String candidate) - Gir en stemme til kandidaten
  • getFormattedResults() - Returnerer antall stemmer hver kandidat har fått
  • getVotes(String candidate) - Returnerer antall stemmer en kandidat har fått
  • getWinner() - Returnerer kandidaten som fikk flest stemmer, eventuelt om det ble uavgjort


Expand
titleLF


Code Block
public class VaccineTrialVolunteer {

	private final String id;
	private final boolean placebo; 
	private boolean gotSick;
	
	public VaccineTrialVolunteer(String id, boolean placebo) {
		this.id = id;
		this.placebo = placebo;
	}

	public String getId() {
		return this.id;
	}

	/* Whether the volunteer was given a placebo or the actual vaccine */
	public boolean isPlacebo() {
		return this.placebo;
	}

	/* Whether the volunteer got sick during the trial period, 
	 * the default value for this should be false */
	public boolean gotSick() {
		return this.gotSick;
	}

	/*
	 * Updates whether the participant got sick during the trial period
	 */
	public void setGotSick(boolean gotSick) {
		this.gotSick = gotSick;
	}

}
Expand
titleLF
Code Block
languagejava
package del1;

import java.util.HashMap;
import java.util.Map;

public class VoterCounter {

	private Map<String, Integer> votes = new HashMap<>();
	public static final String TIE_RESULT = "TIE";

	/**
	 * Register a candidate to the poll
	 * 
	 * @param candidate the new candidate to add
	 */
	public void addCandidate(String candidate) {
		votes.put(candidate, 0);

	}

	/**
	 * Vote on a given candidate
	 * 
	 * @param candidate the candidate to vote on
	 * 
	 * @throws IllegalArgumentException if the candidate is not registered
	 */
	public void castVote(String candidate) {
		if (!this.votes.containsKey(candidate)) {
			throw new IllegalArgumentException();
		}
		votes.replace(candidate, votes.get(candidate) + 1);
	}

	/**
	 * Prints all the results in an arbitrary order. The results should be on the
	 * format {candidate name}-{number of votes for the candidate} with each
	 * candidate on a new line
	 */
	public String getFormattedResults() {
		String s = "";
		for (String candidate : votes.keySet()) {
			s += (candidate + "-" + votes.get(candidate)) + "\n";
		}
		return s;
	}

	/**
	 * Get's the number of votes a candidate has received
	 * 
	 * @param candidate the candidate to get number of votes for
	 * @return the number of votes the candidate has received. null if the candidate
	 *         is not registered
	 */

	public Integer getNumberOfVotes(String candidate) {
		return votes.getOrDefault(candidate, null);
	}

	/**
	 *
	 * @return the name of the candidate who won the election. If two or more
	 *         candidates got the same number of maximum votes, return the
	 *         TIE_RESULT field. Return null if there is no candidates.
	 */

	public String getWinner() {
		int maxValue = 0;
		String winner = null;
		for (String candidate : votes.keySet()) {
			if (votes.get(candidate) > maxValue) {
				winner = candidate;
				maxValue = votes.get(candidate);
			}
		}
		for (String candidate : votes.keySet()) {
			if (votes.get(candidate) == maxValue && !winner.equals(candidate)) {
				return TIE_RESULT;
			}
		}
		return winner;
	}

	public static void main(String[] args) {
		String candidate1 = "CANDIDATE1";
		String candidate2 = "CANDIDATE2";
		VoterCounter counter = new VoterCounter();
		counter.addCandidate(candidate1);
		counter.addCandidate(candidate2);
		// Should print 0
		System.out.println(counter.getNumberOfVotes(candidate1));
		counter.castVote(candidate1);
		// Should print 1;
		System.out.println(counter.getNumberOfVotes(candidate1));
		// Should print CANDIDATE 1
		System.out.println(counter.getWinner());
		counter.castVote(candidate2);
		// Should print TIE
		System.out.println(counter.getWinner());
	}
}




...