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

Object Oriented Programming Problem 1 Statement Write a program that reads a fil

ID: 3822027 • Letter: O

Question

Object Oriented Programming

Problem 1 Statement Write a program that reads a file (input.txt) and produces a histogram of the most frequently used words (all in lower case) found in the text. Ignore capitalization, whitespace, and punctuation. Sort the output in order of increasing usage of words. Words with same frequency can appear in any order. Sample Output 0) Frequency word about 1) Frequency 1 word an 2) Frequency 1 word- object 3) Frequency 1 word oriented 4) requency 1 word focused 5) Frequency 1 word, on 6) Frequency 1 word being 7) Frequency 1 word powerful 8 Frequency 1 word but 9) Frequency 1 word simple 10) Frequency-1 word with 11) Frequency 1 word-consistent 12) Frequency-1 Word E clear 13) Frequency-1 word meaning 14) Frequency-1 word it 15 Frequency-1 word more 16) Frequency word important 17) Frequency word that 18) Frequency 1 word programs 19) Frequency word be 20) Frequency-1 word than 21) Frequency-1 word write 22) Frequency-1 word graham 23) Frequency word hamilton

Explanation / Answer

Problem 1:

import java.io.*;
import java.util.*;

public class Main{

public static void main(String[] args) {
try {
int number_of_words = 0;
int i=0;
TreeMap<String, Integer> wordFreqMap = generateWordFrequencyList();
for (String key : wordFreqMap.keySet()) {
number_of_words += wordFreqMap.get(key);
}

for (String key : wordFreqMap.keySet()) {

System.out.println( i +")"+ "Frequency="+wordFreqMap.get(key) +" "+"word=" +key);
i++;
}
} catch (Exception e) {
e.printStackTrace();
}
}
  
/**
Function which will read file and return Tree Map of WOrds with its frequency
*/

public static TreeMap<String, Integer> generateWordFrequencyList()
throws IOException {
// Initialize a tree map
TreeMap<String, Integer> wordsFrequencyMap = new TreeMap<String, Integer>();
String file = "path/to/your/file.txt";
//Redaing file
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while( (line = br.readLine()) != null){
String [] tokens = line.split("\s+");
for (String token : tokens) {
token = removePunctuation(token);
// Find if the word is present in map then increase the frequency else add new record with its frequency as 1
if (!wordsFrequencyMap.containsKey(token.toLowerCase())) {
wordsFrequencyMap.put(token.toLowerCase(), 1);
} else {
int count = wordsFrequencyMap.get(token.toLowerCase());
wordsFrequencyMap.put(token.toLowerCase(), count + 1);
}
}
}
return wordsFrequencyMap;
}

private static String removePunctuation(String token) {
/// If your file contains punctuations other than , and . then include more replace all
token = token.replaceAll("[^a-zA-Z]", "");
return token;
}
}

Problem 2:

import java.util.*;
import java.util.Scanner;

public class MainClass {
public static void main(String[] args) {
int numOfDisks = 0;
System.out.println("Enter the number of Disks on peg
Scanner in =new Scanner (System.in);
numOfDisks= in.nextInt();
moveDisks(numOfDisks, 'A', 'B', 'C');
}

public static void moveDisks(int topDisk, char source, char aux, char dest) {
//if there is only one disk on the peg move that to destination
if (topDisk == 1){
System.out.println("Disk 1 from " + source + " to " + dest);
}else {
// Call move Disks recursively
moveDisks(topDisk - 1, source, dest, aux);
System.out.println("Disk " + topDisk + " from " + source + " to " + dest);
moveDisks(topDisk - 1, aux, source, dest);
}
}
}