Question
A student wants an algorithm to find the hardest spelling word in a list of vocabulary. They define hardest by the longest word. Implement the
A student wants an algorithm to find the hardest spelling word in a list of vocabulary. They define hardest by the longest word. Implement the findLongest method to return the longest String stored in the parameter array of Strings named words (you may assume that words is not empty). If several Strings have the same length it should print the first String in list with the longest length. For example, if the following array were declared: String[] spellingList = {"high", "every", "nearing", "checking", "food ", "stand", "value", "best", "energy", "add", "grand", "notation", "abducted", "food ", "stand"}; The method call findLongest(spellingList) would return the String "checking". Use the runner class to test this method: do not add a main method to your code in the U6_L3_Activity_One.java file or it will not be scored correctly. Hint - this algorithm is very similar to the algorithms you have seen to find maximum/minimum values in unit 4. You need a variable which will keep track of the longest word in the array (either directly or as the array index of that word). Start this variable off with a sensible value, update it whenever a longer word is found, then return the longest word at the end.
//RUnner code
import java.util.Scanner; public class runner_U6_L3_Activity_One{ public static void main(String[] args){ Scanner scan = new Scanner(System.in); System.out.println("Enter array length:"); int len = scan.nextInt(); scan.nextLine(); String[] wordList = new String[len]; System.out.println("Enter values:"); for(int i = 0; i < len; i++){ wordList[i] = scan.nextLine(); } System.out.println("Longest word: " + U6_L3_Activity_One.findLongest(wordList)); } }
//Given code
public class U6_L3_Activity_One{
public static String findLongest(String[] words) { //Implement code to find and return the longest String in wordList return ""; }
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started