daily pastebin goal
90%
SHARE
TWEET

Untitled

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