Assignment #6 -- Array exercises
Due: Mon, July 7 (revised)
Objective
This assignment will consist of writing a program, as well as a set of methods in another program,
that involve practice with arrays
Task
Write the following exercises, each in a separate file. Filenames should be:
- Array6.java
- DiceStats.java
Exercise 1
Filename: Array6.java
In a single class called Arary6, write the
methods described below - each works on an integer array.
I've provided a starter file Array6.java,
which contains a method printArray for printing out the contents
of an integer array and a method inputArray for filling an integer
array with user input. It also contains a small SAMPLE main() method, with a
few hard-coded tests. You can make any changes to "main()" that you like, for
testing your methods -- ONLY your methods (described below) will be graded, and
these will be tested with a different main() program. The one provided here
is just a sample to help you get started with testing.
The methods you will write should be named as follows:
- howMany
- isSymmetric
- reverse
As I will be testing this with a program other than the sample main()
here, your method names must match exactly.
-
Write a method called howMany that takes in an integer array
(1st parameter) and an integer value (2nd parameter), and returns the
number of occurrences of the value in the array. For example, if the
array list is
{3, 6, 8, 6, 2, 10, 6, 5, 2, 6, 5, 2, 6}
then the call howMany(list, 6) should return 5 (since the value
6 appears in the array 5 times).
-
Write a method called isSymmetric which takes in an array of
integers and returns a boolean response. The method should return
true if the array is the same backwards as forwards, and
false otherwise. (i.e. this is like the idea of a palindrome, but
with numbers, not letters -- and checking based on array slots, not on
digits).
-
Write a method called reverse that takes in an integer array (as
a parameter) and returns a new array that is a reversal of the original
array. For example, if the following array is sent in:
{2, 4, 9, 10}
The method should return the array:
{10, 9, 4, 2}
Note that this method should not change the original array at all.
Sample Output from the provided main()
Sample user input is underlined.
Enter array size: 10
Enter array element 0: 4
Enter array element 1: 6
Enter array element 2: 8
Enter array element 3: 10
Enter array element 4: 20
Enter array element 5: 25
Enter array element 6: 10
Enter array element 7: 10
Enter array element 8: 4
Enter array element 9: 6
Initial array:
{4, 6, 8, 10, 20, 25, 10, 10, 4, 6}
The number 6 appears 2 times
The number 10 appears 3 times
The number 25 appears 1 times
The array IS NOT symmetric
The reversed array is:
{6, 4, 10, 10, 25, 20, 10, 8, 6, 4}
Initial array is:
{4, 6, 8, 10, 20, 25, 10, 10, 4, 6}
Exercise 2
Filename: DiceStats.java
This is based on Exercise 7.17 from the textbook, and it expands on
the Dice exercise from assignment 4.
Write a program that does the following:
- Ask the user to enter how many dice will constitute one roll.
(This is based on the idea that different games require different
numbers of rolls for a "turn". For example, in Yahtzee, you roll 5
dice. In craps, you roll 2 dice. Etc.)
- Ask the user to enter how many rolls they would like to perform
- Create and use an array to keep track of how many times each
possible dice sum appears. (Essentially, you need a bunch of counters,
but how many will depend on how many dice are rolled per "turn". For
example, 2 dice can come up with a total of 2 through 12. But 3 dice
could yield a total of 3 through 18).
- Hint: The idea of this array is to be a "frequency array", like in
example 7.7 from the book
- Hint: How many counters will you need? Well, the lowest possible
total is the number of dice rolled (all 1s). The highest possible
total is all 6s, so (6 * number_of_dice). Use this information to
decide on the size of your frequency array.
- Use a loop to roll the specified number of dice the desired number
of times (and calculate the sum of each dice roll). Use the array to
count and track the number of times each possible sum appears
- Display the results in a table with 3 columns:
- the die total
- the number of times that total appeared
- the percentage of the total rolls that this sum appeared
Compute and print the percentage to 2 decimal places.
Sample Run 1
(user input is underlined)
How many dice will constitute one roll? 2
How many rolls? 100000
Sum # of times Percentage
2 2799 2.80 %
3 5568 5.57 %
4 8261 8.26 %
5 10970 10.97 %
6 13830 13.83 %
7 16862 16.86 %
8 13895 13.90 %
9 11073 11.07 %
10 8391 8.39 %
11 5576 5.58 %
12 2775 2.78 %
Sample Run 2
(user input is underlined)
How many dice will constitute one roll? 4
How many rolls? 100000
Sum # of times Percentage
4 78 0.08 %
5 291 0.29 %
6 756 0.76 %
7 1490 1.49 %
8 2710 2.71 %
9 4268 4.27 %
10 6309 6.31 %
11 8060 8.06 %
12 9696 9.70 %
13 10866 10.87 %
14 11132 11.13 %
15 10847 10.85 %
16 9649 9.65 %
17 7963 7.96 %
18 6166 6.17 %
19 4331 4.33 %
20 2657 2.66 %
21 1558 1.56 %
22 778 0.78 %
23 309 0.31 %
24 86 0.09 %
Requirements for each program
- Use the Scanner class for input
- Use the Random class for random number generation
- When you write source code, it should be readable and
well-documented.
- Declare methods to be public and static
Submitting:
Submit the files through the Assignment 6 Blackboard link. Make sure you
attach BOTH files before you click Submit:
Array6.java
DiceStats.java