Programming Assignment 6 – Arrays and Main
Due Tuesday May 17 10:00pm Pacific Time
In this assignment, you will get practice with writing methods that calculate values from arrays using loops.
Different assignments in this course have different collaboration policies. On this assignment, you can collaborate with anyone in the course, including sharing code. In your submission, give credit to all students and course staff who helped you with this assignment by noting their name and how you used their ideas or work. Note that using someone’s work without giving credit to them is a violation of academic integrity.
The starter code is available at:
https://github.com/ucsd-cse11-sp22/cse11-pa6-starter
Submission
Submit the following files to Gradescope in the PA6 assignment:
ArrayExamples.javaCmdTool.javaCmdTool-transcript.txt
FAQ
- I wrote test methods with the Tester, but 
./runis telling me that no tests ran.- Tester methods have to start with “test” at the beginning! For example,
 
boolean testAdd(Tester t) { …. }. InArrayExamples.java, all tests should be in classArrayExamples, notPair. 
 - Tester methods have to start with “test” at the beginning! For example,
 
 
Array Methods
In a file called ArrayExamples.java, write the following methods in a class
called ArrayExamples. For each, write three tests (a test is a use of
checkExpect) where each of the three has a different length of array used in
the input, one of which tests an empty array (if it is allowed as an input). All
of these methods should be static.
- 
    
Write a
staticmethod calledjoinWiththat takes an array ofStringand aStringseparator, and returns a newStringthat contains the strings from the array separated by that separator. For example, for an array containing"a","b", and"c"with separator":", the result would be"a:b:c"(note that there’s no colon at the end, just in between the elements). If the input array is empty, produces the empty string. - 
    
Write a
staticmethod calledsomethingFalsethat takes an array ofbooleanand returnstrueif at least one of the elements in the array isfalse. If the array is empty, producesfalse. 
- 
    
Write a
staticmethod calledcountWithinRangethat takes an array ofdoubleand two otherdoubles calledlowandhigh, and returns the count of elements of the array that are betweenlowandhigh(both inclusive). If the array is empty, this should produce0. You can assume thatlow≤high - 
    
Write a
staticmethod callednumsWithinRangethat takes an array ofdoubleand two otherdoubles calledlowandhighand returns an array ofdoublethat contains all the elements in the array that are betweenlowandhigh(inclusive). If the array is empty, this should produce a new empty array. You can assume thatlow≤high. Hint: UsecountWithinRangeto help you construct the new array. - 
    
Write a class called
Pairwith twointfields,aandb, and include the usual initializing constructor. (AddPairat the top level, outside theArrayExamplesclass). Then write astaticmethod (inArrayExamples, not inPair) calledmaxminthat takes an array ofintand returns aPairwhere theafield is set to the smallest integer in the array and thebis set to the largest. Assume the array has at least one element. 
- Write a 
staticmethod calledearliestthat takes an array ofStrings and returns theStringthat is the earliest alphabetically. You can assume that the array has at least one element, and that if there is a tie you should return the one at the earliest index. 
Below are a few tests to get you started. We designed these to work on their own in a separate class. You must include them all in your final submission (it helps us check that basic things work when reviewing your code). But these tests don’t cover all cases, which is why you must write your own as well.
import tester.*;
class ProvidedArrayExamples {
  void testJoinWith(Tester t){
    String[] example1 = {"a", "b","c"};
    t.checkExpect(ArrayExamples.joinWith(example1, ":"), "a:b:c");
  }
  void testSomethingFalse(Tester t){
    boolean[] example1 = {true, false};
    t.checkExpect(ArrayExamples.somethingFalse(example1), true);
  }
  void testCountWithinRange(Tester t){
    double[] example = {0.1, 1.3, 2.6};
    t.checkExpect(ArrayExamples.countWithinRange(example, 1.1, 2.2), 1);
  }
  void testNumsWithinRange(Tester t){
    double[] example = {0.0, 3.0, 1.4, 1.5, 2.7, 9.1, 2.1};
    double[] expected = {1.4, 1.5, 2.1};
    t.checkExpect(ArrayExamples.numsWithinRange(example, 1.1, 2.2), expected);
  }
  void testMaxmin(Tester t){
    int[] example = {4, 5, 2, 3, 1};
    t.checkExpect(ArrayExamples.maxmin(example), new Pair(1, 5));
  }
  void testEarliest(Tester t){
    String[] example = {"aa", "aab", "abcd", "a"};
    t.checkExpect(ArrayExamples.earliest(example), "a");
  }
}
Using Main and Command-line Arguments
- 
    
In a file called
CmdTool.java, write a class calledCmdTool. It should have a main method which will allow CmdTool to do different types of operations depending on the command line arguments given. It will print its output to the terminal in a single line.In all cases, it can assume that there will be at least two command-line arguments (the name of an operation and at least one number expressed as 0, a positive integer, or negative integer).
 
== operator can
be unreliable. Instead use .equals or .compareTo,
which are in the Java String documentation.You will be responsible for implementing the following 12 command options:
"sum", computes the sum of an array of numbers. If there are no numbers, then returns aString[]containing 0"product", computes the product of an array of integers. If there are no integers, then returns aString[]containing 1."mean", computes the mean of an array of integers. The mean itself may be a double. If there are no integers, then returns aString[]containing 0"max", computes the maximum of an array of integers. If there are no integers, then returns an empty array"min", computes the minimum of an array of integers. If there are no integers, then returns an empty array"positive", filters the given array to include only strictly positive integers. Zero is not included. If there are no integers that are strictly positive, then returns an empty array."negative", filters the given array to include only strictly negative integers. Zero is not included. If there are no integers that are strictly negative, then returns an empty array"count", computes the size of an array of integers."greater #", filters the given array to include integers that are strictly greater than#. If there are no integers that are strictly greater than#, then returns an empty array."lesser #", filters the given array to include integers that are strictly lesser than#. If there are no integers that are strictly lesser than#, then returns an empty array"equal #", filters the given array to include integers that are equal to#. If there are no integers that are equal to#, then returns an empty array"-l c1 c2 ... cn"and"-list c1 c2 ... cn", given one or more command options, returns the array that is the result of executing commandc1, thenc2, until commandcn. One command line will only have one or-lor-listand this option will always be the first option if it is used.
As this portion of PA6 will be significantly longer than the prior PAs, it is recommended that you decompose this problem and work step by step. The PA writeup will guide you through the recommended process to approach this task.
To assist you in completing this task, we introduce the Command interface:
  interface Command {
      // Takes a String[] that contains the initial data
      // and returns a String[] that contains the 
      // data that results from executing a command
      String[] execute(String[] data);
  }
It is required that you create several small classes, each corresponding to a single command option, that implement the Command interface. You are free to implement these classes however you want, including adding fields and/or helper methods. These small classes and their corresponding execute method will be tested separate from the functionality of CmdTool.
These classes will be:
class Sum { ... }which corresponds to thesumcommand optionclass Product { ... }which corresponds to theproductcommand optionclass Mean { ... }which corresponds to themeancommand optionclass Max { ... }which corresponds to themaxcommand optionclass Min { ... }which corresponds to themincommand optionclass Count { ... }which corresponds to thecountcommand optionclass Positive { ... }which corresponds to thepositivecommand optionclass Negative { ... }which corresponds to thenegativecommand optionclass Greater { ... }which corresponds to thegreatercommand option. You will use the String representation of#for the constructor.class Lesser { ... }which corresponds to thelessercommand option. You will use the String representation of#for the constructor.class Equal { ... }which corresponds to theequalcommand option. You will use the String representation of#for the constructor.class CmdList { ... }which corresponds to the-land-listcommand options. You will use aCommand[]for the constructor
Here is a list of recommended progress milestones that may help you while implementing CmdTool. It is not required that you follow these milestones. Be aware that as you progress through the different milestones, you should not be losing any functionality.
### Milestone 1
Your program should take in one command option between sum, product, mean, max, min, positive, negative, and count and a list of integers on the command line and print out the result.
  $ javac CmdTool.java
  
  $ java CmdTool sum 1 2 3
  6
  
  $ java CmdTool product 1 2 3
  6
  
  $ java CmdTool mean 1 2 3
  2.0
  
  $ java CmdTool max 1 2 3
  3
  
  $ java CmdTool min 1 2 3
  1
  
  $ java CmdTool positive -1 0 1 2 3
  1 2 3
  
  $ java CmdTool negative -1 0 1 2 3
  -1
  
  $java CmdTool count 1 -1 0
  3
### Milestone 2
  Your program should take in one command option between greater, lesser, and equal and a list of integers on the command line and print out the result
  $ javac CmdTool.java
  $ java CmdTool greater 1 1 2 3
  2 3
  $ java CmdTool lesser 2 1 2 3
  1
  $ java CmdTool equal 3 3 1 2 3
  3 3
### Milestone 3
Your program should take in the -l or -list option and one of any command option and a list of integers on the command line and print out the result.
  $ javac CmdTool.java
  $ java CmdTool -l greater -2 0 1 2 3
  0 1 2 3
  $ java CmdTool -l sum 1 2 -2 -1
  0
  $ java CmdTool -list positive -3 -2 -1 0
  $ java CmdTool -list mean 1 2 3 4
  2.5
### Milestone 4
Your program should take in the -l or -list option and any number of any of the command options in any order and a list of integers on the command line and print out the result.
  $ javac CmdTool.java
  $ java CmdTool -l sum greater -1 -1 0 1 2
  2
  $ java CmdTool -l sum sum sum sum sum sum 1 2 3
  6
  $ java CmdTool -list greater 0 negative -1 0 1
  $ java CmdTool -list positive mean -1 -2 -3 9 8 7
  8.0
Here are some helper methods that you may find helpful:
  // Prints the contents of a String[] on one line
  void printArray(String[] data);
  // Returns the number of command options on the command line
  int countCmds(String[] args);
  // Returns a String[] containing only the integer data
  String[] processCmdData(String[] args);
  // Returns a Command object corresponding to a command option
  Command processCmd(String[] args);
  // Returns a Command[] containing Command objects corresponding
  // to all the command options on the command line
  Command[] processCmdList(String[] args);
## Testing
In a file you create called CmdTool-transcript.txt, include at least 2 of your own tests (different from the provided tests for Milestones 1 - 4) of your CmdTool implementation running each of the following situations. You will include both the command line and the output:
- One command option between 
sum,product,mean,max,min,positive,negative, orcountand a list of at least 3 integers - One command option between 
greater,lesser, orequaland a list of at least 3 integers - The 
-lor-listoption followed by one of any command option and a list of at least 3 integers - The 
-lor-listoption followed by at least 2 command options and a list of at least 3 integers. Your 2 tests for this situation should not have the same amount of command options after-l 
Example of CmdTool-transcript.txt:
  java CmdTool sum 1 2 3
  6
  java CmdTool max 1 2 3
  3
  java CmdTool greater 1 1 2 3
  2 3
  java CmdTool equal 3 3 1 2 3
  3 3
  java CmdTool -l sum greater -1 -1 0 1 2
  2
  java CmdTool -l sum sum sum sum sum sum 1 2 3
  6
 Schedule
 Calendar
 Syllabus
 Questions
 Material
 Assignments
 Help Hours