Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.31 KB | None | 0 0
  1. package edu.wit.cs.comp1050;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. //TODO: document this class
  6. public class PA3a {
  7.  
  8.     /**
  9.      * Error to output if no command-line arguments supplied
  10.      */
  11.     public static final String ERR_USAGE = "Please supply at least one argument at the command line."; // DO NOT CHANGE
  12.  
  13.     /**
  14.      * Opening quote character
  15.      */
  16.     public static final char QUOTE_CHAR_OPEN = '<'; // DO NOT CHANGE
  17.  
  18.     /**
  19.      * Closing quote character
  20.      */
  21.     public static final char QUOTE_CHAR_CLOSE = '>'; // DO NOT CHANGE
  22.  
  23.     /**
  24.      * Finds an element in a non-empty array that has minimal length. If first is
  25.      * true, the first such; otherwise the last.
  26.      *
  27.      * @param a     input array
  28.      * @param first if true, return the first minimal-length element; otherwise the
  29.      *              last
  30.      * @return element in a that has maximal length
  31.      */
  32.     private static String findShortest(String[] a, boolean first) {
  33.         if (a == null || a.length < 1) {
  34.             return null;
  35.         }
  36.  
  37.         int numberOfStrings = a.length;
  38.         String smallest;
  39.  
  40.         if (first) {
  41.             smallest = a[0];
  42.             for (int i = 1; i < numberOfStrings; i++) {
  43.                 if (a[i].length() < smallest.length()) {
  44.                     smallest = a[i];
  45.                 }
  46.             }
  47.         } else {
  48.             smallest = a[numberOfStrings - 1];
  49.             for (int i = numberOfStrings - 2; i >= 0; i--) {
  50.                 if (a[i].length() < smallest.length()) {
  51.                     smallest = a[i];
  52.                 }
  53.             }
  54.         }
  55.         return smallest; // replace with your code
  56.     }
  57.  
  58.     /**
  59.      * Finds the first of the elements of a supplied array that have the minimal
  60.      * length
  61.      *
  62.      * @param a input array (assumed non-empty)
  63.      * @return first element in a that has minimal length
  64.      */
  65.     public static String firstShortestElement(String[] a) {
  66.         //////////////////////////////////
  67.         // !! DO NOT CHANGE THIS METHOD !!
  68.         //////////////////////////////////
  69.         return findShortest(a, true);
  70.     }
  71.  
  72.     /**
  73.      * Finds the last of the elements of a supplied array that have the minimal
  74.      * length
  75.      *
  76.      * @param a input array (assumed non-empty)
  77.      * @return last element in a that has minimal length
  78.      */
  79.     public static String lastShortestElement(String[] a) {
  80.         //////////////////////////////////
  81.         // !! DO NOT CHANGE THIS METHOD !!
  82.         //////////////////////////////////
  83.         return findShortest(a, false);
  84.     }
  85.  
  86.     /**
  87.      * Finds an element in a non-empty array that has maximal length. If first is
  88.      * true, the first such; otherwise the last.
  89.      *
  90.      * @param a     input array
  91.      * @param first if true, return the first maximal-length element; otherwise the
  92.      *              last
  93.      * @return element in a that has maximal length
  94.      */
  95.     private static String findLongest(String[] a, boolean first) {
  96.         if (a == null || a.length < 1) {
  97.             return null;
  98.         }
  99.  
  100.         int numberOfStrings = a.length;
  101.         String longest;
  102.  
  103.         if (first) {
  104.             longest = a[0];
  105.             for (int i = 1; i < numberOfStrings; i++) {
  106.                 if (a[i].length() > longest.length()) {
  107.                     longest = a[i];
  108.                 }
  109.             }
  110.         } else {
  111.             longest = a[numberOfStrings - 1];
  112.             for (int i = numberOfStrings - 2; i >= 0; i--) {
  113.                 if (a[i].length() > longest.length()) {
  114.                     longest = a[i];
  115.                 }
  116.             }
  117.         }
  118.  
  119.         return longest;
  120.  
  121.     } // replace with your code
  122.  
  123.     /**
  124.      * Finds the first of the elements of a supplied array that have the maximal
  125.      * length
  126.      *
  127.      * @param a input array (assumed non-empty)
  128.      * @return first element in a that has maximal length
  129.      */
  130.     public static String firstLongestElement(String[] a) {
  131.         //////////////////////////////////
  132.         // !! DO NOT CHANGE THIS METHOD !!
  133.         //////////////////////////////////
  134.         return findLongest(a, true);
  135.     }
  136.  
  137.     /**
  138.      * Finds the last of the elements of a supplied array that have the maximal
  139.      * length
  140.      *
  141.      * @param a input array (assumed non-empty)
  142.      * @return last element in a that has maximal length
  143.      */
  144.     public static String lastLongestElement(String[] a) {
  145.         //////////////////////////////////
  146.         // !! DO NOT CHANGE THIS METHOD !!
  147.         //////////////////////////////////
  148.         return findLongest(a, false);
  149.     }
  150.  
  151.     /**
  152.      * Returns an element with a supplied length (if it exists, else null). If first
  153.      * is true, the first such element; else the last.
  154.      *
  155.      * @param a     input array
  156.      * @param len   length of element for which to search
  157.      * @param first if true searches for first; else searches for last
  158.      * @return element with length len in a according to first
  159.      */
  160.     private static String findOfLength(String[] a, int len, boolean first) {
  161.         if (a == null || a.length < 1) {
  162.             return null;
  163.         }
  164.  
  165.         int numOfStrings = a.length;
  166.         String last = null;
  167.  
  168.         if (first) {
  169.             for (int i = 0; i < numOfStrings; i++) {
  170.                 if (a[i].length() == len) {
  171.                     last = a[i];
  172.                     break;
  173.                 }
  174.             }
  175.  
  176.         }
  177.         return last; // replace with your code
  178.     }
  179.  
  180.     /**
  181.      * Returns the first element of a supplied array that has the supplied length
  182.      * exactly, or null if not found
  183.      *
  184.      * @param a   input array
  185.      * @param len length of element for which to search
  186.      * @return first element with length len, or null if not found
  187.      */
  188.     public static String firstOfLength(String[] a, int len) {
  189.         //////////////////////////////////
  190.         // !! DO NOT CHANGE THIS METHOD !!
  191.         //////////////////////////////////
  192.         return findOfLength(a, len, true);
  193.     }
  194.  
  195.     /**
  196.      * Returns the last element of a supplied array that has the supplied length
  197.      * exactly, or null if not found
  198.      *
  199.      * @param a   input array
  200.      * @param len length of element for which to search
  201.      * @return last element with length len, or null if not found
  202.      */
  203.     public static String lastOfLength(String[] a, int len) {
  204.         //////////////////////////////////
  205.         // !! DO NOT CHANGE THIS METHOD !!
  206.         //////////////////////////////////
  207.         return findOfLength(a, len, false);
  208.     }
  209.  
  210.     /**
  211.      * Returns the sum of the lengths of all the elements in the supplied string
  212.      * array
  213.      *
  214.      * @param a input array
  215.      * @return summation over the length of all strings in a
  216.      */
  217.     public static int sumOfElementLengths(String[] a) {
  218.         if (a == null || a.length < 1) {
  219.             return 0;
  220.         }
  221.  
  222.         int sum = 0;
  223.         for (String s : a) {
  224.             sum += s.length();
  225.         }
  226.  
  227.         return sum;
  228.     }// replace with your code
  229.  
  230.     /**
  231.      * Given a sum and count, returns the average as an integer naturally rounded
  232.      *
  233.      * @param sum   elements summed
  234.      * @param count count of elements
  235.      * @return rounded average of sum and count
  236.      */
  237.     public static int averageAsInt(int sum, int count) {
  238.         return (int) Math.round((double) sum / count); // replace with your code
  239.     }
  240.  
  241.     /**
  242.      * Returns true if the supplied array has two elements with the same contents
  243.      * (not necessarily same identity)
  244.      *
  245.      * @param a array to examine
  246.      * @return true if a contains duplicate elements
  247.      */
  248.     public static boolean hasDuplicates(String[] a) {
  249.         if (a == null || a.length < 1) {
  250.             return false;
  251.         }
  252.         ArrayList<String> newList = new ArrayList<String>();
  253.  
  254.         newList.add(a[0]);
  255.  
  256.         boolean flag = false;
  257.  
  258.         for (int i = 1; i < a.length; i++) {
  259.  
  260.             if (newList.contains(a[i])) {
  261.  
  262.                 flag = true;
  263.  
  264.                 break;
  265.  
  266.             } else {
  267.  
  268.                 newList.add(a[i]);
  269.  
  270.             }
  271.  
  272.         }
  273.  
  274.         return flag;
  275.     }// replace with your code
  276.  
  277.     /**
  278.      * Utility method to quote a supplied string using supplied open/close
  279.      * characters
  280.      *
  281.      * @param s      string to quote
  282.      * @param cOpen  character before the string
  283.      * @param cClose character after the string
  284.      * @return quoted string
  285.      */
  286.     public static String flexibleQuote(String s, char cOpen, char cClose) {
  287.         if (s == null) {
  288.             return null;
  289.         }
  290.  
  291.         StringBuilder last = new StringBuilder();
  292.         last.append(cOpen);
  293.         last.append(s);
  294.         last.append(cClose);
  295.         return last.toString();
  296.     }
  297.     // replace with your code
  298.  
  299.     /**
  300.      * Utility method for printing the first and last Strings from a list. Rules: -
  301.      * If both are null, return "none" - If either are null, return the other
  302.      * (quoted) - If the same string is provided twice (identity, not content);
  303.      * return either (quoted) - Otherwise return "first=quoted(sFirst),
  304.      * last=quoted(sLast)"
  305.      *
  306.      * Characters for quoting are supplied as arguments
  307.      *
  308.      * @param sFirst first string
  309.      * @param sLast  last string
  310.      * @param cOpen  open character for quoting
  311.      * @param cClose close character for quoting
  312.      * @return string according to the rules above
  313.      */
  314.     public static String oneOrBothOrNone(String sFirst, String sLast, char cOpen, char cClose) {
  315.         StringBuilder last = new StringBuilder();
  316.         if (sFirst == null && sLast == null) {
  317.             return "none";
  318.         } else if (sFirst == null) {
  319.             return flexibleQuote(sLast, cOpen, cClose);
  320.         } else if (sLast == null) {
  321.             return flexibleQuote(sFirst, cOpen, cClose);
  322.         } else if (sFirst == sLast) {
  323.             return flexibleQuote(sLast, cOpen, cClose);
  324.         } else {
  325.             last.append("first=");
  326.             last.append(flexibleQuote(sFirst, cOpen, cClose));
  327.             last.append(", last=");
  328.             last.append(flexibleQuote(sLast, cOpen, cClose));
  329.             return last.toString();
  330.         }
  331.     }
  332.     // replace with your code
  333.  
  334.     /**
  335.      * Utility method to print (to the terminal) an array of Strings given something
  336.      * before the list, something after, and something between each element
  337.      *
  338.      * @param a       elements
  339.      * @param sBefore what to print before the list
  340.      * @param sSep    what to print between each element
  341.      * @param sAfter  what to print after the list
  342.      */
  343.     public static void printWithSeparator(String[] a, String sBefore, String sSep, String sAfter) {
  344.  
  345.         System.out.print(sBefore);
  346.  
  347.         for (int i = 0; i < a.length; i++) {
  348.  
  349.             System.out.print(a[i] + sSep);
  350.  
  351.         }
  352.  
  353.         System.out.print(sAfter);
  354.  
  355.     }
  356.  
  357.     // replace with your code
  358.  
  359.     /**
  360.      * Returns true if command-line arguments were supplied; false otherwise
  361.      *
  362.      * @param args program arguments
  363.      * @return true if input is of non-zero length
  364.      */
  365.     public static boolean validArgs(String[] args) {
  366.         if (args.length > 0) {
  367.  
  368.             return true;
  369.  
  370.         }
  371.         return false; // replace with your code
  372.     }
  373.  
  374.     /**
  375.      * Outputs a small analysis of strings supplied as command-line arguments to the
  376.      * program
  377.      *
  378.      * @param args command-line arguments
  379.      */
  380.     public static void main(String[] args) {
  381.         //////////////////////////////////
  382.         // !! DO NOT CHANGE THIS METHOD !!
  383.         //////////////////////////////////
  384.  
  385.         if (!validArgs(args)) {
  386.             System.out.printf("%s%n", ERR_USAGE);
  387.             System.exit(1);
  388.         }
  389.  
  390.         //
  391.  
  392.         System.out.printf("Arguments (%d, %s duplication):", args.length, (hasDuplicates(args)) ? ("has") : ("no"));
  393.         printWithSeparator(args, " ", " ", String.format("%n"));
  394.  
  395.         //
  396.  
  397.         final int lSum = sumOfElementLengths(args);
  398.         final int lAvg = averageAsInt(lSum, args.length);
  399.         System.out.printf("Length: total=%d, avg=%d%n", lSum, lAvg);
  400.  
  401.         //
  402.  
  403.         final String fShortest = firstShortestElement(args);
  404.         final String lShortest = lastShortestElement(args);
  405.         System.out.printf("Shortest (%d): %s%n", fShortest.length(),
  406.                 oneOrBothOrNone(fShortest, lShortest, QUOTE_CHAR_OPEN, QUOTE_CHAR_CLOSE));
  407.  
  408.         //
  409.  
  410.         final String fLongest = firstLongestElement(args);
  411.         final String lLongest = lastLongestElement(args);
  412.         System.out.printf("Longest (%d): %s%n", fLongest.length(),
  413.                 oneOrBothOrNone(fLongest, lLongest, QUOTE_CHAR_OPEN, QUOTE_CHAR_CLOSE));
  414.  
  415.         //
  416.  
  417.         final String fAverage = firstOfLength(args, lAvg);
  418.         final String lAverage = lastOfLength(args, lAvg);
  419.         System.out.printf("Average (%d): %s%n", lAvg,
  420.                 oneOrBothOrNone(fAverage, lAverage, QUOTE_CHAR_OPEN, QUOTE_CHAR_CLOSE));
  421.     }
  422.  
  423. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement