Lab 1: Making a Fair Division
OBJECTIVES
To practice your Java and to design your own algorithm (it does
not have to be recursive).
RELATED EXPERIENCE
After several years of elementary school teaching, I learned
how to divide goodies between two children so that both think
they are getting a fair deal: choose one of the children
to divide the goodies into two parts, and then let the
other child pick which part he/she wants. Note that the
goal of the child who divides is to get as even of a split
as possible.
SCENARIO
You and a greedy classmate win a large sack of gems. You take the gems
to a reputable gem appraiser who determines the value of each gem.
One of you wants to sell the gems and split the profit down the
middle, but the other wants to hold onto the gems. You ask your
data structures teacher for advice, and she suggests that the two of
you divide up the rocks (no gem can be split in two, tho) in the following
manner: it is your job to divide the gems into two piles; your classmate
gets to choose which pile they want.
ASSIGNMENT
Write a program that takes as input a list of gem values and
divides the gems into two piles such that the sum of the values of the rocks
in the two piles is close to equal. The values can be
any positive integer <= 50000.
- Give a menu allowing for two different kinds of input entry:
- Ask the user how many gems are in the bag, and then choose that
many random numbers (random numbers should be from 1..50000).
- Read the name of a file in the program, then read the gem values
from that file: the first entry in the file
is the number of gems, followed by that number of gem values.
The values are delimited by whitespace.
- Have the program figure out a fair way to divide the gems.
- Produce the following output:
- The original list of gem values
- The gem values in the first group: and the sum of this group
- The gem values in the second group: and the sum of this group
EXAMPLE OUTPUT 1
Gem Values:
26004 39663 29502 40739 14724 15188 48659 47360
3106 777 6038 5010 33108 213 26954 43668
Pile #1.
48659 40739 39663 29502 26004 5010 777
Sum: 190354
Pile #2.
47360 43668 33108 26954 15188 14724 6038 3106 213
Sum: 190359
EXAMPLE OUTPUT 2
Gem Values:
25342 12858 14827 15960 35321 18784 24278 23101
9727 44395 25120 41247 9868 27622 1796 16221
29060 11148 39333 45273 10494 3486 44636 19997
15941 5572 41958 22909 20382 45803 37723 12075
25012 18901 28034 26685 37684 18664 49785 47410
13058 24905 38657 39278 2526 6804 5498 47937
34304 11183
Pile #1.
49785 47937 47410 45803 45273 44636 44395 41958 41247 39333
39278 38657 37723 27622 11148 6804 3486 1796
Sum: 614291
Pile #2.
37684 35321 34304 29060 28034 26685 25342 25120 25012 24905
24278 23101 22909 20382 19997 18901 18784 18664 16221 15960
15941 14827 13058 12858 12075 11183 10494 9868 9727 5572
5498 2526
Sum: 614291
GRADING
Your program will be graded on the following, with approximate
point values shown:
- Program follows input/output specifications (40 points)
- Algorithm produces a good solution (40 points)
- Algorithm creativity (10 points)
- Readable program style and documentation (5 points)
- Modularity, program structure (5 points)
Your idea for how to divide the numbers into two groups should be your own
because you are trying to beat the other guys in the class.
Due date: August 21(11:59pm).
SUBMISSION
Your program should be in your directory on the student2 machine in
your 3460/Lab1 folder. (Make a directory for 3460, if you don't already
have one, and then for Lab1). Place your main method in Lab1.java in the
Lab1 directory. You may use other classes as desired.
I will run your program by typing java Lab1. I should get a
menu allowing me to choose random gems or read the gems from a file.
Reasonable output should follow.