600.107: Introduction to Programming

Summer Session 2006: May 30, 2006 - June 30, 2006

Assignment 6: Array Relaxation

Out on: June 14, 2006
Due by: June 16, 2006 before noon (hard deadline)
Collaboration: None
Grading: Documentation 10%, Style 10%, Functionality 80%

Overview

Assignment 6 consists of several smaller programming problems, allowing for some (relative) relaxation before Assignment 7 comes out. I hope you're not too mad about Assignment 5... :-) As before, all previously covered concepts are applicable, but unless explicitly required by a problem, you do not have to use all of them. Use your own judgement.

As with the previous assignments, please follow these ground rules: Try to write your functions in such a way that only one return instruction at the end of your function is necessary! Try to write your procedures without explicit return instructions! Try to make variables as local as possible to the place where they are needed, procedures and functions should communicate only through arguments and results! Remember that we grade on programming style and documentation as well as functionality (including the use of javadoc comments for classes and methods).

Notes: If you have problems with any of these tasks, please contact the staff or everybody in class using the mailing lists! Do this early so you have a shot at finishing the assignment. Also, if you can't solve one problem, feel free to skip it and work on the problems you can solve. Handing in something is much better than not handing in anything.

Problem 0: Election Time (40%)

Write a program Election.java that keeps track of the votes for a simple election and announces a winner in the end. The votes come in as integers (there's only one question, namely who to elect, and candidates are numbered). Only positive integers represent votes, negative integers or zeros represent "void" ballots from which the voter's intent could not be clearly determined. You know neither how many candidates there are nor how many votes will come in, all you know that you'll get a stream of integers, and once that stream is over, you need to print the winner of the election. Make sure you handle "unexpected" input gracefully: Your program should not stop with an error or go into some weird state regardless of what the user types.

For this problem, you must solve the task at hand using arrays. I recommend two arrays, one to keep track of candidates and one to keep track of how many votes each candidate received. Once there is no more input, you can run over the array in which votes were counted, determine who has the most votes, and then print the number of that candidate (the "winner"); if you get a tie, select an arbitrary candidate. Note that "the number of that candidate" is not the same as the index you reserved for the candidate in your table! This problem has several similarities to the Freq.java problem from the last assignment, but not everything is the same!

Problem 1: Smarter Elections (40%)

Write a program BoyerMoore.java that solves exactly the same problem posed above, but without using arrays at all! If you have a hard time figuring out how to do that, read up on the famous MJRTY algorithm by Boyer and Moore. If that's still not helpful, ask on the discussion list...

Problem 2: Array Reversals (20%)

Write a Java class Reversals.java that consists of three methods dealing with arrays:

Make sure that you don't miss any "special cases" such as arrays of length 1, even length, odd length, etc. You should be able to deal with all of those correctly.

Deliverables

Submit your solutions by email to the submission address listed on the course website. Please make sure that you answer written questions in the text of the email. Please make sure that you attach programming questions as separate somename.java files. Note that you'll get a confusing automatic reply that I will explain later.

Finally, please include your name and email address as well as a brief comment explaining how to use your programs at the top of each Java file. Here's a template you can clone:

/*
 * CS 107
 * Assignment x: Problem y
 *
 * Your Name Here
 * Your Email Here
 *
 * Description of your program and any comments you may have
 * for the grader go here...
 */
Updated: $Id: assignment-6.html 156 2006-06-14 23:37:44Z phf $ Validate: XHTML CSS
Copyright © 2006 Peter H. Fröhlich. All rights reserved.