Overview You need to make changes to OnLineShopper.java by adding 6 more items to the shopping cart program. You will al

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

Overview You need to make changes to OnLineShopper.java by adding 6 more items to the shopping cart program. You will al

Post by answerhappygod »

Overview You Need To Make Changes To Onlineshopper Java By Adding 6 More Items To The Shopping Cart Program You Will Al 1
Overview You Need To Make Changes To Onlineshopper Java By Adding 6 More Items To The Shopping Cart Program You Will Al 1 (51.5 KiB) Viewed 39 times
Overview You Need To Make Changes To Onlineshopper Java By Adding 6 More Items To The Shopping Cart Program You Will Al 2
Overview You Need To Make Changes To Onlineshopper Java By Adding 6 More Items To The Shopping Cart Program You Will Al 2 (87.02 KiB) Viewed 39 times
public final class ArrayBag<T> implementsBagInterface<T>
{
private final T[] bag;
private int numberOfEntries;
private boolean integrityOK = false;
private static final intDEFAULT_CAPACITY = 25;
private static final int MAX_CAPACITY= 10000;
/** Creates an empty bag whose initial capacity is 25. */
public ArrayBag()
{
this(DEFAULT_CAPACITY);
} // end default constructor
/** Creates an empty bag having a given capacity.
@param desiredCapacity Theinteger capacity desired. */
public ArrayBag(int desiredCapacity)
{
if (desiredCapacity <=MAX_CAPACITY)
{
// The cast is safe becausethe new array contains null entries
@SuppressWarnings("unchecked")
T[] tempBag = (T[])newObject[desiredCapacity]; // Unchecked cast
bag = tempBag;
numberOfEntries = 0;
integrityOK = true;
// Test that contents arenulls - OK
// for(int index = 0; index < desiredCapacity; index++)
// System.out.print(bag[index] + " ");
// System.out.println();
}
else
throw newIllegalStateException("Attempt to create a bag " +
"whose capacity exceeds " +
"allowed maximum.");
} // end constructor
/** Adds a new entry to this bag.
@paramnewEntry The object to be added as a new entry.
@return Trueif the addition is successful, or false if not. */
public boolean add(T newEntry)
{
checkIntegrity();
boolean result = true;
if (isArrayFull())
{
result = false;
}
else
{ // Assertion: result is truehere
bag[numberOfEntries] =newEntry;
numberOfEntries++;
} // end if
return result;
} // end add
/** Retrieves all entries that are in this bag.
@return Anewly allocated array of all the entries in this bag. */
// public <T> T[] toArray() //OK
public T[] toArray() //OK
{
checkIntegrity();
// The cast is safe because the new arraycontains null entries.
@SuppressWarnings("unchecked")
T[] result = (T[])newObject[numberOfEntries]; // Unchecked cast
for (int index = 0; index <numberOfEntries; index++)
{
result[index] =bag[index];
} // end for
return result;
// Note: The body of this method couldconsist of one return statement,
// if you call Arrays.copyOf
} // end toArray
/** Sees whether this bag is empty.
@return Trueif this bag is empty, or false if not. */
public boolean isEmpty()
{
return numberOfEntries == 0;
} // end isEmpty
/** Gets the current number of entries in this bag.
@return Theinteger number of entries currently in this bag. */
public int getCurrentSize()
{
return numberOfEntries;
} // end getCurrentSize
/** Counts the number of times a given entry appears in thisbag.
@param anEntry The entry to be counted.
@return Thenumber of times anEntry appears in this ba. */
public int getFrequencyOf(T anEntry)
{
checkIntegrity();
int counter = 0;
for (int index = 0; index <numberOfEntries; index++)
{
if(anEntry.equals(bag[index]))
{
counter++;
} // end if
} // end for
return counter;
} // end getFrequencyOf
/** Tests whether this bag contains a given entry.
@param anEntry The entry to locate.
@return Trueif this bag contains anEntry, or false otherwise. */
public boolean contains(T anEntry)
{
checkIntegrity();
return getIndexOf(anEntry) > -1; // or>= 0
} // end contains
/** Removes all entries from this bag. */
public void clear()
{
while (!isEmpty())
remove();
} // end clear
/** Removes one unspecified entry from this bag, ifpossible.
@return Eitherthe removed entry, if the removal
wassuccessful, or null. */
public T remove()
{
checkIntegrity();
T result = removeEntry(numberOfEntries -1);
return result;
} // end remove
/** Removes one occurrence of a given entry from this bag.
@param anEntry The entry tobe removed.
@return True if the removalwas successful, or false if not. */
public boolean remove(T anEntry)
{
checkIntegrity();
int index = getIndexOf(anEntry);
T result = removeEntry(index);
return anEntry.equals(result);
} // end remove
// Returns true if the array bag is full, or falseif not.
private boolean isArrayFull()
{
return numberOfEntries >= bag.length;
} // end isArrayFull
// Locates a given entry within the array bag.
// Returns the index of the entry, if located,
// or -1 otherwise.
// Precondition: checkInitialization has beencalled.
private int getIndexOf(T anEntry)
{
int where = -1;
boolean found = false;
int index = 0;
while (!found && (index <numberOfEntries))
{
if (anEntry.equals(bag[index]))
{
found = true;
where = index;
} // end if
index++;
} // end while
// Assertion: If where > -1, anEntry isin the array bag, and it
// equals bag[where]; otherwise, anEntry isnot in the array.
return where;
} // end getIndexOf
// Removes and returns the entry at a given indexwithin the array.
// If no such entry exists, returns null.
// Precondition: 0 <= givenIndex <numberOfEntries.
// Precondition: checkInitialization has beencalled.
private T removeEntry(int givenIndex)
{
T result = null;
if (!isEmpty() && (givenIndex >= 0))
{
result =bag[givenIndex]; // Entry toremove
int lastIndex =numberOfEntries - 1;
bag[givenIndex] =bag[lastIndex]; // Replace entry to remove with lastentry
bag[lastIndex] = null; // Remove reference to lastentry
numberOfEntries--;
} // end if
return result;
} // end removeEntry
// Throws an exception if this object is notinitialized.
private void checkIntegrity()
{
if (!integrityOK)
throw newSecurityException("ArrayBag object is corrupt.");
} // end checkIntegrity
} // end ArrayBag
BagInterface.java code
/** An interface that describes the operations of a bag ofobjects. */
public interface BagInterface<T>
{
/** Gets the current number of entries in this bag.
@return The integer number of entriescurrently in the bag. */
public int getCurrentSize();
/** Sees whether this bag is empty.
@return True if the bag is empty, orfalse if not. */
public boolean isEmpty();
/** Adds a new entry to this bag.
@param newEntry The objectto be added as a new entry.
@return True if theaddition is successful, or false if not. */
public boolean add(T newEntry);
/** Removes one unspecified entry from this bag, ifpossible.
@return Eitherthe removed entry, if the removal.
wassuccessful, or null. */
public T remove();
/** Removes one occurrence of a given entry from this bag, ifpossible.
@param anEntry The entry to be removed.
@return Trueif 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 thisbag.
@param anEntry The entry to becounted.
@return The number of times anEntryappears in the bag. */
public int getFrequencyOf(T anEntry);
/** Tests whether this bag contains a given entry.
@param anEntry The entry to find.
@return True if the bag contains anEntry,or false if not. */
public boolean contains(T anEntry);
/** Retrieves all entries that are in this bag.
@return A newly allocated array of allthe entries in the bag.
Note: Ifthe bag is empty, the returned array is empty. */
public T[] toArray();
} // end BagInterface
Overview You Need To Make Changes To Onlineshopper Java By Adding 6 More Items To The Shopping Cart Program You Will Al 3
Overview You Need To Make Changes To Onlineshopper Java By Adding 6 More Items To The Shopping Cart Program You Will Al 3 (117.79 KiB) Viewed 39 times
Overview You Need To Make Changes To Onlineshopper Java By Adding 6 More Items To The Shopping Cart Program You Will Al 4
Overview You Need To Make Changes To Onlineshopper Java By Adding 6 More Items To The Shopping Cart Program You Will Al 4 (161.2 KiB) Viewed 39 times
Overview You need to make changes to OnLineShopper.java by adding 6 more items to the shopping cart program. You will also create a student class to be used for your StudentRecord system. For this project you can work with classmates, but you may not exchange code. Make sure you add the name of classmates you work with to the top of the java files file (in comments).
The project's code distribution is available at ADT.zip. Download the code distribution and import it as you have imported our class examples. The code distribution provides you with the following: ● ● programs - A package (folder) where you will find the following programs to be used for the project. ArrayBag.java, BagInterface.java, Item.java, OnlineShopper.java. Specifications Part 1 You will edit the OnlineShopper as follows: a) Run the program and do a screen capture of the output b) Add 6 new items to the cart c) Repeat (a) above.
ALMACENESSAAN~~~~~ 1 20/** A class of items for sale. @author Frank M. Carrano @author Timothy M. Henry. @version 5.0 3 4 5 6 */ 7 public class Item 8 { 9 private String description; private int price; 10 11 120 13 14 15 16 17 180 19 20 21 22 230 24 25 26 public Item(String product Description, int productPrice) { description = product Description; price = productPrice; } // end constructor public String getDescription() { return description; } // end getDescription public int getPrice() { return price; } // end getPrice 27 A280 public String toString() { 29 30 return description + "\t$" + price / 100 + } // end toString 32 } // end Item 31 33 + price % 100;
LEMAFOTOSENBERG 1 2 /** A class that maintains a shopping cart for an online store. */ 3 public class OnlineShopper 4 { 50 6 8 9 10 11 12 13 14 15 16 17 18 28 19 20 21 22 23 24 wwN8NBGD 25 26 829 27 public static void main(String[] args) { 28 Item [] items = {new Item("Bird feeder", 2050), new Item("Squirrel guard", 1547), new Item("Bird bath", 4499), new Item("Sunflower seeds", 1295)}; BagInterface shopping Cart = new ArrayBag<>(); 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 (!shopping Cart.isEmpty()) System.out.println(shopping Cart.remove()); System.out.println("Total cost: " + "\t$" + totalCost / 100 + totalCost % 100); } // end main 29 } // end OnlineShopper 30 31
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply