Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help with this part of assignment: Use a custom data structure CharInt.java that implements Comparable and Comparator , to provide the correct sorting

I need help with this part of assignment:Use a custom data structure CharInt.java that implements Comparable and Comparator, to provide the correct sorting as described below (i.e. TreeMap is not sorted correctly). We must not use Map.Entry

Here is my code for Character count:

mport java.util.*;

import java.io.*;

public class CharacterCounter {

public static void main(String [] args)throws FileNotFoundException{

Scanner input = new Scanner(new File("prac.txt"));

MapcountMap = new TreeMap();

while(input.hasNext()){

String word = input.next().toLowerCase();

for(int i=0; i

char test = word.charAt(i);

if(countMap.containsKey(test)){

int count = 1 + countMap.get(test);

countMap.put(test,count);

}

else{

countMap.put(test, 1);

}

}

}

System.out.println(countMap);

System.out.println(countMap.size());

}

}

This is the requirments:

Requirements:

DO NOT use Map.Entry nor the entrySet() method (***below)

Keep with the Map concept for toString() output like { c=i } where c is the Character and i is the Integer count of c

Ignore case for alphabetic characters, report just a-z (meaning a-z are the same as A-Z).

Other characters such as tabs, returns, line feeds, spaces (there will be many spaces) also count.

Use the constructor to read a data file (see testing code below)

Provide overloaded (char or int or default) getCounts method that returns a Collection of character counts (see test code below: char returns count of that char, int returns highest of that many or lowest when negative, default() returns the entire Collection). Test code below:

Collection defined: use any List, Set, or Map that makes this easiest for you.

Scanner data = new Scanner(new File("moby.txt")); CharacterCounter working = new CharacterCounter(data); // reads data from file, all the code to read file System.out.println(working.getCounts(3)); // returns highest 3 counts e.g. =199250, e=115002, t=86488... // I counted 199250 space characters in moby.txt file, yes 'e' is the most common char as Wikipedia predicts. System.out.println(working.getCounts('a')); // returns int count of char 'a' (zero if not present) System.out.println(working.getCounts(-3)); // returns a Collection of lowest 3 counts, e.g. $=2, [=2, ]=2 System.out.println(working.getCounts()); // returns the ENTIRE Collection like a Map (like e=9, etc.) crazy long... 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Microsoft SQL Server 2012 Unleashed

Authors: Ray Rankins, Paul Bertucci

1st Edition

0133408507, 9780133408508

More Books

Students also viewed these Databases questions