1 Introduction In the US, we use a simple system of election, known as “First Pa
ID: 3683230 • Letter: 1
Question
1 Introduction
In the US, we use a simple system of election, known as “First Past the Post” - where a candidate
can win an election with less than 50% of the vote. In some other countries, they use a more
complex system, known as the “Alternative Vote.” I don’t know if it’s really any better, but it
certainly is an interesting algorithm. You can learn about it here:
https://www.youtube.com/watch?v=3Y3jE3B8HsE
We’d like to model this type of election, and so we are going to read a data file which gives us a
set of candidates and the preferences of several different voters. This Project will focus on
reading the file, and then storing the data in a class, and then implementing code that lets the
voters choose their favorite. In the next project, we’ll actually count the votes, add code to
implement the Alternative Vote, and also (probably) allow for a more flexible file format.
2 Overview
In this Project, you will write two classes. One class, named Proj06 Voting, will contain your
main() method. (If you want to add additional methods, you are allowed, but it isn’t required.) The
other class, named Voter, will model a single voter; we’ll create several of these objects, and
store them in an array. This class will include some instance fields (storing data about one
voter), and a couple of instance methods.
Your program will (in this Project) perform a couple simple steps. First, it will read an input
file into a set of arrays (including an array of Voter objects). Then it will ask each Voter to
cast their vote.
In this Project, you won’t be counting the votes, and you won’t have to figure out which candidate
was the winner. But you still need to implement the voting logic - which will be useful in the next
project.
2.1 The Input File
The file you read from will look like this (this is the voter.1 example, which you can download to
test your code):
1
Gorilla Leopard Tiger Turtle Owl
red_squirrel Turtle Owl Gorilla Tiger Leopard fox Leopard Tiger Gorilla Owl Turtle
wolf Leopard Gorilla Turtle Owl Tiger hyena Gorilla Turtle Owl Leopard Tiger
baboon Gorilla Owl Leopard Tiger Turtle puffin Owl Turtle Gorilla Tiger Leopard
shrew Leopard Turtle Gorilla Tiger Owl frog Gorilla Tiger Leopard Owl Turtle
gray_squirrel Gorilla Leopard Turtle Owl Tiger pig Gorilla Turtle Owl Leopard Tiger
ocelot Leopard Tiger Gorilla Turtle Owl rodent Owl Tiger Leopard Turtle Gorilla
The first line has exactly 5 names; these are the candidates. Following that are exactly 12 voters
- each with the 5 candidates listed in a different order.
Each voter’s line has six words in it: the name of the voter, followed by the five candidates, in
order. The first candidate listed is the one which that voter most hopes will win the election; the
last is the one that the voter most hopes will lose.
(In this version of the project, everything will be exact, which makes it easy to read the file.
You can read with Scanner.next(), and not worry about badly-formed input files. You don’t even have
to check hasNext(); you can just assume that the file will be perfect! We might change this in the
next project - I haven’t decided yet.)
2.1.1 Downloading the Inputs
You can download four different example inputs (along with each one’s output) here:
http://www.cs.arizona.edu/classes/cs127a/spring16/projects/proj06_ files
3 Program Requirements
Your program must implement implement two classes: Proj06 Voting, which includes (at least) your
main() method, and Voter, which is detailed below.
In the main() method, your program must:
• Check the command line arguments; if the user did not pass exactly one argument, print an error
message and terminate.
• Otherwise, open a Scanner to read from the file (see Slide Deck B, slides 15-22).
2
• Allocate an array of Strings to hold the candidate names; read exactly five candidate names with
the next() method (you do not have to call hasNext()).
• Allocate a Voter array, and then fill it with Voter objects. (See Slide Deck 8-basic, slides
28-44.)
• Read the information for each voter from the file: exactly six Strings per voter. The first is
the voter name; the other five are the preferences. Store all of this information into the Voter
object. (Again, you can simply call next() several times; you do not have to call hasNext().)
• Print out the information for all of the voters using the print() method in the Voter class.
(Remember, print() only prints out the information on a single voter. So you will need to build a
loop, and call this method on every voter.)
Then also print out the list of the candidates.
See the Output section below to see exactly how it should look.
• Iterate through all of the voters, and call the vote() method on each one. (You may ignore the
return value from vote().)
Remember, each call to vote() will print something out; see the Output
section below to see exactly how it should look.
3.1 Output
The TAs will be using scripts to help them grade this project. So your output should look exactly
like the example output below.
For the input file voters.1, your output should look like this:
THESE ARE THE VOTERS:
Voter: name=red_squirrel preferences: Turtle Owl Gorilla Tiger Leopard Voter: name=fox preferences:
Leopard Tiger Gorilla Owl Turtle
Voter: name=wolf preferences: Leopard Gorilla Turtle Owl Tiger Voter: name=hyena preferences:
Gorilla Turtle Owl Leopard Tiger Voter: name=baboon preferences: Gorilla Owl Leopard Tiger Turtle
Voter: name=puffin preferences: Owl Turtle Gorilla Tiger Leopard Voter: name=shrew preferences:
Leopard Turtle Gorilla Tiger Owl Voter: name=frog preferences: Gorilla Tiger Leopard Owl Turtle
Voter: name=gray_squirrel preferences: Gorilla Leopard Turtle Owl Tiger Voter: name=pig
preferences: Gorilla Turtle Owl Leopard Tiger
Voter: name=ocelot preferences: Leopard Tiger Gorilla Turtle Owl Voter: name=rodent preferences:
Owl Tiger Leopard Turtle Gorilla THESE ARE THE CANDIDATES:
Gorilla Leopard
3
Tiger Turtle Owl
STARTING A DUMMY ELECTION:
red_squirrel votes for Turtle fox votes for Leopard
wolf votes for Leopard hyena votes for Gorilla baboon votes for Gorilla puffin votes for Owl shrew
votes for Leopard frog votes for Gorilla
gray_squirrel votes for Gorilla pig votes for Gorilla
ocelot votes for Leopard rodent votes for Owl
4 The Voter Class
You must implement a class named Voter. It must include an instance field which stores the name of
the voter. It must also include another instance field which stores an array of Strings - which are
the voter’s preferences.
Remember! When you allocate a Voter object, the array variable will be null. (Look at Slide Deck
05-advanced, slide 2; the default value for reference variables is null.) Therefore, you will need
to allocate the array as a second step.
4.1 The print() Method
The Voter class must include a method named print(), which will print out the information about
that one voter. (See the example output above to see what to print.)
4.2 The vote() Method
The Voter class must include a method named vote(), which takes an array of Strings as a parameter.
These are the names of the candidates in the election. The Voter will compare these candidates to
its own preferences array, and return the index of the candidate that the voter wants to vote for.
Voters always vote for the candidate that they prefer the most. (No strategic voting in 127A -
that’s a much harder problem!)
The vote() method should also print out a message, indicating the name of the voter, and the name
of the candidate they chose. (See the example output above.)
4
The return value from vote() is the index of the candidate which the voter
is voting for. So for example, if the array of candidates is
{Gorilla,Leopard,Tiger,Turtle,Owl}
then the voter can return 0 to vote for Gorilla, 3 to vote for Turtle, etc.
4.2.1 Implementation of vote()
The vote() method will need nested loops, since it is comparing all possible preferences to all
possible candidates. Once the voter finds any match, it should return immediately.
But to make this work, you will need to be think about which loop should be the outer loop, and
which should be the inner loop. Basically, you want the voter to vote for their first preference
(if they are in the election), and only vote for some other candidate if the first preference isn’t
in the election.
WARNING: It will be tempting to always vote for preference 0 in the vote method. Don’t do it -
instead, search for every possible preference, one at a time! (See the voters.5 input to see an
example where searching for prference 0 would not work.) This is important because, in the next
Project, you will implement the Alternative Vote - and not all of the candidates will be in the
ction at all times.
ction at all times.
Explanation / Answer
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.Arrays; class Test{ public static void main(String[] args) { BufferedReader br = null; Voter[] voters = new Voter[12]; String[] candidates = new String[5]; try { String sCurrentLine; br = new BufferedReader(new FileReader("C:\input.txt")); String cs = br.readLine(); candidates = cs.split(" "); int c = 0; while ((sCurrentLine = br.readLine()) != null) { String[] input = sCurrentLine.split(" "); Voter voter = new Voter(input[0]); String[] prefs = new String[5]; for(int i=0;iRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.