Guest User

Untitled

a guest
Nov 14th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. /*
  2. * Eliezer Encarnacion
  3. * Due Date: 11/14/2018
  4. * Course Number: CSC-220-D01
  5. * Course Name: Data Structures/Algorithms
  6. * Problem Number: HW #7
  7. * Recursion Homework. Input a String and returns a report on the frequency of characters in the String.
  8. */
  9.  
  10. import java.util.ArrayList;
  11. import java.util.Scanner;
  12.  
  13. public class HW7Recursion {
  14.  
  15. private static void setReport(ArrayList<Report> report, String phrase, int times) {
  16. int i = times;
  17.  
  18. if (i == -1)
  19. return;
  20.  
  21. Report newReport = new Report(phrase.charAt(i), 1);
  22. int index = report.indexOf(newReport);
  23.  
  24. if (index == -1) {
  25. report.add(newReport);
  26. setReport(report, phrase, (i - 1));
  27. }
  28. else {
  29. report.get(report.indexOf(newReport)).addToCount();
  30. setReport(report, phrase, (i - 1));
  31. }
  32. }
  33.  
  34. // **********************************************
  35.  
  36. private static String output(ArrayList<Report> report, int times, String outP) {
  37. int i = times;
  38.  
  39. if (i == 0)
  40. return outP;
  41. else {
  42. outP = outP + report.get(i-1).toString() + "\n";
  43. return output(report, (i - 1), outP);
  44. }
  45. }
  46.  
  47. // **********************************************
  48.  
  49. private static String getPhrase(Scanner sc) {
  50. System.out.println("Enter a String: ");
  51. String phrase = sc.nextLine();
  52. return phrase;
  53. }
  54.  
  55. // **********************************************
  56.  
  57. private static void process(Scanner sc, String args[]) {
  58.  
  59. try {
  60. System.out.println();
  61. String phrase = getPhrase(sc);
  62. int times = phrase.length();
  63. ArrayList<Report> report = new ArrayList<>();
  64. setReport(report, phrase, (times - 1));
  65. System.out.print(output(report, report.size(), ""));
  66.  
  67. /*for (int i=0; i < report.size(); i++) {
  68. System.out.println(report.get(i).toString());
  69. }*/
  70.  
  71. } catch (Exception ex) {
  72. ex.printStackTrace();
  73. }
  74.  
  75. }
  76.  
  77. // **********************************************
  78.  
  79. private static boolean doThisAgain(Scanner sc, String prompt) {
  80. System.out.print(prompt);
  81. String doOver = sc.nextLine();
  82. return doOver.equalsIgnoreCase("Y");
  83. }
  84.  
  85. // **********************************************
  86.  
  87. public static void main(String args[]) {
  88. final String TITLE = "String and Report Recursion Application V1.0";
  89. final String CONTINUE_PROMPT = "Do this again? [y/N] ";
  90.  
  91. System.out.println("Welcome to " + TITLE);
  92. Scanner sc = new Scanner(System.in);
  93.  
  94. do {
  95. process(sc, args);
  96. } while (doThisAgain(sc, CONTINUE_PROMPT));
  97. sc.close();
  98. System.out.println("Thank you for using " + TITLE);
  99. }
  100.  
  101. }
Add Comment
Please, Sign In to add comment