Guest User

Untitled

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