Question: Java Question... The following program using a generic class ItemCount to count the number of times the same word is read from the user input.

Java Question...

The following program using a generic class ItemCount to count the number of times the same word is read from the user input. Modify the program to:

Complete the incrementIfDuplicate() method and update the main() method within the DuplicateCounter class to use the incrementIfDuplicate() method.

Modify the program to count the number of times to a specific integer value is read from the user input. Be sure to use the Integer class.

import java.util.Scanner;

public class DuplicateCounter { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); ItemCount wordCounter = new ItemCount(); String inputWord = ""; wordCounter.setItem("that");

System.out.println("Enter words (END at end):");

// Read first word inputWord = scnr.next(); // Keep reading until word read equals while( !inputWord.equals("END") ) { if (wordCounter.getItem().compareTo(inputWord) == 0) { wordCounter.incrementCount(); }

// Read next word inputWord = scnr.next(); } // Display final word count System.out.println("The word \"" + wordCounter.getItem() + "\" was read " + wordCounter.getCount() + " times."); return; } }

public class ItemCount > { private Type itemVal; // Value for item private int itemCount; // Count for item

// Set item value, and reset item count to 0 public void setItem(Type newItemVal) { itemVal = newItemVal; itemCount = 0; } // Get item value public Type getItem() { return itemVal; }

// Get item count public int getCount() { return itemCount; }

// Reset item count to 0 public void resetCount() { itemCount = 0; }

// Reset item count to 0 public void incrementCount() { itemCount = 0; }

// Increments the item count if compareVal argument // is equal to item value. public void incrementIfDuplicate(Type compareVal) { ++itemCount; } // Returns string for item value and count using // the format itemVal: itemCount @Override public String toString() { return "" + itemVal + ": " + itemCount; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!