Skip to content

Instantly share code, notes, and snippets.

@bytecodeman bytecodeman/CharacterData.java Secret
Created Nov 11, 2018

Embed
What would you like to do?
CSC-220 HW7 Recursive Character Frequency Generator
/*
* Name: Prof. Antonio C. Silvestri (STCC)
* Date: 11/12/2018
* Course Number: CSC-220
* Course Name: Data Structures and Algorithms
* Problem Number: HW7
* Email: silvestri@stcc.edu
* Description: Recursive Character Frequency Generator
*/
package charactercount;
public class CharacterData implements Comparable<CharacterData> {
private char ch;
private int count;
public CharacterData(char ch, int count) {
this.ch = ch;
this.count = count;
}
public char getCh() {
return ch;
}
public void setCh(char ch) {
this.ch = ch;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public void addToCount(int number) {
this.count += number;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ch;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
CharacterData other = (CharacterData) obj;
if (ch != other.ch)
return false;
return true;
}
@Override
public String toString() {
return String.format("%c:%d", this.ch, this.count);
}
@Override
public int compareTo(CharacterData arg) {
return this.ch - arg.ch;
}
}
/*
* Name: Prof. Antonio C. Silvestri (STCC)
* Date: 11/12/2018
* Course Number: CSC-220
* Course Name: Data Structures and Algorithms
* Problem Number: HW7
* Email: silvestri@stcc.edu
* Description: Recursive Character Frequency Generator
*/
package charactercount;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class CharacterFrequencyApp {
private static ArrayList<CharacterData> characterCount(String line) {
if (line.equals(""))
return new ArrayList<CharacterData>();
ArrayList<CharacterData> frequency = characterCount(line.substring(1));
int index = frequency.indexOf(new CharacterData(line.charAt(0), 0));
if (index == -1)
frequency.add(new CharacterData(line.charAt(0), 1));
else
frequency.get(index).addToCount(1);
return frequency;
}
//**********************************************
private static void frequencyReport(ArrayList<CharacterData> frequency) {
Collections.sort(frequency);
for (CharacterData cd: frequency) {
System.out.println(cd);
}
}
//**********************************************
private static void process(Scanner sc, String args[]) {
System.out.print("Enter a string: ");
String line = sc.nextLine();
ArrayList<CharacterData> frequency = characterCount(line);
frequencyReport(frequency);
}
//**********************************************
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 = "Character Frequency App 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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.