Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Eliezer Encarnacion
- * Due Date: 11/14/2018
- * Course Number: CSC-220-D01
- * Course Name: Data Structures/Algorithms
- * Problem Number: HW #7
- * Email: eeencarancion0001@student.stcc.edu
- * Recursion Homework. Input a String and returns a report on the frequency of characters in the String.
- */
- import java.util.ArrayList;
- import java.util.Scanner;
- public class HW7Recursion {
- private static void setReport(ArrayList<Report> report, String phrase, int times) {
- int i = times;
- if (i == -1)
- return;
- Report newReport = new Report(phrase.charAt(i), 1);
- int index = report.indexOf(newReport);
- if (index == -1) {
- report.add(newReport);
- setReport(report, phrase, (i - 1));
- }
- else {
- report.get(report.indexOf(newReport)).addToCount();
- setReport(report, phrase, (i - 1));
- }
- }
- // **********************************************
- private static String output(ArrayList<Report> report, int times, String outP) {
- int i = times;
- if (i == 0)
- return outP;
- else {
- outP = outP + report.get(i-1).toString() + "\n";
- return output(report, (i - 1), outP);
- }
- }
- // **********************************************
- private static String getPhrase(Scanner sc) {
- System.out.println("Enter a String: ");
- String phrase = sc.nextLine();
- return phrase;
- }
- // **********************************************
- private static void process(Scanner sc, String args[]) {
- try {
- System.out.println();
- String phrase = getPhrase(sc);
- int times = phrase.length();
- ArrayList<Report> report = new ArrayList<>();
- setReport(report, phrase, (times - 1));
- System.out.print(output(report, report.size(), ""));
- /*for (int i=0; i < report.size(); i++) {
- System.out.println(report.get(i).toString());
- }*/
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- // **********************************************
- private static boolean doThisAgain(Scanner sc, String prompt) {
- System.out.print(prompt);
- String doOver = sc.nextLine();
- return doOver.equalsIgnoreCase("Y");
- }
- // **********************************************
- public static void main(String args[]) {
- final String TITLE = "String and Report Recursion Application V1.0";
- final String CONTINUE_PROMPT = "Do this again? [y/N] ";
- System.out.println("Welcome to " + TITLE);
- Scanner sc = new Scanner(System.in);
- do {
- process(sc, args);
- } while (doThisAgain(sc, CONTINUE_PROMPT));
- sc.close();
- System.out.println("Thank you for using " + TITLE);
- }
- }
Add Comment
Please, Sign In to add comment