District

class District(val name: String, val seats: Int, val candidates: Vector[Candidate])

The class District represents electoral districts (Finnish: vaalipiiri). Each district has a certain number of seats for which a (larger) number of candidates compete in an election. Each district has its own candidates.

A district object is immutable.

Value parameters:
candidates

the candidates vying for the seats in the district; there is always at least one candidate per district

name

the name of the electoral district

seats

the number of seats (elected positions) available

class Object
trait Matchable
class Any

Value members

Concrete methods

NOTE TO STUDENTS: THIS METHOD IS INTENDED FOR IMPLEMENTATION ONLY IN CHAPTER 10.1!

NOTE TO STUDENTS: THIS METHOD IS INTENDED FOR IMPLEMENTATION ONLY IN CHAPTER 10.1!

Returns a mapping from parties to their candidates. That is, returns a Map whose keys are the names of all the parties that have candidates in this district. For each key, the value is a vector containing the candidates from that party in arbitrary order. (The order is arbitrary in the sense that the method is free to choose whichever order; this does not imply randomization.)

See also:

Returns all the candidates in this district that belong to the party named by the parameter.

Returns all the candidates in this district that belong to the party named by the parameter.

NOTE TO STUDENTS: THIS METHOD IS INTENDED FOR IMPLEMENTATION ONLY IN CHAPTER 10.1!

NOTE TO STUDENTS: THIS METHOD IS INTENDED FOR IMPLEMENTATION ONLY IN CHAPTER 10.1!

Returns a mapping of candidates to their distribution figures (Finnish: vertailuluku). That is, returns a Map whose keys are all the candidates in this district and whose values are the distribution figures of those candidates.

The distribution figure of a candidate is obtained as follows. Take the position (rank) of the candidate in the ranking list within his or her own party (as defined by rankingsWithinParties). For instance, the most-voted-for candidate within a party has a rank of 1, the second-most-voted-for has a rank of two, and so on. Divide the total number of votes received by the candidate’s party by the candidate’s rank, and you have the candidate’s distribution figure.

If multiple candidates from a single party received the same number of votes, the arbitrary order chosen by rankingsWithinParties is used here, despite the fact that this is likely to be a undesirable feature in a real-world system.

See also:

NOTE TO STUDENTS: THIS METHOD IS INTENDED FOR IMPLEMENTATION ONLY IN CHAPTER 10.1!

NOTE TO STUDENTS: THIS METHOD IS INTENDED FOR IMPLEMENTATION ONLY IN CHAPTER 10.1!

Returns all the candidates who will be elected on the basis of the vote. The seats available are given to the candidates with the highest distribution figures.

If multiple candidates happen to have the same distribution figure, an arbitrary order is used, despite the fact that this is likely to be a undesirable feature in a real-world system. (The order is arbitrary in the sense that the method is free to choose whichever order; this does not imply randomization.)

Returns:

the elected candidates in descending order by distribution figure

See also:

Prints (to the console) a description of each candidate in this district, each on a line of its own. A candidate’s description is obtained by calling its toString method.

Prints (to the console) a description of each candidate in this district, each on a line of its own. A candidate’s description is obtained by calling its toString method.

NOTE TO STUDENTS: It’s not usually a good idea to create “print some of your data” methods (“return some information” methods are better), but we’re still making one as an exercise here. More about this in later chapters.

NOTE TO STUDENTS: THIS METHOD IS INTENDED FOR IMPLEMENTATION ONLY IN CHAPTER 10.1!

NOTE TO STUDENTS: THIS METHOD IS INTENDED FOR IMPLEMENTATION ONLY IN CHAPTER 10.1!

Returns a vector containing the names of all the parties that have candidates in this district, ordered by the total number of votes received by each party. Descending order is used, so the party with the most votes comes first.

If multiple parties received the same number of votes, this method orders them in an arbitrary way. (The order is arbitrary in the sense that the method is free to choose whichever order; this does not imply randomization.)

NOTE TO STUDENTS: THIS METHOD IS INTENDED FOR IMPLEMENTATION ONLY IN CHAPTER 10.1!

NOTE TO STUDENTS: THIS METHOD IS INTENDED FOR IMPLEMENTATION ONLY IN CHAPTER 10.1!

Returns a mapping from parties to ranking lists of those parties’ candidates. That is, returns a Map whose keys are the names of all the parties that have candidates in this district. For each key, the value is a vector containing the candidates from that party in order by the number of votes they received, starting with the highest.

If multiple candidates from a single party received the same number of votes, this method returns those candidates in an arbitrary order. (Arbitrary in the sense that those candidates may be in whichever order, not in the sense of randomizing the order.)

override def toString: String

Returns a textual description of the district. The description is of the form Name: X candidates, Y seats, where Name is the name of the district, and X and Y the numbers of candidates and seats, respectively. For instance, might return the string Helsinki: 1064 candidates, 85 seats

Returns a textual description of the district. The description is of the form Name: X candidates, Y seats, where Name is the name of the district, and X and Y the numbers of candidates and seats, respectively. For instance, might return the string Helsinki: 1064 candidates, 85 seats

Definition Classes

Returns the top candidate (Finnish: ääniharava) of the district. That is, returns the candidate with the highest number of votes. If multiple candidates have the same number of votes, this method chooses one of them arbitrarily. (The choice is arbitrary in the sense that the method is free to choose whichever of the tied candidates; this does not imply randomization.) The method assumes that every district always has at least one candidate.

Returns the top candidate (Finnish: ääniharava) of the district. That is, returns the candidate with the highest number of votes. If multiple candidates have the same number of votes, this method chooses one of them arbitrarily. (The choice is arbitrary in the sense that the method is free to choose whichever of the tied candidates; this does not imply randomization.) The method assumes that every district always has at least one candidate.

NOTE TO STUDENTS: THIS METHOD IS INTENDED FOR IMPLEMENTATION ONLY IN CHAPTER 10.1!

NOTE TO STUDENTS: THIS METHOD IS INTENDED FOR IMPLEMENTATION ONLY IN CHAPTER 10.1!

Returns a mapping from parties to their top candidates. That is, returns a Map whose keys are the names of all the parties that have candidates in this district. For each key, the value is the candidate from that party with the most votes.

If multiple candidates from one party received the same number of votes, this method chooses one of them arbitrarily. (The choice is arbitrary in the sense that the method is free to choose whichever of the tied candidates; this does not imply randomization.) The method assumes that every party has at least one candidate.

Returns the total number of votes received by all the candidates, that is, the total number of votes cast in this district.

Returns the total number of votes received by all the candidates, that is, the total number of votes cast in this district.

def totalVotes(party: String): Int

Returns the total number of votes received by members of the party named by the parameter.

Returns the total number of votes received by members of the party named by the parameter.

NOTE TO STUDENTS: THIS METHOD IS INTENDED FOR IMPLEMENTATION ONLY IN CHAPTER 10.1!

NOTE TO STUDENTS: THIS METHOD IS INTENDED FOR IMPLEMENTATION ONLY IN CHAPTER 10.1!

Returns a mapping from parties to their vote totals. That is, returns a Map whose keys are the names of all the parties that have candidates in this district. For each key, the value is the number of votes received in total by all the members of that party in this district.

Concrete fields

val seats: Int