Java Programing: The contents of the Pouch MUST be stored in a generic array. An ArrayList must not be used. Create a ge

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

Java Programing: The contents of the Pouch MUST be stored in a generic array. An ArrayList must not be used. Create a ge

Post by answerhappygod »

Java Programing:
The contents of the Pouch MUST be stored in a generic
array. An ArrayList must not be used.
Create a generic class, called Pouch, with a type
parameter that simulates drawing an item at random
out of a Pouch. For example the Pouch might contain Strings
representing names written on a slip of paper, or the Pouch might
contain integers representing a random drawing for a lottery.
Include the following methods in your generic class, along with any
other methods you’d like:
Your Pouch class with need an array of size 10 to hold the
objects in the Pouch, and a count variable to maintain a count of
how many objects are actually in the Pouch.
In the driver file that tests your class create 3 Pouches, one
to test all the methods in the Pouch class, one with the names of 7
of your friends, the third with numbers between 3 and 7 inclusive
representing the number of hours you will spend studying for an
upcoming test with 2 of your friends.
For the test Pouch,
For the remaining 2 Pouches, use the add( ) method to populate
the 2 Pouches, and the drawItem( ) method for each Pouch to
determine i) which 2 friends you will study
with and ii) how many hours of studying you
and your friends will do.
Refer to the code for the ArrayStack class from Chapter
12. Use that code as a starting point to create the Pouch class,
modifying it to remove the methods in the ArrayStack class (push,
pop, etc) then add the methods described below. Also change the
variable names to reflect the new class. For example the array name
should NOT be stack, instead it should be contents (as in the
contents of the Pouch), etc..
The contents of the Pouch MUST be stored in a generic
array. An ArrayList must not be used.
package jsjf;
import jsjf.exceptions.*;
import java.util.Arrays;
/**
* An array implementation of a stack in which the bottom of
the
* stack is fixed at index 0.
*
* @author Java Foundations
* @version 4.0
*/
public class ArrayStack<T> implements StackADT<T>
{
private final static int DEFAULT_CAPACITY = 100;
private int top;
private T[] stack;
/**
* Creates an empty stack using the default capacity.
*/
public ArrayStack()
{
this(DEFAULT_CAPACITY);
}
/**
* Creates an empty stack using the specified capacity.
* @param initialCapacity the initial size of the array
*/
public ArrayStack(int initialCapacity)
{
top = 0;
stack = (T[])(new Object[initialCapacity]);
}
/**
* Adds the specified element to the top of this stack,
expanding
* the capacity of the array if necessary.
* @param element generic element to be pushed onto stack
*/
public void push(T element)
{
if (size() == stack.length)
expandCapacity();
stack[top] = element;
top++;
}
/**
* Creates a new array to store the contents of this stack
with
* twice the capacity of the old one.
*/
private void expandCapacity()
{
stack = Arrays.copyOf(stack, stack.length * 2);
}
/**
* Removes the element at the top of this stack and returns a
* reference to it.
* @return element removed from top of stack
* @throws EmptyCollectionException if stack is empty
*/
public T pop() throws EmptyCollectionException
{
if (isEmpty())
throw new EmptyCollectionException("stack");
top--;
T result = stack[top];
stack[top] = null;
return result;
}
/**
* Returns a reference to the element at the top of this
stack.
* The element is not removed from the stack.
* @return element on top of stack
* @throws EmptyCollectionException if stack is empty
*/
public T peek() throws EmptyCollectionException
{
if (isEmpty())
throw new EmptyCollectionException("stack");
return stack[top-1];
}
package jsjf.exceptions;
/**
* Represents the situation in which a collection is empty.
*
* @author Java Foundations
* @version 4.0
*/
public class EmptyCollectionException extends
RuntimeException
{
/**
* Sets up this exception with an appropriate message.
* @param collection the name of the collection
*/
public EmptyCollectionException(String collection)
{
super("The " + collection + " is empty.");
}
}
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply