Assignment #4 - Chapter 5 Exercises - Arrays
Due: Thursday, June 12
From chapter 5, do programming exercises
5.8, 5.9, and 5.10 (page 214), and the following problem:
(Also note the extra instructions below).
Additional Problem
Write a method called HowMany that takes in an integer array and
an integer value, 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). Use the above array to test the method
(in your sample main routine) and make three calls to HowMany, checking
the values 6, 2, and 11).
Filenames should be
- Prob5_8.java
- Prob5_9.java
- Prob5_10.java
- Array4.java - (4th array exercise)
Note that these filenames all start with a capital letter. Please make
your filenames match mine exactly.
Extra Instructions
- For all 4 of these, you are given a sample array to use to test the
method you write. Write a main method for each one that creates the
given sample array and then tests the method with this array. However,
the method for each problem should be general enough to work for
any array (1 dimensional arrays for 5.8 and the extra exercise, 2
dimensional arrays for 5.9 and 5.10).
- For each of these programs, print the information given in the sample
runs shown below. Note that these all involve printing the original
array, then printing out the result returned by the method. Note that in
5.8, the result is an entire array
- For 5.9, recall that the major diagonal of a matrix is the one
you get by drawing a line from the upper left hand corner to the bottom
right hand corner of the matrix
Sample Outputs
Sample user input is underlined.
Sample output for 5.8
Programming Exercise 5.8 'array reversal'
Original array = {5.0, 4.4, 1.9, 2.9, 3.4, 3.5}
Reversed array = {3.5, 3.4, 2.9, 1.9, 4.4, 5.0}
Sample output for 5.9
Programming Exercise 5.9 'major diagonal sum'
Array =
{
{1, 2, 4, 5},
{6, 7, 8, 9},
{10, 11, 12, 13},
{14, 15, 16, 17}
}
Sum of elements on major diagonal = 37
Sample output for 5.10
Programming Exercise 5.10 'Sum of all array elements'
Array =
{
{1, 2, 4, 5},
{6, 7, 8, 9},
{10, 11, 12, 13},
{14, 15, 16, 17}
}
Sum of all array elements = 150
Sample output for Extra Exercise
Programming Exercise 'How Many'
The array = {3, 6, 8, 6, 2, 10, 6, 5, 2, 6, 5, 2, 6}
How many occurrences of 6: 5
How many occurrences of 2: 3
How many occurrences of 11: 0
Compiling
Remember that the compile command is "javac", at the unix command prompt.
Compile your code on program.cs.fsu.edu, and then run your programs with
the "java" command.
Submitting:
Submit programs through the submission web page.