Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA PROGRAM ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ Need countOccurrencesWithIterator completed --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- import java.util.Arrays; import java.util.Iterator; import java.util.NoSuchElementException; import java.util.ArrayList; public class StringArrayBag implements Iterable , Iterator { private final

JAVA PROGRAM

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Need countOccurrencesWithIterator completed

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

import java.util.Arrays; import java.util.Iterator; import java.util.NoSuchElementException; import java.util.ArrayList; public class StringArrayBag implements Iterable, Iterator { private final static int INITIAL_CAPACITY = 10; private String[] data; private int manyItems; /** * Initializes a bag of INITIAL_CAPACITY */ public StringArrayBag() { this(INITIAL_CAPACITY); } /** * Initializes a bag of the given initial capacity * @param initialCapacity, initial capacity of the bag */ public StringArrayBag(int initialCapacity) { if (initialCapacity < 0 ) throw new IllegalArgumentException("initialCapacity is negative: " + initialCapacity); this.manyItems = 0; this.data = new String[initialCapacity]; } /** * Add the element to end of the bag * @param element, element to add * @throws IllegalArgumentException, if element is null */ public void add(String element) { if (element == null) throw new IllegalArgumentException("element cannot be null"); ensureCapacity(this.manyItems+1); this.data[manyItems++] = element; } /** * Add many elements to the end of the bag in the order they are passed * @param elements, elements to add to the end of the bag * @throws IllegalArgumentException, if any element of the elements is null */ public void addMany(String... elements) { for (String element : elements) { if (element == null) throw new IllegalArgumentException("there cannot be a null element"); } ensureCapacity(this.manyItems + elements.length); System.arraycopy(elements, 0, this.data, this.manyItems, elements.length); this.manyItems += elements.length; } /** * Remove an element from the bag * @param target, element to be removed * @throws IllegalArgumentException, if the target is null */ public void remove(String target) { if (target == null) throw new IllegalArgumentException("target cannot be null"); for (int index = 0; index < this.manyItems; index++) if (target.equals(this.data[index])) { data[index] = data[manyItems-1]; manyItems--; return; } } /** * Counts the occurrences of the target in the bag * @param target, the element to count the occurrences of * @return number of times the target appears in the bag * @throws IllegalArgumentException, if the target is null */ public int countOccurrences(String target) { if (target == null) throw new IllegalArgumentException("target cannot be null"); int answer = 0; // TODO for a given target, loop through your elements // and count the times it shows. DO NOT use an iterator // as a cursor-style iterator will reset other iterators ArrayList arraylist = new ArrayList(Arrays.asList(this.data)); Iterator itr = arraylist.iterator(); while (itr.hasNext()) { String str=(String)itr.next(); if (str!=null && str.equals(target)) { answer++; } } return answer; } /** * Counts the occurrences of the target in the bag * This version uses the iterator to count the occurrences * @param target, the element to count the occurrences of * @return number of times the target appears in the bag * @throws IllegalArgumentException, if the target is null */ public int countOccurrencesWithIterator(String target) { if (target == null) throw new IllegalArgumentException("target cannot be null"); int answer = 0; // TODO for a given target, loop through your elements // and count the times it shows. DO use an iterator // even though a cursor-style iterator will reset other iterators return answer; } /** * Ensures the bag can hold the minimum capacity given, and * resizes if needed to the minimum capacity. * @param minimumCapacity, minimum capacity of the data-structure */ public void ensureCapacity(int minimumCapacity) { if (this.data.length < minimumCapacity) { String[] biggerArray = new String[minimumCapacity * 2 + 1]; System.arraycopy(this.data, 0 , biggerArray, 0, this.manyItems); this.data = biggerArray; } } /** * Returns the size of the bag, or number of elements in the bag * @return size of the bag */ public int size() { return manyItems; } /** * Prints the contents of the bag, along with its occurrences to console * in the following format: * element: occurrences */ public void printOccurrences() { ArrayList printed = new ArrayList(); for (String element : this) { if (!printed.contains(element)) { System.out.printf("%s: %d ", element, this.countOccurrences(element)); printed.add(element); } } } /** * Prints the contents of the bag, along with its occurrences to console * this method uses the iterator * in the following format: * element: occurrences */ public void printOccurrencesWithIterator() { ArrayList printed = new ArrayList(); for (String element : this) { if (!printed.contains(element)) { System.out.printf("%s: %d ", element, this.countOccurrencesWithIterator(element)); printed.add(element); } } } /************************************************ * Iterator Methods ************************************************/ // Tracks the index of the current element of the iterator private int currentIndex = -1; /** * Prepares the internal iterator for iteration */ @Override public Iterator iterator() { this.currentIndex = -1; return this; } /** * Checks to see if there is a next element */ @Override public boolean hasNext() { return this.currentIndex + 1 < this.manyItems; } /** * Fetches and returns the next element */ @Override public String next() { if (!hasNext()) throw new NoSuchElementException("There is no next element"); String nextElement = this.data[++this.currentIndex]; // TODO Using the fields above, move to the next element and return it return nextElement; } /** * Removes the current element if there is one * * We will leave this method to the homework */ @Override public void remove() { throw new UnsupportedOperationException("cannot remove element from bag"); } } 

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Test for countOccurrencesWithIterator

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

public void testCountOccurrencesWithIterator() { assertEquals(0,bag.countOccurrencesWithIterator("Ten")); assertEquals(0,bag.countOccurrencesWithIterator("Negative Two")); assertEquals(1,bag.countOccurrencesWithIterator("Five")); assertEquals(1,bag.countOccurrencesWithIterator("Negative One")); assertEquals(2,bag.countOccurrencesWithIterator("Three")); assertEquals(3,bag.countOccurrencesWithIterator("Negative Three")); assertEquals(4,bag.countOccurrencesWithIterator("Zero")); bag = new StringArrayBag(); assertEquals(0,bag.countOccurrencesWithIterator("Ten")); bag=new StringArrayBag(); bag.addMany(new String("One"), new String("Three"), new String("Negative Three"), new String("Five"), new String("Three"), new String("Negative Three"), new String("Negative One"), new String("Zero"), new String("Zero"), new String("Zero"), new String("Negative Three"), new String("Zero")); assertEquals(2,bag.countOccurrencesWithIterator("Three")); assertEquals(3,bag.countOccurrencesWithIterator("Negative Three")); assertEquals(4,bag.countOccurrencesWithIterator("Zero")); bag.remove("Zero"); assertEquals(3,bag.countOccurrencesWithIterator("Zero")); }

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Complete class and explain what is wrong with printOccurrencesWithIteratorcall.

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; /** * The ArrayBagDriver will read a document * and for each word that exists in the document * will count the occurrences of each word * * Note: assume the document is on a single line * * */ public class ArrayBagDriver { /** * @param args, main arguments */ @SuppressWarnings("resource") public static void main(String[] args) { String filename = (args.length == 0) ? "document.txt" : args[0]; // TODO Create an ArrayBag of Strings try {// String[] words; BufferedReader br = new BufferedReader(new FileReader(filename)); words = br.readLine().split(" "); // TODO Add the words to your bag of Strings // Check the StringArrayBag class for useful methods } catch (IOException e) { e.printStackTrace(); System.exit(-1); } System.out.println(" Contents of Bag: "); // TODO call the printOccurrences method of your bag System.out.println(" Contents of Bag using Iterator: "); // TODO call the printOccurrencesWithIterator method of your bag } } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

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

OCA Oracle Database SQL Exam Guide Exam 1Z0-071

Authors: Steve O'Hearn

1st Edition

1259585492, 978-1259585494

Students also viewed these Databases questions

Question

JAVA PROGRAM...

Answered: 1 week ago

Question

What is Ramayana, who is its creator, why was Ramayana written?

Answered: 1 week ago

Question

To solve by the graphical methods 2x +3y = 9 9x - 8y = 10

Answered: 1 week ago

Question

Why is the System Build Process an iterative process?

Answered: 1 week ago