Programming Assignment 6 – Arrays and Main

Due Tuesday February 22 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-w22/cse11-pa6-starter

Submission

Submit the following files to Gradescope in the PA6 assignment:

FAQ

  1. I wrote test methods with the Tester, but ./run is telling me that no tests ran.
    • Tester methods have to start with “test” at the beginning! For example, boolean testAdd(Tester t) { …. }. In ArrayExamples.java, all tests should be in class ArrayExamples, not Pair.

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.

“Inclusive” is another way of saying to use ≤ instead of < and ≥ instead of >
Computer scientists have a fancy name for alphabetical: lexicographic. You will need the compareTo method on Strings here. Try it out on a few examples if you're not sure what it will do!

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

When comparing Strings, the == 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:

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:

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:

  1. One command option between sum, product, mean, max, min, positive, negative, or count and a list of at least 3 integers
  2. One command option between greater, lesser, or equal and a list of at least 3 integers
  3. The -l or -list option followed by one of any command option and a list of at least 3 integers
  4. The -l or -list option 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