Programming 7. Design A Nifty Tool using Single-Dimensional Arrays
In this project, we are to design and implement a Java application that process arguments passed from the command line.
We call this application NiftyTool
. The application treats the very first command line argument as a command that
tells the application the way to process the rest of the command line arguments.
- Sum the arguments. The application should sum up the arguments 4, 8, 9, 92, and print out the result, as the
follow example exhibits:
$ java NiftyTool Sum 4 8 9 92 Sum([4,8,9,92]) -> 113
- Find the average
$ java NiftyTool Average 0 1 2 3 4 5 6 7 8 Average([0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0]) -> 4.000000
- Find the maximum value.
$ java NiftyTool MinValue 4 8 9 92 MinValue([4.0,8.0,9.0,92.0]) -> 4.000000
- Find the maximum element.
$ java NiftyTool MaxElement 4 8 9 92 73 21 MaxElement([4.0,8.0,9.0,92.0,73.0,21.0]) -> 3
- Count lower case letters
$ java NiftyTool CountLowers a b A B c d e a b z Z CountLowers(['a,'b','A','B','c','d','e','a','b','z','Z']) -> {'a':2,'b':2,'c':1,'d':1,'e':1,'f':0,'g':0,'h':0,'i':0,'j':0,'k':0,'l':0,'m':0,'n':0,'o':0,'p':0,'q':0,'r':0,'s':0,'t':0,'u':0,'v':0,'w':0,'x':0,'y':0,'z':1}
- Find the longest runs
$ java NiftyTool LongestRun 1 2 1 1 1 2 1 1 1 1 3 LongestRun([1,2,1,1,1,2,1,1,1,1,3]) -> [6,4,1]
- Reverse
$ java NiftyTool Reverse 0 1 2 3 4 5 6 7 8 Reverse([0,1,2,3,4,5,6,7,8]) -> [8,7,6,5,4,3,2,1,0]
- Swap
$ java NiftyTool "Swap(3,6)" 0 1 2 abc 4 5 6 xyz 8 Swap([0,1,2,abc,4,5,6,xyz,8],3,6) -> [0,1,2,6,4,5,abc,xyz,8]
- Shift
$ java NiftyTool "Shift(2,L)" 0 1 2 3 4 5 6 7 8 Shift([0,1,2,3,4,5,6,7,8],2,L) -> [2,3,4,5,6,7,8,0,1]
- Unrecognized command
$ java NiftyTool "Not a command" Command "Not a command" not yet supported Usage: NiftyTool command ...
- No command given
$ java NiftyTool Usage: NiftyTool command ...
To complete this, you must implement and use a list of Java methods besides any other methods you wish to implement. The relationship of the methods are in the following figure:
The following a description about the list of methods you must implement.
public static void help()
This method prints out a help message. It shall be invoked when the user entered no command line arguments or an unrecognized command line arguments. Below is an example showing the role this method plays in the program.
$ java NiftyTool
Usage: NiftyTool command ...
public static void argSum(String[] args)
This method takes the command line arguments passed in args
, use the getSum
to compute the sum of the int
array extracted from the command line arguments, and then display the
results. This method is invoked when the user enters the Sum
command on the command line, e.g.,
$ java NiftyTool Sum 8 12 8 6
Sum([8,12,8,6]) -> 34
public static void argMinValue(String[] args)
This method takes the command line arguments passed in args
, use the getSmallestValue
method to find the smallest
value from the double
array extracted from the command line arguments, and then display the results. The method
is invoked when the user enters the MinValue
command on the command line, e.g.,
$ java NiftyTool MinValue 9 3 2 9
MinValue([9.0,3.0,2.0,9.0]) -> 2.000000
public static void argMaxElement(String[] args)
This method takes the command line arguments passed in args
, use the getLargestElement
method to find
the index of the largest array element from the double
array extracted from the command line arguments,
and then display the results. The method
is invoked when the user enters the MaxElement
command on the command line, e.g.,
$ java NiftyTool MaxElement 9 3 2 9
MaxElement([9.0,3.0,2.0,9.0]) -> 0
public static void argAverage(String[] args)
This method takes the command line arguments passed in args
, use the getAverage
method to compute
the average of the double
array extracted from the command line arguments,
and then display the results. The method
is invoked when the user enters the Average
command on the command line, e.g.,
$ java NiftyTool Average 9 3 2 9
Average([9.0,3.0,2.0,9.0]) -> 5.750000
public static void argSwap(String[] args)
This method takes the command line arguments passed in args
, use the swapElements
method to
exchange the values of two array elements from
the String
array extracted from the command line arguments,
and then display the results. The method
is invoked when the user enters the Swap
command on the command line, e.g.,
$ java NiftyTool "Swap(3,4)" It is awesome are you doing this.
Swap([It,is,awesome,are,you,doing,this.],3,4) -> [It,is,awesome,you,are,doing,this.]
This example shows that it swaps the values of the array elements at indices 3 and 4. When a command line argument contains parenthesis, you will have to surround it with a pair of quotation marks.
public static void argCountLowers(String[] args)
This method takes the command line arguments passed in args
, use the countLowerCaseLetters
method to
count the occurrences of each lower case English letters from the char
array
extracted from the command line arguments,
and then display the results. The result is an int
array where the value at index 0 is the
number of occurrences of letter 'a'
, the value at index 1 that of ‘b’, and so on. The method
is invoked when the user enters the CountLowers
command on the command line, e.g.,
$ java NiftyTool CountLowers a 1 b A C Z X Y a a a
CountLowers(['a,'1','b','A','C','Z','X','Y','a','a','a']) -> {'a':4,'b':1,'c':0,'d':0,'e':0,'f':0,'g':0,'h':0,'i':0,'j':0,'k':0,'l':0,'m':0,'n':0,'o':0,'p':0,'q':0,'r':0,'s':0,'t':0,'u':0,'v':0,'w':0,'x':0,'y':0,'z':0}
public static void argLongestRun(String[] args)
This method takes the command line arguments passed in args
, use the findLongestRun
method to
find the longest run from the int
array
extracted from the command line arguments,
and then display the results. The result is an int
array where the value at index 0 is the
starting index of the run in the array, that at index 1 the length of the run, and that at
index 2 is the value of the run.
The method
is invoked when the user enters the LongestRun
command on the command line, e.g.,
$ java NiftyTool LongestRun 1 1 2 2 2 2 1 2 1 2 3 3 3 3 3 3 3 1
LongestRun([1,1,2,2,2,2,1,2,1,2,3,3,3,3,3,3,3,1]) -> [10,7,3]
public static void argReverse(String[] args)
This method takes the command line arguments passed in args
, use the reverseArray
method to
reverse the int
array
extracted from the command line arguments,
and then display the results.
The method
is invoked when the user enters the Reverse
command on the command line, e.g.,
$ java NiftyTool Reverse 3 9 12 1
Reverse([3,9,12,1]) -> [1,12,9,3]
public static void argShift(String[] args)
This method takes the command line arguments passed in args
, use the circularShift
method to
circular-shift to the left or the right the int
array
extracted from the command line arguments,
and then display the results.
The method
is invoked when the user enters the Shift
command on the command line. Here is an example:
$ java NiftyTool "Shift(3,L)" 2 4 6 8 10 12 14
Shift([2,4,6,8,10,12,14],3,L) -> [8,10,12,14,2,4,6]
which shows that it shifts the array 3 positions to the left. Here is an another example:
$ java NiftyTool "Shift(2,R)" 2 4 6 8 10 12 14
Shift([2,4,6,8,10,12,14],2,R) -> [12,14,2,4,6,8,10]
which shows that it shifts the array 2 positions to the right.
public static int getSum(int[] numList)
This method is to compute the sum of the int
array numList
and is to be invoked by the
argSum
method.
public static double getSmallestValue(double[] numList)
This method is to find the smallest value of the double
array numList
and is to be
invoked by the argMinValue
method.
public static int getLargestElement(double[] data)
This method is to find the index of the largest array element from double
array data
.
It should be invoked in method argMaxElement
.
public static double getAverage(double[] arr)
This method is to compute the average of array arr
. It should be invoked in method
argAverage
.
public static void swapElements(String[] arr, int i, int j)
This method is to swap values of two array elements at indices i
and j
.
It is invoked in method argSwap
.
public static int[] countLowerCaseLetters(char[] letters)
This method is to count the occurrence of each lower case letters in letters
, and returns
an array containing the counts of the occurrences of letters 'a'
to 'z'
. It is invoked
in method argCountLowers
.
public static int[] findLongestRun(int[] arr)
This method is to find the longest run in array arr
. It returns an array of size 3.
The 3 elements of the returned array are the starting index of the longest run, the length
of the run, and the value of the run. It is invoked in method argLongestRun
.
public static void reverseArray(int[] arr)
This method is to reverse the passed array arr
. It is invoked in method argReverse
.
public static void circularShift(int[] arr, int position, char direction)
This method circular-shifts the array array
to the number of positions in position
along the direction in direction
. The direction
can be either L
or R
indicating
left or right shift. It is invoked in method argShift
.
public static int[] parseArgsToIntArray(String[] args, int begin, int end)
This method is to extract an int
array from the command line arguments args
from index begin
to index end
.
public static double[] parseArgsToDoubleArray(String[] args, int begin, int end)
This method is to extract a double
array from the command line arguments args
from index begin
to index end
.
public static String[] parseArgsToStringArray(String[] args, int begin, int end)
This method is to extract a String
array from the command line arguments args
from index begin
to index end
.
public static char[] parseArgsToCharArray(String[] args, int begin, int end)
This method is to extract a char
array from the command line arguments args
from index begin
to index end
.
public static String arrayToString(int[] arr)
There are several arrayToString
methods. These methods are to be used to
display the results in the methods whose name beginning from arg
. This method
is to form a String
from the int
array arr
. The String begins at '['
and ends at ']'
, and the array values are separated by a ','
.
public static String arrayToString(double[] arr)
This method
is to form a String
from the double
array arr
. The String begins at '['
and ends at ']'
, and the array values are separated by a ','
.
public static String arrayToString(String[] arr)
This method
is to form a String
from the String
array arr
. The String begins at '['
and ends at ']'
, and the array values are separated by a ','
. Each String
here
is not quoted.
public static String arrayToString(char[] arr)
This method
is to form a String
from the char
array arr
. The String begins at '['
and ends at ']'
, and the array values are separated by a ','
. Each char
here
is quoted by single quotes.
public static String letterCountsToString(int[] arr)
This method is invoked in argCountLowers
method to display the results.
It
is to form a String
from the int
array arr
. The String begins at '{'
and ends at '}'
. Since each value in the array represents the count of
an English letter, the value should be represented by a letter, followed by a :
,
and the count. See method argCountLowers
for the example of such a String
.
public static int[] cloneArray(int[] arr)
This method is to make a clone of array arr
. It is invoked by
several methods like argShift
and argReverse
.
public static String[] cloneArray(String[] arr)
This method is to make a clone of array arr
. It is invoked by
several methods like argSwap
.