Assignment #3 - Arrays
Due: Friday, June 4
In a single class called HW3 (filename HW3.java), write the
methods described below - each works on an array. You will also write a
main method (as described below) to perform tests of your other
methods. I've provided a starter file
HW3.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.
The methods you will write should be named as follows:
- howMany
- isSymmetric
- reverse
- printMatrix
As I will probably be testing this with a program other than your main,
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.
-
Write a method called printMatrix that takes in a two-dimensional
array as a parameter and prints out the array in the format given in this
example:
{
{1, 2, 4, 5},
{6, 7, 8, 9},
{10, 11, 12, 13},
{14, 15, 16, 17}
}
i.e. it should look like a set of sets, arranged roughly as a table
(although the columns do not need to line up -- just one "row" per line).
It's a comma-separated set of rows, each row a comma-separated set of
elements.
-
Write a main method for your class to test your other methods.
Your main method should do the following.
- Prompt the user to input a size (integer)
- Create an array of the given size (assume valid input)
- Call the inputArray method to let the user fill the array
with values
- Print out the initial array
- Call the howMany method on the values 6, 10, and 25, and
print messages that state how many times these numbers appear in the
array
- Call the isSymmetric method and based on the result, print a
message that indicates whether or not the array is the same backwards as
forwards
- Call the reverse method and print the reversed array. Then
print the original array to show that it has not been changed by the
method.
- To test the printMatrix method, simply create the two-dimensional
array shown in the example above (in the description of this method)
and using your method, print the array. (No need for user input on this
test).
Sample Output from 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}
Test of two dimensional array:
{
{1, 2, 4, 5},
{6, 7, 8, 9},
{10, 11, 12, 13},
{14, 15, 16, 17}
}
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.
Since all your code is in one file, you only need to compile the HW3.java
file. You will need the MyInput.java file in the same directory.
Submitting:
To submit this one, we will use the jar utility (instead of tar). jar up
your .class files AND your source code (HW3.java) into one jar
file called hw3.jar. Make your main program runnable from the
jar file. Remember that this means you will need to include a file to be
added to the manifest, which contains the line:
Main-Class: HW3
For example, I packed my jar file with the command:
jar cvmf manifest.mf hw3.jar *.class HW3.java
where "manifest.mf" is the name of my manifest file that I'm adding to the
archive. This packs up my source code, as well as all .class files from
the current directory (you need HW3.class and MyInput.class).
Remember that to run the main program from your jar file, use the command:
java -jar hw3.jar
E-mail this jar file to me (myers@cs.fsu.edu) as an e-mail with a single
attachment. Your e-mail subject should be HW3-SUBMIT. Include your
name and section in the e-mail body. Please only submit ONCE, unless you
make a mistake and need to correct it (before the due date) -- and
minimize this. Only the last submission will be graded.