2.17 LAB: Word frequencies - methods Write a program that reads a list of words. Then, the program outputs those words and their frequencies. The input begins with an integer indicating the number of words that follow. Assume that the list will always contain less than 20 words.

Respuesta :

Using the knowledge in computational language in JAVA it is possible to write a code that reads a list of words.

Writting the code in JAVA:

import java.util.*;

//class name LabClass

public class LabClass{

   //function getFrequencyOfWord with array wordList, ListSize and currWord as parameter

   public static int getFrequencyOfWord(String[] wordsList, int ListSize, String currWord){

       int frequency = 0;

       //for loop for iteration

       for(int i=0; i<ListSize; i++){

           //if current word matches with words present in list

           //then increase the frequency by 1

           if(wordsList[i].compareTo(currWord)==0){

               frequency++;

           }

       }

       //return frequency at last

       return frequency;

   }

   //main function

   public static void main(String[] args) {

       //creating object of Scanner class

       Scanner sc = new Scanner(System.in);

       int size;

       //asking user to input size of list

       System.out.println("Enter the size of list :");

       size = sc.nextInt();

       //creating array of same size as user input size

       String[] wordList = new String[size];

       sc.nextLine();

       //asking user to input array elements

       System.out.println("Enter list elements one by one :");

       for(int i =0; i<size; i++)

       {

           wordList[i] = sc.nextLine();

       }

       System.out.print("\n");

       sc.close();

       //calling function getFrequencyOfWord in every iteration by passing

       //required arguments to it

       for(int i=0; i<size; i++)

       {

           String currWord = wordList[i];

           int count = getFrequencyOfWord(wordList, size, currWord);

           //printing result

           System.err.println(currWord + " : " + count);

       }

   }

}

See more about JAVA at brainly.com/question/12978370

#SPJ1

Ver imagen lhmarianateixeira
ACCESS MORE
ACCESS MORE
ACCESS MORE
ACCESS MORE