Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

zyDE 4 . 3 . 1 : Class generics. The following program uses a generic class ItemCount to count the number of times the same

zyDE 4.3.1: Class generics.
The following program uses a generic class ItemCount to count the number of times the same word is read from the user input. Modify the program to:
Build #1- Complete the incrementIfDuplicate() method and update the main() method within the DuplicateCounter class to use the incrementIfDuplicate() method.
Build #2- Modify the program to count the number of times a specific integer value is read from the user input. Be sure to use the Integer class.
ItemCount.Java
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;
}
// Increment item count
public void incrementCount(){
++itemCount;
}
// Increments the item count if compareVal argument
// is equal to item value.
public void incrementIfDuplicate(Type compareVal){
// FIXME: Complete method
}
// Returns string for item value and count using
// the format itemVal: itemCount
@Override
public String toString(){
return ""+ itemVal +": "+ itemCount;
}
}
DuplicateCounter.java
import java.util.Scanner;
public class DuplicateCounter {
public static void main(String[] args){
Scanner scnr = new Scanner(System.in);
ItemCount integerCounter = new ItemCount();
int inputInteger;
integerCounter.setItem(5);
System.out.println("Enter values (9999 at end):");
// Read first word
inputInteger = scnr.nextInt();
// Keep reading until word read equals
while(inputInteger !=9999){
integerCounter.incrementIfDuplicate(inputInteger);
inputInteger = scnr.nextInt();
}
// Display final word count
System.out.println("The integer \""+ integerCounter.getItem()+
"\" was read "+ integerCounter.getCount()+
" times.");
}
}
------------------------------------------------------------------------------------------
Please provide two sets of code (one for each build)

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

Climate And Environmental Database Systems

Authors: Michael Lautenschlager ,Manfred Reinke

1st Edition

ISBN: 1461368332, 978-1461368335

More Books

Students also viewed these Databases questions

Question

What is the best conclusion for Xbar Chart? UCL A X B C B A LCL

Answered: 1 week ago

Question

f. Did they change their names? For what reasons?

Answered: 1 week ago