Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

You are to use the set of codes provided to complete the implementation of the Bags as seen in lecture. Some of the pieces of

You are to use the set of codes provided to complete the implementation of the Bags as seen in lecture. Some of the pieces of information missing should be designed appropriately to obtain the outputs as indicated. The completed source code files should be submitted in the dropbox Lab # 5 part a Output:

Sunflower seeds $12.95

Bird bath $44.99

Squirrel guard $15.47

Bird feeder $20.50

Total cost: $93.91

Items : Item [] items = {new Item ("Bird feeder", 2050),

new Item ("Squirrel guard", 1547),

new Item ("Bird bath", 4499),

new Item ("Sunflower seeds", 1295) };

BagInterface:

/** LISTING 1-1 An interface that describes the operations of a bag of objects. @author Frank M. Carrano @version 3.0 */ public interface BagInterface { /** Gets the current number of entries in this bag. @return the integer number of entries currently in the bag */ public int getCurrentSize(); /** Sees whether this bag is full. @return true if the bag is full, or false if not */ public boolean isFull(); /** Sees whether this bag is empty. @return true if the bag is empty, or false if not */ public boolean isEmpty(); /** Adds a new entry to this bag. @param newEntry the object to be added as a new entry @return true if the addition is successful, or false if not */ public boolean add(T newEntry);

/** Removes one unspecified entry from this bag, if possible. @return either the removed entry, if the removal was successful, or null */ public T remove(); /** Removes one occurrence of a given entry from this bag. @param anEntry the entry to be removed @return true if the removal was successful, or false if not */ public boolean remove(T anEntry); /** Removes all entries from this bag. */ public void clear(); /** Counts the number of times a given entry appears in this bag. @param anEntry the entry to be counted @return the number of times anEntry appears in the bag */ public int getFrequencyOf(T anEntry); /** Tests whether this bag contains a given entry. @param anEntry the entry to locate @return true if this bag contains anEntry, or false otherwise */ public boolean contains(T anEntry); /** Retrieves all entries that are in this bag. @return a newly allocated array of all the entries in the bag */ public T[] toArray();

/** Creates a new bag that combines the contents of this bag and anotherBag. @param anotherBag the bag that is to be added @return a combined bag */ // public BagInterface union(BagInterface anotherBag);

/** Creates a new bag that contains those objects that occur in both this bag and anotherBag. @param anotherBag the bag that is to be compared @return a combined bag */ // public BagInterface intersection(BagInterface anotherBag);

/** Creates a new bag of objects that would be left in this bag after removing those that also occur in anotherBag. @param anotherBag the bag that is to be removed @return a combined bag */ // public BagInterface difference(BagInterface anotherBag); } // end BagInterface

Bag:

/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package shopping;

/** * * @author apple */ public class Bag implements BagInterface { private final item[] bag; private static final int DEFAULT_CAPACITY=25; private int numberOfEntries; public Bag() { this(DEFAULT_CAPACITY); } public Bag(int capacity){ numberOfEntries=0; @SuppressWarnings("unchecked") item[] tempBag=(item[])new Object[capacity]; bag = tempBag; } public int CurrentSize(){ return numberOfEntries; } public boolean isFull(){ return numberOfEntries==DEFAULT_CAPACITY; } public boolean isEmpty(){ return numberOfEntries==0; } public boolean add(item newEntry){ boolean result=true; if(isFull()){ result=false; } else{ bag[numberOfEntries]=newEntry; numberOfEntries++; } return result; } public item remove(){ item result = null; if(numberOfEntries>0){ result=bag[numberOfEntries-1]; bag[numberOfEntries-1]=null; numberOfEntries--; } return result; } public boolean remove(item anEntry){ boolean found=false; String a= anEntry.toString(); String b=""; for(int index=0; !found&&(index

Shopping:

/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package shopping;

/** * * @author apple */ public class Shopping {

/** * @param args the command line arguments */ public static void main(String[] args) { item[] items={new item("Bird feeder", 2050), new item("Squirrel guard", 1547), new item("Bird bath", 4499), new item("Sunflower seeds", 1295) }; BagInterface < item > shoppingCart =new Bag (); int totalCost = 0; // statements that add selected items to the shopping cart: for (int index = 0 ; index < items.length ; index++) { item nextItem = items [index]; // simulate getting item from // shopper shoppingCart.add (nextItem); totalCost = totalCost + nextItem.getPrice (); } // end for // simulate checkout while (!shoppingCart.isEmpty ()) System.out.println (shoppingCart.remove ().toString()); System.out.println ("Total cost: " + "\t$" + totalCost / 100 + "." + totalCost % 100); } // end main }

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

Object Databases The Essentials

Authors: Mary E. S. Loomis

1st Edition

020156341X, 978-0201563412

More Books

Students also viewed these Databases questions

Question

Define assets.

Answered: 1 week ago

Question

3. How has Starbucks changed since its early days?

Answered: 1 week ago

Question

What is cost plus pricing ?

Answered: 1 week ago

Question

1. What are the types of wastes that reach water bodies ?

Answered: 1 week ago

Question

Which type of soil has more ability to absorb water?

Answered: 1 week ago

Question

Why is the System Build Process an iterative process?

Answered: 1 week ago