CS161 Assignment 1: Review of CS160 Material
Loops and Arrays
Due - 9/2 at 3:00pm

Version 1.0

Objective

The objective of this assignment is to refresh your knowledge of how to solve simple programming problems involving conditionals, loops, nested loops, and array indexing.

Details

The assignment program consists of a client class Assign1F16.java and a java program Assign1.java that you need to complete. Assign1F16 exercises your Assign1.java code. Read but don't change Assign1F16.java. We will use different clients to test your code. Assign1.java contains a Boolean instance variable debug, that can be used to create debug output. By turning debug to false no debug output must be printed (but you should leave your debug code in your programs, so you can reuse it later.) There is some helper code in Assign1.java. Don't change it. Download Assign1F16.java, Assign1.java and the input file in and import them in a java project in Eclipse. Assign1F16.java is a client program. It creates an Assign1 object and exercises its methods. You can read it and play with it, but you don't need to change it or submit it. Your job is to finish methods in Assign1.java.

For Assign1, set the command line arguments to filename sizeA sizeB sizeT sizeS, eg "in 3 5 3 5". Now you can run the program. It will mainly print "Method not implemented" messages. Your job is to remove (or comment out) the "method not implemented yet" messages and implement the following methods in Assign1.java:

  1. main() Use main to set debug and test your individual methods.

  2. isOdd(int n) returns true if n is odd, false otherwise. Make sure it deals with positive, zero, and negative inputs.

  3. print3Tuples(int n) Print all ordered 3 tuples (i,j,k) i in 0..(n-1), j in 0 .. (n-1), k in 0..(n-1), in lexicographical order, one 3 tuple per line. Lexicographical order (think about words in a dictionary), orders sequences: the first UNEQUAL element in the two sequences determines the order, e.g.: (0,1,5,3) < (0,1,6,0).
    Format: open-paren number comma number comma number close-paren. Use the print3Tuple() method provided to print, so that we all print in the same format. (Think about this: how would you have to change your code to print 4-tuples, 5-tuples, k-tuples?)
    Example: for n=2, your output should be
    (0,0,0)
    (0,0,1)
    (0,1,0)
    (0,1,1)
    (1,0,0)
    (1,0,1)
    (1,1,0)
    (1,1,1)
    
  4. print3Subsets(int n) Print all subsets size 3 {i,j,k} i in 0..(n-1), j in 0 .. (n-1), k in 0..(n-1), in lexicographical order, one 3 subset per line. (Think about this: how would you have to change your code to print 4 sets, 5 sets, k sets?)

    Notice {i,j,k} = {i,k,j} = {j,i,k} .. only the lexicographically first is printed.
    Format: open-brace number comma number comma number close-brace. Use the print3Set() method provided to print, so that we all print in the same format.
    Example: for n=4, your output should be
    {0,1,2}
    {0,1,3}
    {0,2,3}
    {1,2,3}
    

  5. int[]shuffle(int[]A, int[]B) Return an array of ints, which are the shuffled values of A and B: A[0], B[0], A[1], B[1], ...
    Do not change A or B. When A and B are of different length, the rest of the longest array is appended to the result, eg shuffle({1,2,3},{10,20,30,40,50}) returns {1,10,2,20,3,30,40,50} and shuffle({1,2,3,4,5},{10,20}) returns {1,10,2,20,3,4,5}

  6. int[] makeCumul(int[] in) Return an array of ints, which are the cumulative sums if the array in: in[0], in[0]+in[1], in[0]+in[1]+in[2],...
    For example, makeCumul({0,2,3,-1,-1}) returns {0,2,5,4,3}. Do not change array in.

Make your own test input files and play with them. Test Assign1.java, first using your own test code in Assign1.java's main method, then using Assign1F16.java, then using the test files provided.

Submitting Your Assignment

Submit your Assign1.java source code, using the checkin test and submit website.