Java Question... The following program using a generic class ItemCount to count
ID: 3604718 • Letter: J
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. 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<String> wordCounter = new ItemCount<String>();
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 <end>
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 <Type extends Comparable<Type>> {
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;
}
}
Explanation / Answer
DuplicateCounter.java
import java.util.Scanner;
public class DuplicateCounter {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
ItemCount<String> wordCounter = new ItemCount<String>();
String inputWord = "";
Integer count = 0;
wordCounter.setItem("that");
System.out.println("Enter words (END at end):");
// Read first word
inputWord = scnr.next();
// Keep reading until word read equals <end>
while( !inputWord.equals("END") ) {
count++;
wordCounter.incrementIfDuplicate(inputWord);
// Read next word
inputWord = scnr.next();
}
System.out.println("Number of words read: "+count);
// Display final word count
System.out.println("The word "" + wordCounter.getItem() +
"" was read " + wordCounter.getCount() +
" times.");
return;
}
}
ItemCount.java
public class ItemCount <Type extends Comparable<Type>> {
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) {
if(itemVal.equals(compareVal))
++itemCount;
}
// Returns string for item value and count using
// the format itemVal: itemCount
@Override
public String toString() {
return "" + itemVal + ": " + itemCount;
}
}
Output:
Enter words (END at end):
aaaaa
bbbbb
ccccc
ddddd
that
wwww
that
that
END
Number of words read: 8
The word "that" was read 3 times.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.