Question: Variable currentWeights is declared as a HashSet. Then, integer numWeights is read from input representing the number of integers to be read next. For each
Variable currentWeights is declared as a HashSet. Then, integer numWeights is read from input representing the number of integers to be read next. For each integer read into variable weight:
Add weight to currentWeights.
If weight is successfully added, output "Weight registered: followed by weight.
Otherwise, output "Duplicate entered: followed by weight, and assign duplicateEntered with true.
End each output with a newline.
Ex: If the input is:
then the output is:
Weight registered: Weight registered: Duplicate entered:
import java.util.HashSet;
import java.util.Scanner;
public class Weights
public static void mainString args
Scanner scnr new ScannerSystemin;
HashSet currentWeights new HashSet;
int numWeights;
int weight;
boolean duplicateEntered;
numWeights scnrnextInt;
duplicateEntered false;
while numWeights
if duplicateEntered
weight scnrnextInt;
Your code goes here
numWeights;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
