Question: Exercise 2: Use Bag Interface Implementation to design the program. Setting up the Environment: . . Create a new Project on Netbeans. Give the project

 Exercise 2: Use Bag Interface Implementation to design the program. Setting

Exercise 2: Use Bag Interface Implementation to design the program. Setting up the Environment: . . Create a new Project on Netbeans. Give the project name Lab1.java Go to the course Moodle and download the following java files o BagInterface o BagArray Add these files to the project directory. Question 1: Suppose aBag represents an empty bag that has a finite capacity. Write Java statements to add user-supplied strings to the bag until the operation falls. Question 2: Given the full bag aBag that you created in Question 1, write some pseudocode statements that remove and display all the strings in bag. Question 3: Given the full bag aBag that you mated in Question 1, mite some pseudocode statements to find the number of times, if any, that the string "Hello" occurs in aBag. Question 4: Given the bag Bag that you created in Question 1, write some Java statements that display all of the strings in aBag. Do not alter the contents of aBag. Question 5: while (!shoppingCart.isEmpty()) System.out.println(shoppingCart.remove()); In this code, a while loop executes during the checkout process until the bag is empty. What for statement could replace the while statement? Use only the existence of shopping Cart, not the array items. 3 Question 6: Consider the program below. After creating the instance myBank of the class PiggyBank, suppose that we add several unknown coins to myBank. Write some code that will remove coins from the bank until either you remove a penny or the bank becomes empty public class PiggyBankExample { public static void main(String[] args) { PiggyBank myBank - new PiggyBank: addCoin(new Coin(1, 2010). myBank): addCoin(new Coin(5, 2011), myBank): addCoin(new Coin(10, 2000), myBank): addCoin(new Coin(25, 2012), myBank); System.out.println("Removing all the coins:"); int amountRemoved - 0; while (!myBank.isEmpty()) { Coin removedCoin - myBank.remove: System.out.println("Removed a " + removedCoin.getCoinName() + amountRemoved = amount Removed + removedCoin.getValue(): } end while System.out.println("All done. Removed " + amount Removed "cents."); } end main private static void addCoin(Coin acoin, PiggyBank aBank) { if (aBank.add(Coin)) System.out.println("Added a " + aCoin.getCoinName() + "."); else System.out.println("Tried to add a " + acoin.getCoinName() + ", but couldn't"): } end addCoin }//end PiggyBankumple Exercise 2: Use Bag Interface Implementation to design the program. Setting up the Environment: . . Create a new Project on Netbeans. Give the project name Lab1.java Go to the course Moodle and download the following java files o BagInterface o BagArray Add these files to the project directory. Question 1: Suppose aBag represents an empty bag that has a finite capacity. Write Java statements to add user-supplied strings to the bag until the operation falls. Question 2: Given the full bag aBag that you created in Question 1, write some pseudocode statements that remove and display all the strings in bag. Question 3: Given the full bag aBag that you mated in Question 1, mite some pseudocode statements to find the number of times, if any, that the string "Hello" occurs in aBag. Question 4: Given the bag Bag that you created in Question 1, write some Java statements that display all of the strings in aBag. Do not alter the contents of aBag. Question 5: while (!shoppingCart.isEmpty()) System.out.println(shoppingCart.remove()); In this code, a while loop executes during the checkout process until the bag is empty. What for statement could replace the while statement? Use only the existence of shopping Cart, not the array items. 3 Question 6: Consider the program below. After creating the instance myBank of the class PiggyBank, suppose that we add several unknown coins to myBank. Write some code that will remove coins from the bank until either you remove a penny or the bank becomes empty public class PiggyBankExample { public static void main(String[] args) { PiggyBank myBank - new PiggyBank: addCoin(new Coin(1, 2010). myBank): addCoin(new Coin(5, 2011), myBank): addCoin(new Coin(10, 2000), myBank): addCoin(new Coin(25, 2012), myBank); System.out.println("Removing all the coins:"); int amountRemoved - 0; while (!myBank.isEmpty()) { Coin removedCoin - myBank.remove: System.out.println("Removed a " + removedCoin.getCoinName() + amountRemoved = amount Removed + removedCoin.getValue(): } end while System.out.println("All done. Removed " + amount Removed "cents."); } end main private static void addCoin(Coin acoin, PiggyBank aBank) { if (aBank.add(Coin)) System.out.println("Added a " + aCoin.getCoinName() + "."); else System.out.println("Tried to add a " + acoin.getCoinName() + ", but couldn't"): } end addCoin }//end PiggyBankumple

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!