0% found this document useful (0 votes)
45 views

HW 12

hw12

Uploaded by

David M Rodgers
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

HW 12

hw12

Uploaded by

David M Rodgers
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

CS101 Spring 2014 Homework #12

More practice with creating & manipulating arrays of primitive type. More file input/output.
Also more practice with program design.
1. Aim
Gain experience in creating and manipulating arrays. For part II of this assignment we leave the design of the solution up to you.
Rather than supplying you with a detailed problem description and how to break down the problem, you are given a more open-ended
problem. Make sure you take the time do create a good design before you start coding it will make your life so much easier. Use
methods to help handle small tasks for you.

2. Files Needed
You will not be provided with any starter files for this homework. Create a project in Eclipse for this assignment. For each exercise
below, create a Java class with the appropriate name. When you create each program, be sure to have Eclipse create the public static
void main method for you. Be sure to include the standard header comments at the top of each file.

3. To be Handed In
The files InversePerm.java & MasterMind.java should be submitted on-line via the homework #12 page in Oak. Be sure to name
the files/classes as specified.

4. Exercises
Part I: Inverse Permutations
Complete the following exercise by writing appropriate Java code in the file InversePerm.java.
Your job is to write a program that reads in a permutation of the integers 0 to N-1 (where the size N will be specified with the
permutation) and prints the inverse permutation. If the permutation is in an array a[], its inverse is the array b[] such that
a[b[i]] == b[a[i]] == i, for all i such that 0<=i<N
For example, given the permutation of 3 values a[] = {2, 0, 1}, it inverse is b[] = {1, 2, 0}. You can find additional
information on inverse permutations on the web1 (Warning: most discussions on the web use 1-based indexing whereas we will be
using zero-based indexing).
Program requirements:
1) All input for this program will come from the ASCII text file permutations.txt. This file will contain only integers. The file will
contain data on one or more permutations. Each permutation is represented by two lines of data. The first line will contain a single
integer N that represents the size of the permutation. The second line will contain N integers that are supposed to represent a
permutation of the integers 0 to N-1. You are guaranteed that the file will contain an even number of lines of data (since there are
two lines per permutation). You are also guaranteed that the first line of each pair will have a single integer value, call it N, and
the second line will have exactly N values. You are not guaranteed that the N values actually represent a valid permutation of the
integers 0 to N-1. You can process this file via line-based processing, but since the contents are so well defined it is also possible
to process the file via token-based processing (which makes it a bit easier). But either strategy is valid. You should add more
permutations to the input text file so as to fully test your program.
2) You are to process the permutations in the input file one at a time. You are also required to process all the permutations in the file.
3) For each permutation in the file, you will read it in and store the values in a correctly sized array.
4) Before attempting to compute the permutations inverse, you are required to verify that the N values are a valid permutation of the
values 0 to N-1. That means that the values are only in the range 0 to N-1, and that each of those N values appears exactly once.
1

http://mathworld.wolfram.com/InversePermutation.html, and
http://www.wolframalpha.com/entities/mathworld/inverse_permutation/we/qh/5e/

5) If the N values are not a valid permutation then print the values (to the output file) along with an appropriate message see the
sample execution below.
6) If the N values are a valid permutation, then compute the permutations inverse. The inverse should be stored in a separate array
of the appropriate size. Print both the permutation and its inverse (to the output file) along with an appropriate message see the
sample execution.
7) All program output is supposed to be printed to the file inversions.txt. The output file should contain a blank line after the output
produced for each permutation. The format of the output file you produce should exactly match the sample execution below
(there is a blank line at the bottom of the output file).
8) You must use good programming practices and write separate methods to handle the various tasks. E.g., you should have a
separate method that reads a permutation from the file and returns the array; you should have a separate method that tests an array
to see if it is a valid permutation and return a boolean value; you should have another method that computes the inverse and
returns it; and you should have another method to produce the output.
Sample execution:
The program InversePerm.java does not produce any output to the screen. Rather data is read from the file permutations.txt and
output is sent to inversions.txt. Here is one sample input file along with the corresponding output file your program is to produce. You
must match the formatting of the output file exactly. Note: you should add more permutations to the file permutations.txt so as to fully
test your program.
permutations.txt:

inversions.txt

3
2 0 1
4
1 2 3 4
11
0 3 8 5 10 9 4 6 1 7 2

The permutation is: [2, 0, 1]


And its inverse is: [1, 2, 0]
The values [1, 2, 3, 4] are not a valid permutation.
The permutation is: [0, 3, 8, 5, 10, 9, 4, 6, 1, 7, 2]
And its inverse is: [0, 8, 10, 1, 6, 3, 7, 9, 2, 5, 4]

Part II: MasterMind.java


Our project will be to write a program that plays a variation of the game of Master Mind. If you are not familiar with the game, visit
the Wikipedia page (http://en.wikipedia.org/wiki/Mastermind_%28board_game%29).
Your task is to write a program that plays a game of Master Mind under the following rules:
1. We will use 4 integer numbers rather than 4 colored pegs. You will want to use an array of ints
to hold these numbers.
2. Each number will be in the range 1 to 6.
3. Numbers may not be repeated. I.e., the hidden numbers can be {3, 2, 6, 4}, but they cannot be
{2, 5, 1, 2} because the number 2 is repeated. This restriction makes it a bit easier on the
person playing the game, but more importantly for us, it also simplifies the logic necessary to
give proper feedback on a players guess.
4. Whenever the user wants to play a game (or play another one), your program should create the
array of 4 hidden numbers by generating random integers in the range 1 to 6, ensuring that
there are no repeats.
5. Each time the player makes a guess, they will enter 4 integers (store them in another array).
Note that the 4 values may not all be entered on the same line, so you should use token-based
input methods to read them. You will need to verify that the numbers they entered are in the expected range and that they are all
unique. You then need to provide proper feedback on how many numbers were correct in both value & position, and how many
number were correct in value but in the wrong position. See the sample execution below. [Not allowing duplicate numbers makes
this task a bit easier.]
6. When the player finally wins, inform them how many guesses it took, and ask them if they want to play again. When the player is
done playing games, report the number of games played, the total number of guesses made in all the games, the average number
of guesses per game, and the number of guesses made in their best game.
7. You should write your program in such a way so that if someone wants to change it to have a different number of hidden numbers
(say 5 or 6 rather than 4) then you would only have to change one line of code in your program. Similarly, if someone wanted to
change the range of values for the numbers to something other than 1 to 6 (say 1 to 9), then you would again only have to change
one line of code.

8.

Be sure to think about your design and how to break the problem down into smaller, manageable pieces. Write separate methods
to handle all the pieces. As an example, my solution has 5 methods and approximately 90-110 lines of Java code (not counting
blank lines or lines containing only a comment or curly braces). None of my method has more than 25 lines of code. For this
assignment, we are placing a limit of 25 lines of code per method (again, not counting blank lines or lines containing only a
comment or curly braces).

Those are the guidelines for this project. As you see, I have not given you a high level design that will be your job. You might want
to consider the examples you have seen in prior programming assignments. Be sure to create small helper methods as needed. Your
design will account for one sixth of your grade for this program. Think before you code!
Going above & beyond: If you want an additional challenge once you have completed the MasterMind game, consider removing the
restriction that the hidden numbers and the users guess numbers are unique. This makes generating the hidden numbers much easier,
since you no longer have to prevent duplicates, but it makes the task of giving a proper response much more difficult. This is because
if there are duplicate numbers in the guess, they cannot all be counted as a correct guess (whether correct position or not) unless they
correspond to the same number of duplicate numbers in the hidden code. If you do this extra challenge, please be sure to make a note
of it in your Oak submission so that the grader is aware of it. This work is worth up to 20% extra credit on this portion of the
assignment.
Example:
This is a sample execution to show you how MasterMind.java should behave:
I am thinking of 4 numbers in the range 1 to 6.
Enter 4 numbers separated by blanks: 1 2 3 4
You had 0 correct numbers in the correct positions
And you had 2 correct numbers in the wrong positions
Enter 4 numbers separated by blanks: 4 6 7 5
You entered a value not in the range 1 to 6
Try again.

Note: does not count as a guess

Enter 4 numbers separated by blanks: 1 2 5 2


Your guess contained duplicate numbers (the number 2 appeared at least twice).
Note: also does not count as a guess
Try again.
Enter 4 numbers separated by blanks: 3
Note: data
4
5
6
You had 1 correct numbers in the correct positions
And you had 2 correct numbers in the wrong positions
Enter 4 numbers separated by blanks: 3 1 6 5
You had 2 correct numbers in the correct positions
And you had 2 correct numbers in the wrong positions
Enter 4 numbers separated by blanks: 3 6 1 5
You had 1 correct numbers in the correct positions
And you had 3 correct numbers in the wrong positions
Enter 4 numbers separated by blanks: 6 1 3 5
You had 0 correct numbers in the correct positions
And you had 4 correct numbers in the wrong positions
Enter 4 numbers separated by blanks: 3 5 6 1
You had 4 correct numbers in the correct positions
And you had 0 correct numbers in the wrong positions
Congratulations, you solved the puzzle in 6 guesses.
Do you want to play again? (Y|N)
y

could be entered on separate lines

I am thinking of 4 numbers in the range 1 to 6.


Enter 4 numbers separated by blanks: 6 5 4 2
You had 1 correct numbers in the correct positions
And you had 3 correct numbers in the wrong positions
Enter 4 numbers separated by blanks: 6 4 2 5
You had 0 correct numbers in the correct positions
And you had 4 correct numbers in the wrong positions
Enter 4 numbers separated by blanks: 2 5 6 4
You had 4 correct numbers in the correct positions
And you had 0 correct numbers in the wrong positions
Congratulations, you solved the puzzle in 3 guesses.
Do you want to play again? (Y|N)
y
I am thinking of 4 numbers in the range 1 to 6.
Enter 4 numbers separated by blanks: 1 2 5 6
You had 0 correct numbers in the correct positions
And you had 3 correct numbers in the wrong positions
Enter 4 numbers separated by blanks: 3 6 2 5
You had 0 correct numbers in the correct positions
And you had 3 correct numbers in the wrong positions
Enter 4 numbers separated by blanks: 2 5 6 4
You had 2 correct numbers in the correct positions
And you had 0 correct numbers in the wrong positions
Enter 4 numbers separated by blanks: 2 5 1 3
You had 1 correct numbers in the correct positions
And you had 2 correct numbers in the wrong positions
Enter 4 numbers separated by blanks: 2 3 6 1
You had 4 correct numbers in the correct positions
And you had 0 correct numbers in the wrong positions
Congratulations, you solved the puzzle in 5 guesses.
Do you want to play again? (Y|N)
n
You played a total of 3 games.
You made 14 total guesses over those games.
That is an average of 4.666666666666667 guesses per game.
Your best game was 3 guesses.

5. Additional requirements
A. You must start your program with header comments that provide your name, VUnetID, email address, the date the program
was last modified, an honor statement (I have neither given nor received unauthorized help on this assignment), and a short
description of the program (see the examples distributed with homework #2).
B. Each method that you write (except main), should be preceded by a block of Javadoc comments which describe what the
method does, what the parameters are, and what it returns. Any required preconditions should also be clearly stated.
C. For this assignment, we are placing a limit of 25 lines of code per method (again, not counting blank lines or lines
containing only a comment or curly braces).
D. You should use a consistent programming style. This should include the following.
a. Meaningful variable & method names

b.
c.
d.
e.
f.

Consistent indenting
Use of "white-space" and blank lines to make the code more readable
Use of comments to explain pieces of tricky code
A descriptive JavaDoc comment before each method (other than main, which already has a short description of the
program). Note that we will be using JavaDoc comments from here on out to document our methods.
Lines that do not extend beyond column 100 (or column 80 is even better)

See the code examples in the class text for a good formatting style.

6. Submission for grading


Once you have completed the exercise, submit the files InversePerm.java & MasterMind.java for grading by visiting the homework
#12 page in Oak.

7. Grading
This lab is worth 45 points: 15 points for InversePerm and 30 points for MasterMind. Your grade will be based on whether your
solution is correct or not, and on how closely you followed the directions above. Remember that programming style will now be a
larger part of your grade. 5 of the 30 points for the MasterMind program will be based solely on the design of your solution.

You might also like