Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Write a java program to count the number of occurences of each letter in a st

ID: 3601316 • Letter: 1

Question

1. Write a java program to count the number of occurences of each letter in a string. The program displays the letters in ascending order.
You should provide two methods. Each method that has an argument of map collection.


Sample output:

Enter a string:
Welcome to CECS 277

Map contains:

Sort by keys
Key Value
c 3
e       3
l        1
m      1
o       2
s       1
t       1
w     1

Sort by values
Key Value
c 3
e       3
o       2
l        1
m      1
s       1
t       1
w     1

2. Rewrite problem 1 by reading the data from a file.

Explanation / Answer

Note : I have a doubt regarding the second question..You want me rewrite the program by reading the data from the file..means should i have to take word by word or do u want to include spaces (Space count also )in map..?Just clear that.I will folow u .thank You.

______________

MapClass.java

import java.util.Map.Entry;
import java.util.*;

public class MapClass {

static Map < Character, Integer > m = new HashMap < Character, Integer > ();

public static void main(String[] args) {
// Declaring variables
String str;

/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);

// Getting the input entered by the user
System.out.print("Enter the String :");
str = sc.nextLine();

// Populating each character into the Map
for (int i = 0; i < str.length(); i++) {
checkMap(str.charAt(i));
}

Set < Entry < Character, Integer >> set = m.entrySet();
List < Entry < Character, Integer >> list = new ArrayList < Entry < Character, Integer >> (
set);

// Sorting based on values
Collections.sort(list, new Comparator < Map.Entry < Character, Integer >> () {
public int compare(Map.Entry < Character, Integer > o1,
Map.Entry < Character, Integer > o2) {
return (o2.getValue()).compareTo(o1.getValue());
}
});

System.out.println("Map contains: ");
System.out.println("Sort by values ");
System.out.println("Key Value");
// Displaying the Key value Pairs
for (Map.Entry < Character, Integer > entry: list) {
System.out.println(entry.getKey() + " " + entry.getValue());
}

// Sorting based on keys
Collections.sort(list, new Comparator < Map.Entry < Character, Integer >> () {
public int compare(Map.Entry < Character, Integer > o1,
Map.Entry < Character, Integer > o2) {
return (o1.getKey()).compareTo(o2.getKey());
}
});

System.out.println("Sort by keys ");
System.out.println("Key Value");
// Displaying the Key value Pairs
for (Map.Entry < Character, Integer > entry: list) {
System.out.println(entry.getKey() + " " + entry.getValue());
}

}

private static void checkMap(char c) {
// Checking whether the word is available in the HashMap
if (m.containsKey(c)) {
m.put(c, m.get(c) + 1);
} else {
// If not available Adding the word to the HashMap
m.put(c, 1);
}

}

}

______________

Output:(This is the world largest single word)

Enter the String :pneumonoultramicroscopicsilicovolcanoconiosis

Map contains:

Sort by values

Key Value

o 9

c 6

i 6

s 4

n 4

l 3

u 2

r 2

a 2

p 2

m 2

v 1

t 1

e 1

Sort by keys

Key Value

a 2

c 6

e 1

i 6

l 3

m 2

n 4

o 9

p 2

r 2

s 4

t 1

u 2

v 1

_________________Thank You