CS 280 Spring 2008

Assignment 11 – Array of Objects

100 points

 

Assigned: 04/02/2008

Due: 04/11/2008 at the start of class  (longer than usual due to the test – BUT don’t put it off until the day before!!!)

 

Pre-Lab (Do Before Lab):  Bring materials – a USB for a take home copy.  Plan out your GUI, tasks, objects, and events needed for program.  Plan out how you will solve the problem.

 

Main Assignment:

E-Vote Inc is working on a new program to cash in on the electronic voting era. Your program is a (huge) simplification dealing with voting for one office.

You will write: 1) a Candidate class as described below (and in Javadoc documentation); 2) The GUI class that provides the main capabilities of the program.

The program should get from a file (candidates.txt) a list of candidates and put them in a combo box AND in an array of Candidates.  When a user then selects the candidate from the combo box AND selects a “Vote” button, update the number of votes for the given candidate (via Candidate class method!)  (DO NOT have the vote recorded immediately upon selection in case of inadvertent click).

Provide the vote capability, and administrator capabilities to see the results, reset the results, and an exit capability.

Your Candidate class should implement the Comparable interface. It should include instance variables for: candidate name (keep it simple; don’t separate into last and first), and number of votes received.  It should also provide the following methods:

·         2 constructors – the default, and one that is passed the candidate name. Both constructors should set the candidate’s number of votes to zero.

·         Simple inspectors for both instance variables

·         Two controlled mutators:

o    To add one vote to the number of votes received by the candidate

o    To reset the candidate’s votes to zero

·         Methods that override Object methods:

o    Equals – that tells whether a passed Candidate has the same contents as the invoking Candidate

o    A toString method that turns a Candidate into a reasonable String representation of a Candidate.  

·         compareTo – to compare Candidates based on number of votes received - as expected by users of Comparable – cand1.compareTo(cand2) should return:

o    1 if cand1 > cand2

o    0 if cand1 = cand2

o    -1 if cand1 < cand2

·         Overloaded versions of a matches method – each of which tells whether the invoking Candidate matches a passed value (useful for finding a candidate in an array of candidates without knowing how many votes they have).

o    Passed a String, returns whether the candidate’s name matches that string

o    Passed a Candidate, returns whether the two Candidates names are the same

See my simple sample interface below – But don’t let that constrain your creativity. Use it to help understand what I see as the inputs and outputs. The paper version of this assignment shows a couple of screen shots from different points running my program. The version on my www site shows 4 screen shots from various points. We only want to display the results when they are asked for (I suggest using a jPanel so the results can be all visible or not with one statement). Details are given below. As the program is starting up, you should read the candidates from the file loading the array of candidates and a combo box that the user will use to choose a candidate (sorry, no “write-ins” in version 0.1). You should create buttons to:

1.      Vote – the currently selected candidate receives one new vote. NOTE: when we did bank accounts in class, we counted on the array being in the same order as the combo box. We cannot do that here because the array may be sorted as part of showing results. Also, hide the results (as they are no longer correct).

2.      See results – sort the array (see below), then display the candidates from highest votes to lowest votes, and display the winner (don’t worry about ties!!)

3.      Reset the results – set all candidates back to having zero votes. Also, hide the results (as they are no longer correct).

4.      Exit – exit the application.

·         You’re still really just beginning at setting up GUIs. Don’t worry too much about making the interface nice.

·         But, if you plan on working with 5.5 or higher version of the IDE, ask me about using layouts, panels, java version, etc so that your program will work with 5.0. (Java version needs to be 1.5, NOT 1.6).

 

Hand in:

 

Miscellaneous:

·         Please set the name of the project to something other than the default name (JavaApplication1, …). 

·         Put  your name in a jlabel on the GUI form  and/or in the title bar.

·         HINT: since they will be accessed by handlers of multiple buttons, the array and the number of real elements in the array will need to be instance variables in the GUI class.  

·         You can assume that there will never be more than 20 candidates for office.

·         Reading from a file will require some dealing with exceptions.  We now have some examples from class – which are actually more complicated than what you need to do – the candidates file will only have one item per line – one candidate name.   

·         Sorting arrays can be done using static methods of the Arrays class.

o    Be aware that since the array won’t be completely full, you need to use a version of the method in which you can specify which part of the array to sort – otherwise you will get a null pointer exception because of the elements that have no value.

o    BTW, the sort method should sort from low to high, and we want to see high to low – this can be managed in how you display the results.

·         We are NOT including any control over who is voting and how many times they are voting. That is part of the (huge) simplification involved in this program. You will thank me for that.

o    MAKE SURE YOUR PROGRAM WORKS! (i.e. gets correct results, controls what is displayed/enabled).  It must provide all of the requested capabilities AND provide them in the expected way. Check that the desired parameters and return types are provided in your Candidate class – even if they are not needed for the GUI class (we want re-usability!).

o    You MUST include some comments that explain your program in order to get full credit,  including a javadoc compatible comment for each method.

 

 


At  Startup:

After one vote recorded, See Results:

 

After multiple votes, See Results

 

After Reset, See Results (don’t worry about ties for most votes):