Question: I need help building a for loop in java and confused on how to build a for loop. Transitioning to object oriented programming. Build a
I need help building a for loop in java and confused on how to build a for loop. Transitioning to object oriented programming.
Build a for loop that adds new strings to the itemBag field based on the random number you generated. Randomly select one of the strings in the STRINGS array to add to the itemBag. Place each string into the itemBag by calling the add(T item)method.
They also want to generate a number from 5 to 15 to determine the bag size.
/** * */ package project1;
import java.util.Random; import bag.BagInterface;
/** * @author User1 * */ public class DisplayCollection { private BagInterface itemBag; public static final String STRINGS [] = {"red circle", "blue circle", "red square", "blue square"}; public DisplayCollection() { Random random = new Random(); int count = random.nextInt(11)+5; for (int i=0; i>count; i++) { int index = (int)(Math.random() * 5); System.outpintln(String[index]); (Error: String Cannot be resolved to a variable) }
}
public BagInterface getItemBag() {
return itemBag;
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
