In: Computer Science
Write in Java
* Create a new client class called Plants.java
* Write code in the Plants class that solves problems 1,2,3 and 4
* Include the solutions of the different problems in different methods and call them from the main method
* Use the BagInterface.java and ArrayBag.java, but do not add any code
Problem 1: Create a bag plantsCart, which holds the following spring seedlings(represented by String) Rose, Daisy, Cabbage, Cucumber, Carrot, Cucumber, Daffodil, Daisy, Rose, Iris, Rose, Spinach.
Problem 2: Given the bag plantsCart, write statements that remove and display all plants in the bag
Probelm 3: Write statements that remove and count all occurnace of the string Rose in plantsCart. Do not remoe any other strings from the bag. Report the number of time that Rose occured. Print the new content of plantCarts.
Problem 4: Write a code fragment that moves all flowers from plantCart to a new cart (a new bag of Srtring objects) - flowersCart. At the end, report the numbers of flowers in flowersCart.
HINT: You can create a bag called flowers, which contains the strings representing flowers used in this program: Daisy, Iris, Rose, Daffodil and then use it to check each plant from plantsCart, whether it is a flower or not.
************************************************************************************
**BagInterface.java code**
_____________________________________
public interface BagInterface<T> {
/** 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,
if possible.
@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 the bag contains anEntry, or false otherwise
*/
public boolean contains(T anEntry);
/** Creates an array of all entries that are in this bag.
@return a newly allocated array of all the entries in the bag
*/
public T[] toArray();
/** Overrides the toString method to give a nice display of the
items in
* the bag in this format Bag{Size:# <1>
<2> <3> <4> }
* @return a string representation of the contents of the bag
*/
public String toString();
/** Test whether this bag is equal to a given (as
parameter) bag.
@param aBag the bag to be compare to
@return true if both bags are equal */
public boolean equals(ArrayBag<T> aBag)
} // end BagInterface
*******************************************************************
***ArrayBag.java code *******
______________________________________________
//Start of ArrayBag class
public class ArrayBag<T> implements BagInterface<T>
{
private final T[] bag;
private static final int DEFAULT_CAPACITY = 25;
private int numberOfEntries;
/** Creates an empty bag whose initial capacity is 25. */
public ArrayBag() {
this(DEFAULT_CAPACITY);
} // end default constructor
/** Creates an empty bag having a given initial capacity.
@param capacity the integer capacity desired */
public ArrayBag(int capacity) {
numberOfEntries = 0;
// the cast is safe because the new array contains null
entries
@SuppressWarnings("unchecked")
T[] tempBag = (T[]) new Object[capacity]; // unchecked cast
bag = tempBag;
} // end constructor
public boolean add(T newEntry) {
boolean result = true;
if (isFull()) {
result = false;
} else { // assertion: result is true here
bag[numberOfEntries] = newEntry;
numberOfEntries++;
} // end if
return result;
} // end add
/** Retrieves all entries that are in this bag.
@return a newly allocated array of all the entries in the bag
*/
public T[] toArray() {
// the cast is safe because the new array contains null
entries
@SuppressWarnings("unchecked")
T[] result = (T[]) new Object[numberOfEntries]; // unchecked
cast
for (int index = 0; index < numberOfEntries; index++) {
result[index] = bag[index];
} // end for
return result;
} // end toArray
/** Sees whether this bag is full.
@return true if the bag is full, or false if not */
public boolean isFull() {
return numberOfEntries == bag.length;
} // end isFull
/** Sees whether this bag is empty.
@return true if the 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 the integer number of entries currently in the bag */
public int getCurrentSize() {
return numberOfEntries;
} // end getCurrentSize
public int getFrequencyOf(T anEntry) {
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 true if the bag contains anEntry, or false otherwise
*/
public boolean contains(T anEntry) {
return getIndexOf(anEntry) > -1;
} // end contains
/** Removes all entries from this bag. */
public void clear() {
while (!isEmpty()) {
remove();
}
} // end clear
/** Removes one unspecified entry from this bag, if
possible.
@return either the removed entry, if the removal was
successful,
or null otherwise */
public T remove() {
T result = null;
if (numberOfEntries > 0) {
numberOfEntries--;
result = bag[numberOfEntries];
bag[numberOfEntries] = null;
} // end if
return result;
} // end 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) {
int index = getIndexOf(anEntry);
T result = removeEntry(index);
return anEntry.equals(result);
} // end remove
private T removeEntry(int givenIndex) {
T result = null;
if (!isEmpty() && (givenIndex >= 0)) {
result = bag[givenIndex]; // entry to remove
numberOfEntries--;
bag[givenIndex] = bag[numberOfEntries]; // replace entry with last
entry
bag[numberOfEntries] = null; // remove last entry
} // end if
return result;
} // end removeEntry
private int getIndexOf(T anEntry) {
int where = -1;
boolean found = false;
for (int index = 0; !found && (index < numberOfEntries);
index++) {
if (anEntry.equals(bag[index])) {
found = true;
where = index;
} // end if
} // end for
} // end getIndexOf
public String toString() {
String result = "Bag{Size:" + numberOfEntries + " ";
for (int index = 0; index < numberOfEntries; index++) {
result += "<" + bag[index] + "> ";
} // end for
result += "}";
return result;
}
public boolean equals(ArrayBag<T> aBag)
{
boolean result = false; // result of comparison of bags
int position; // want position available throughout method
if (numberOfEntries == aBag.getCurrentSize()) {
// Provisionally these are the same
result = true;
for (position = 0; (position < numberOfEntries); position++)
{
// Get the frequency of the item in this bag
int countInThisBag = getFrequencyOf(bag[position]);
int countInOtherBag = aBag.getFrequencyOf(bag[position]);
if (countInThisBag != countInOtherBag) {
result = false }
} // end for
}
return result;
} // end equals
} // end ArrayBag
public clss Plants extends ArrayBag {
public static void main(String []args) throws
IOException {
Array list = ["rose", "daisy",
"cabbage", "cucumber", "carrot", "cucumber", "daffodil", "daisy",
"rose", "iris", "rose", "spinach"];
ArrayBag plantsCart =
create(list);
deleteAndDisplay(plantsCart);
ArrayBag plantsCart =
create(list);
removeRose(plantsCart);
ArrayBag plantsCart =
create(list);
Array flower = ["rose", "daisy",
"daffodil", "iris"];
ArrayBag flowers =
create(flower);
createFlowersCart(plantsCart,
flowers);
}
ArrayBag create(Array list) {
ArrayBag plantsCart = new
ArrayBag();
for(String element : list) {
plantsCart.add(element);
}
return plantsCart;
}
void deleteAndDisplay(ArrayBag plantsCart) {
while(!plantsCart.isEmpty)
System.out.println(plantsCart.remove());
}
void removeRose(ArrayBag plantsCart) {
System.out.println("Frequency of
Rose:"+plantsCart.getFrequencyOf("rose")+"\n";
while(plantsCart.remove("rose"));
plantsCart =
plantsCart.toArray();
for(ArrayBag element : plantsCart)
{
System.out.println(element);
}
}
void createFlowersCart(ArrayBag plantsCart, ArrayBag
flowers){
ArrayBag flowersCart = new
ArrayBag();
int c = 0;
plantsCart =
plantsCart.toArray();
for(ArrayBag element : plantsCart)
{
if(flowers.contains(element)) {
c++;
flowersCart.add(element);
ArrayBag.remove(element);
}
}
System.out.println("No. of flowers
in FlowerCart:"+c);
}
}