In: Computer Science
Please carefully review the code to fill in after the given instructions and complete the code for me.
Thank you in advance.
In this problem, we will implement a simple dictionary of common words in the English language, represented as an array of words paired with their lengths. You will need to implement each of the below methods in the Dictionary class. In this problem, the first line of input represents the method to call. It will be one of the following: MIN, MAX, RANGE, AVERAGE, MODE. The second line will represent an integer n, which denotes the number of words in the list. The following n lines will each be a word. The words will not necessarily be sorted. Your output should be a single line representing the results of the method. (a) Implement the minWordLength() method, which should return the length of the smallest word in the list. (b) Implement a method called maxWordLength(), which should return the length of the largest word in the list. (c) Implement a method called wordLengthRange(), which should return the range of lengths in the word list. (d) Implement a method called averageWordLength(), which should return the average word length in the word list. The method should return a string representing the average, accurate to exactly two decimal places (i.e. if the average length is 5, return 5.00). (e) Implement a method called mostCommonWordLength(), which should return the most common length of the words in the list. If there is a tie, you should return -1. You may assume that the length of a word is at most 100 and at least 1.
import java.lang.UnsupportedOperationException;
import java.util.Scanner;
public class Main {
// Constants for function names in input
public static final String MIN_METHOD_NAME = "MIN";
public static final String MAX_METHOD_NAME = "MAX";
public static final String RANGE_METHOD_NAME = "RANGE";
public static final String AVERAGE_METHOD_NAME = "AVERAGE";
public static final String MODE_METHOD_NAME = "MODE";
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Read in the method name that we want to call
String methodName = sc.nextLine();
// Read in number of words
int numWords = Integer.parseInt(sc.nextLine());
// Read in list of words
String[] words = new String[numWords];
for (int i = 0; i < numWords; i++) {
words[i] = sc.nextLine();
}
// Run the specified method
switch (methodName) {
case MIN_METHOD_NAME:
System.out.println(minWordLength(words));
break;
case MAX_METHOD_NAME:
System.out.println(maxWordLength(words));
break;
case RANGE_METHOD_NAME:
System.out.println(wordLengthRange(words));
break;
case AVERAGE_METHOD_NAME:
System.out.println(averageWordLength(words));
break;
case MODE_METHOD_NAME:
System.out.println(mostCommonWordLength(words));
break;
default:
throw new UnsupportedOperationException();
}
}
private static int minWordLength(String[] words) {
// TODO implement this function
throw new UnsupportedOperationException();
}
private static int maxWordLength(String[] words) {
// TODO implement this function
throw new UnsupportedOperationException();
}
private static int wordLengthRange(String[] words) {
// TODO implement this function
throw new UnsupportedOperationException();
}
private static String averageWordLength(String[] words) {
// TODO implement this function
throw new UnsupportedOperationException();
}
private static int mostCommonWordLength(String[] words) {
// TODO implement this function
throw new UnsupportedOperationException();
}
}
Please find the answer below, all the details are mentioned in the comments.
package september_2019; import java.lang.UnsupportedOperationException; import java.util.Scanner; public class Main { // Constants for function names in input public static final String MIN_METHOD_NAME = "MIN"; public static final String MAX_METHOD_NAME = "MAX"; public static final String RANGE_METHOD_NAME = "RANGE"; public static final String AVERAGE_METHOD_NAME = "AVERAGE"; public static final String MODE_METHOD_NAME = "MODE"; public static void main(String[] args) { Scanner sc = new Scanner(System.in); // Read in the method name that we want to call String methodName = sc.nextLine(); // Read in number of words int numWords = Integer.parseInt(sc.nextLine()); // Read in list of words String[] words = new String[numWords]; for (int i = 0; i < numWords; i++) { words[i] = sc.nextLine(); } // Run the specified method switch (methodName) { case MIN_METHOD_NAME: System.out.println(minWordLength(words)); break; case MAX_METHOD_NAME: System.out.println(maxWordLength(words)); break; case RANGE_METHOD_NAME: System.out.println(wordLengthRange(words)); break; case AVERAGE_METHOD_NAME: System.out.println(averageWordLength(words)); break; case MODE_METHOD_NAME: System.out.println(mostCommonWordLength(words)); break; default: throw new UnsupportedOperationException(); } } private static int minWordLength(String[] words) { //store answer as the max value int ans = Integer.MAX_VALUE; //find least length form the array for (int i =0;i<words.length;i++){ if(words[i].length() < ans){ ans = words[i].length(); } } return ans; } private static int maxWordLength(String[] words) { //store answer as min value int ans = Integer.MIN_VALUE; //find the max length from the array for (int i =0;i<words.length;i++){ if(words[i].length() > ans){ ans = words[i].length(); } } return ans; } private static int wordLengthRange(String[] words) { //return the range of the max and min length in the array return maxWordLength(words) - minWordLength(words); } private static String averageWordLength(String[] words) { double total = 0; //get the total of all the values for (int i =0;i<words.length;i++){ total += words[i].length(); } //find the avg of the values double average = total / words.length * 1.0; //return the 2 decimal string as the answer String ans = String.format("%.2f", average); return ans; } private static int mostCommonWordLength(String[] words) { int length_counter[] = new int[101]; int max_length = -1; int max = Integer.MIN_VALUE; //first get the count of all type of length from 1-100 for (int i =0;i<words.length;i++){ length_counter[words[i].length()]++; //store the max value and max_length value if(length_counter[words[i].length()] > max){ max = length_counter[words[i].length()]; max_length = words[i].length(); } } //search if there is a tie then return -1 for(int i=0;i<length_counter.length;i++){ if(length_counter[i] == max){ if(i != max_length) return -1; } } //else return max_length value return max_length; } }
Output:
Min:
Max:
Range:
Average:
Mode(1)
Mode(2), tie in common length