/* "Copyright (c) 2012-2015 by Fritz Sieker." * * Permission to use, copy, modify, and distribute this software and its * documentation for any purpose, without fee, and without written * agreement is hereby granted, provided that the above copyright notice * and the following two paragraphs appear in all copies of this software, * * IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT, * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE AUTHOR * HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" * BASIS, AND THE AUTHOR NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, * UPDATES, ENHANCEMENTS, OR MODIFICATIONS." */ import java.util.ArrayList; import java.util.Scanner; import java.io.FileNotFoundException; import java.util.NoSuchElementException; /** This class provides the interface to the methods you will complete in * MyRecursion.java and provides code for testing the those * methods. Do NOT change anything or add anything to this file */ public abstract class Recursion extends Shell { public void showHelp() { super.showHelp(); System.out.println("Recursion 3 commands:"); System.out.println(" repeat "); System.out.println(" reverse "); System.out.println(" list cslListOfValues"); System.out.println(" contains cslListOfValues searchValue"); System.out.println(" intersect cslListOfValues1 cslListOfValues2"); System.out.println(" diamond "); System.out.println(" dir [indent [maxLevels]]"); } /** Return a String that is a result of repeating the input * value n times. This method can be tested with the * repeat command and will be useful in your directory * listing. * @param value the string to be repeated * @param count the number of times to repeat it. */ public abstract String repeat (String value, int count); /** Return a String that is a result of reversing the input * value. This method can be tested with the reverse command * and will be useful in diamond code. * listing. * @param value the string to be reversed * @return an input of abc will return cba */ public abstract String reverse (String value); /** Print the values of list, one per line. This method may * be tested using the list command. * This method will not be directly used in the directory listing, * but will serve as a guide as to how to write that code. Helper method(s) * may make this code easier to write. * * @param list the list to be printed */ public abstract void list (String[] list); /** Determine whether a value exists in the list. This will be reused * in the MyRecursion.intersect code. This may be tested * with the intersect command. * * @param list the list of values to search * @param value the value to search for (may be null) * @return - true if the value is contained in the list, false otherwise */ public abstract boolean contains (String[] list, String value); /** Print a diamond shape recursively. Using repeat() and * reverse() and taking advantage of the symmetry (horizontai * and vertical) should make the code much simpler. * @param size defines the size of the diamond shape. * For a diamond of size 5 the result is (result has no indentation): *
    *     ***** *****
    *     ****   ****
    *     ***     ***
    *     **       **
    *     *         *
    *     **       **
    *     ***     ***
    *     ****   ****
    *     ***** *****
    * 
*/ public abstract void diamond (int size); /** Find the elements that are in common in the two lists. This is the * intersection of the two lists. The common values must be returned in the * order they occur in list1. * * @param list1 an array of values * @param list2 a second array of values * @return an ArrayList containing all values that appear in both lists. If * no values are in common, return an empty ArrayList. */ public abstract ArrayList intersect (String[] list1, String[] list2); /** Recursively list the contents of a file/directory. For example, the * contents of my csXXX directory could be produce this: *
    * csXXX/
    *   slides/
    *     01_introduction.ppt
    *     02_classes.ppt
    *   code/
    *     week1/
    *       example.java
    *       assignments/
    *         assignment1.pdf
    *         assignment2.pdf
    *       quizzes/
    *         quiz1.pdf
    *
* Note that each directory is printed followed by a /. Had * a maximum depth of 4 been specified, the result would be: *
    * csXXX/
    *   slides/
    *     01_introduction.ppt
    *     02_classes.ppt
    *   code/
    *     week1/
    *       example.java
    *       assignments/
    *       quizzes/
    * 
* In this case the contents of assignments are not listed. *

* If the root file/directory does not exist, print * name: No such file or directory where name * is the name of the root file. * *

To help you, explore the directory structure, use Java's File * class. It contains many usefull methods like: isDirectory(), * exists(), list(), and listFiles(). *

You will use multiple helper method(s) to complete the code * for this method. * * @param fileName the name of the file/directory at the root of the tree * @param indent how many spaces to increase the indent per level. * @param maxLevels do NOT go beyond this level. Level 0 is the * top level. Level 1 contains the children of level 0 and so on. */ public abstract void directory (String fileName, int indent, int maxLevels) ; /** Process one command for this assignment. */ public void processOneCommand (String cmd, String params) throws FileNotFoundException, NoSuchElementException { Scanner scan = new Scanner(params); ArrayList common; String fileName; int indent = 2; int maxLevel = Integer.MAX_VALUE; switch (cmd) { case "repeat": System.out.println(repeat(scan.next(), scan.nextInt())); break; case "reverse": System.out.println(reverse(scan.next())); break; case "list": list(getStrArray(scan)); break; case "contains": System.out.println(contains(getStrArray(scan), (scan.hasNext() ? scan.next() : null))); break; case "intersect": common = intersect(getStrArray(scan), getStrArray(scan)); for (String s : common) System.out.println(s); break; case "diamond": diamond(scan.nextInt()); break; case "dir": fileName = scan.next(); if (scan.hasNextInt()) { indent = scan.nextInt(); if (scan.hasNextInt()) { maxLevel = scan.nextInt(); } } directory(fileName, indent, maxLevel); break; default: super.processOneCommand(cmd, params); } scan.close(); } }