Answer:
Below is the JAVA program:
import java.util.Scanner;
//to take input from user
public class Main { //name of the class
public static void main(String[] args) { //beginning of main method
Scanner input = new Scanner(System.in); //creates Scanner instance
int integer = input.nextInt(); //defines and collects an integer for the number of words
String stringArray[] = new String[integer];
//initializes an array to hold strings
for (int i = 0; i < integer; i++) { //iterates through the array
stringArray[i] = input.next(); } //collects strings
int frequency[] = new int[integer]; //creates an array for holding frequencies
int count;
//initializes variable to calculate frequency of each word
for (int i = 0; i < frequency.length; i++) { //iterates through frequency array
count = 0; //sets count to zero
for (int j = 0; j < frequency.length; j++) { //iterates through array
if (stringArray[i].equals(stringArray[j])) { //checks if element at ith index matches jth index
count++; } } //increments count
frequency[i] = count; } //stores count in the frequency array
for (int i = 0; i < stringArray.length; i++) { //iterates through the string array
System.out.println(stringArray[i] + " " + frequency[i]); } } } //displays each word and its frequency
Explanation:
To illustrate the program, consider:
let integer = 3
for (int i = 0; i < integer; i++) is used to input strings into stringArray.
On the first pass:
i = 0
0<3
stringArray[i] = input.next(); takes a word and saves it to the ith index of stringArray. For example, if the user types "hey", it will be in stringArray[0].
Then, i increments to i = 1
On the second pass:
i = 1
1<3
stringArray[i] = input.next(); takes another word and assigns it to stringArray[1]. If the user enters "hi", then hi will be in stringArray[1]
Next, i becomes i = 2
On the third pass:
i = 2
2<3
stringArray[i] = input.next(); captures a word and places it in stringArray[2]. If the user types "hi", it goes to stringArray[2]
Then, i increments to i = 3
The loop terminates since i<integers is false.
The next outer loop for (int i = 0; i < frequency.length; i++) and inner loop for (int j = 0; j < frequency.length; will check each word and if (stringArray[i].equals(stringArray[j])) identifies any duplicates. Words that repeat will have their count incremented and the frequency array will record these values. For example, hey appears once while hi shows up twice, thus resulting in the final outputs:
hey 1
hi 2
hi 2
The visual representation of the program and its outcome based on the example provided is attached.