Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import java.util.ArrayList; /* * Given an array list of words (strings), count the number of words that occur only once * For example, in the

import java.util.ArrayList;

/*
* Given an array list of words (strings), count the number of words that occur only once
* For example, in the list: "we" "have" "a" "meeting" "at" "four" "and" "we" "have" "another" "meeting" "at" "five"
* the words occurring once are: "a" "four" "and" "another" "five" for a total of 5
*/
public class QA1
{
/**
counts the number of words (strings) in a list that occur only once
@param list ArrayList of strings to be examined
@return number of strings occurring once in the list
 */
public static int countSingleOccurrence(ArrayList list)
{
//-----------Start below here. To do: approximate lines of code = 9
// Hint: create boolean flag variable singleOccurrence and set it to true;
// Use a nested loop, loop through each word in the list in the outer, check this word against
// all words in the inner loop. If you find a duplicate word in the inner loop, set singleOccurrence to false






//-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.
}

public static void main(String[] args)
{
ArrayList  words = new ArrayList<>();
words.add("blow");
words.add("wind");
words.add("blow");
words.add("and");
words.add("go");
words.add("mill");
words.add("go");

int unique = countSingleOccurrence(words);
System.out.println(words + " has " + unique + " words that occur only once");
System.out.println("Expected:[blow, wind, blow, and, go, mill, go] has 3 words that occur only once");

words.clear();
words.add("we");
words.add("have");
words.add("a");
words.add("meeting");
words.add("at");
words.add("four");
words.add("and");
words.add("we");
words.add("have");
words.add("another");
words.add("meeting");
words.add("at");
words.add("five");
unique = countSingleOccurrence(words);
System.out.println(words + " has " + unique + " words that occur only once");
System.out.println("Expected:[we, have, a, meeting, at, four, and, we, have, another, meeting, at, five] has 5 words that occur only once");
}
}



Include a output and other necessary screenshots if you can to avoid confusions and mistakes 

Step by Step Solution

3.36 Rating (159 Votes )

There are 3 Steps involved in it

Step: 1

import javautilArrayList Given an array list of words strings count the number of words that occur o... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Statistics For Management And Economics Abbreviated

Authors: Gerald Keller

10th Edition

978-1-305-0821, 1285869648, 1-305-08219-2, 978-1285869643

More Books

Students also viewed these Programming questions