test&lab
This commit is contained in:
BIN
DataLabs/lab4/BagInterface.class
Normal file
BIN
DataLabs/lab4/BagInterface.class
Normal file
Binary file not shown.
48
DataLabs/lab4/BagInterface.java
Normal file
48
DataLabs/lab4/BagInterface.java
Normal file
@ -0,0 +1,48 @@
|
||||
/**
|
||||
An interface that describes the operations of a bag of objects.
|
||||
@author Frank M. Carrano
|
||||
@version 4.0
|
||||
*/
|
||||
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 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 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 all the entries in the bag.
|
||||
Note: If the bag is empty, the returned array is empty. */
|
||||
public T[] toArray();
|
||||
} // end BagInterface
|
BIN
DataLabs/lab4/LinkedBag$Node.class
Normal file
BIN
DataLabs/lab4/LinkedBag$Node.class
Normal file
Binary file not shown.
BIN
DataLabs/lab4/LinkedBag.class
Normal file
BIN
DataLabs/lab4/LinkedBag.class
Normal file
Binary file not shown.
132
DataLabs/lab4/LinkedBag.java
Normal file
132
DataLabs/lab4/LinkedBag.java
Normal file
@ -0,0 +1,132 @@
|
||||
/**
|
||||
A class of bags whose entries are stored in a chain of linked nodes.
|
||||
The bag is never full.
|
||||
@author Frank M. Carrano
|
||||
@version 4.0
|
||||
*/
|
||||
public final class LinkedBag<T> implements BagInterface<T>
|
||||
{
|
||||
private Node firstNode; // Reference to first node
|
||||
private int numberOfEntries;
|
||||
|
||||
public LinkedBag()
|
||||
{
|
||||
firstNode = null;
|
||||
numberOfEntries = 0;
|
||||
} // end default constructor
|
||||
|
||||
/** Adds a new entry to this bag.
|
||||
@param newEntry The object to be added as a new entry.
|
||||
@return True. */
|
||||
public boolean add(T newEntry) // OutOfMemoryError possible
|
||||
{
|
||||
// Add to beginning of chain:
|
||||
Node newNode = new Node(newEntry);
|
||||
newNode.next = firstNode; // Make new node reference rest of chain
|
||||
// (firstNode is null if chain is empty)
|
||||
firstNode = newNode; // New node is at beginning of chain
|
||||
numberOfEntries++;
|
||||
|
||||
return true;
|
||||
} // end add
|
||||
|
||||
/** Retrieves all entries that are in this bag.
|
||||
@return A newly allocated array of all the entries in this 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
|
||||
|
||||
int index = 0;
|
||||
Node currentNode = firstNode;
|
||||
while ((index < numberOfEntries) && (currentNode != null))
|
||||
{
|
||||
result[index] = currentNode.data;
|
||||
index++;
|
||||
currentNode = currentNode.next;
|
||||
} // end while
|
||||
|
||||
return result;
|
||||
// Note: The body of this method could consist of one return statement,
|
||||
// if you call Arrays.copyOf
|
||||
} // end toArray
|
||||
|
||||
/** 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 number of entries currently in this bag.
|
||||
@return The integer number of entries currently in the bag. */
|
||||
public int getCurrentSize()
|
||||
{
|
||||
return numberOfEntries;
|
||||
} // end getCurrentSize
|
||||
|
||||
// STUBS:
|
||||
|
||||
/** Removes one unspecified entry from this bag, if possible.
|
||||
@return Either the removed entry, if the removal
|
||||
was successful, or null. */
|
||||
public T remove()
|
||||
{
|
||||
T top = firstNode.data;
|
||||
firstNode = firstNode.next;
|
||||
|
||||
numberOfEntries--;
|
||||
return top;
|
||||
} // 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 otherwise. */
|
||||
public boolean remove(T anEntry)
|
||||
{
|
||||
return false; // STUB
|
||||
} // end remove
|
||||
|
||||
/** Removes all entries from this bag. */
|
||||
public void clear()
|
||||
{
|
||||
// STUB
|
||||
} // end 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)
|
||||
{
|
||||
return 0; // STUB
|
||||
} // 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 false; // STUB
|
||||
} // end contains
|
||||
|
||||
private class Node
|
||||
{
|
||||
private T data; // Entry in bag
|
||||
private Node next; // Link to next node
|
||||
|
||||
private Node(T dataPortion)
|
||||
{
|
||||
this(dataPortion, null);
|
||||
} // end constructor
|
||||
|
||||
private Node(T dataPortion, Node nextNode)
|
||||
{
|
||||
data = dataPortion;
|
||||
next = nextNode;
|
||||
} // end constructor
|
||||
} // end Node
|
||||
} // end LinkedBag1
|
||||
|
||||
|
||||
|
85
DataLabs/lab4/LinkedBagDemo.java
Normal file
85
DataLabs/lab4/LinkedBagDemo.java
Normal file
@ -0,0 +1,85 @@
|
||||
/** A test of the methods add, toArray, isEmpty, and getCurrentSize,
|
||||
as defined in the first draft of the class LinkedBag.
|
||||
@author Frank M. Carrano
|
||||
@version 4.0
|
||||
*/
|
||||
public class LinkedBagDemo
|
||||
{
|
||||
public static void main(String[] args)
|
||||
{
|
||||
System.out.println("Creating an empty bag.");
|
||||
BagInterface<String> aBag = new LinkedBag<>();
|
||||
testIsEmpty(aBag, true);
|
||||
displayBag(aBag);
|
||||
|
||||
String[] contentsOfBag = {"A", "D", "B", "A", "C", "A", "D"};
|
||||
testAdd(aBag, contentsOfBag);
|
||||
testIsEmpty(aBag, false);
|
||||
} // end main
|
||||
|
||||
// Tests the method isEmpty.
|
||||
// Precondition: If the bag is empty, the parameter empty should be true;
|
||||
// otherwise, it should be false.
|
||||
private static void testIsEmpty(BagInterface<String> bag, boolean empty)
|
||||
{
|
||||
System.out.print("\nTesting isEmpty with ");
|
||||
if (empty)
|
||||
System.out.println("an empty bag:");
|
||||
else
|
||||
System.out.println("a bag that is not empty:");
|
||||
|
||||
System.out.print("isEmpty finds the bag ");
|
||||
if (empty && bag.isEmpty())
|
||||
System.out.println("empty: OK.");
|
||||
else if (empty)
|
||||
System.out.println("not empty, but it is: ERROR.");
|
||||
else if (!empty && bag.isEmpty())
|
||||
System.out.println("empty, but it is not empty: ERROR.");
|
||||
else
|
||||
System.out.println("not empty: OK.");
|
||||
} // end testIsEmpty
|
||||
// Tests the method add.
|
||||
private static void testAdd(BagInterface<String> aBag, String[] content)
|
||||
{
|
||||
System.out.print("Adding the following " + content.length +
|
||||
" strings to the bag: ");
|
||||
for (int index = 0; index < content.length; index++)
|
||||
{
|
||||
if (aBag.add(content[index]))
|
||||
System.out.print(content[index] + " ");
|
||||
else
|
||||
System.out.print("\nUnable to add " + content[index] +
|
||||
" to the bag.");
|
||||
} // end for
|
||||
System.out.println();
|
||||
|
||||
displayBag(aBag);
|
||||
} // end testAdd
|
||||
|
||||
// Tests the method toArray while displaying the bag.
|
||||
private static void displayBag(BagInterface<String> aBag)
|
||||
{
|
||||
System.out.println("The bag contains the following string(s):");
|
||||
Object[] bagArray = aBag.toArray();
|
||||
for (int index = 0; index < bagArray.length; index++)
|
||||
{
|
||||
System.out.print(bagArray[index] + " ");
|
||||
} // end for
|
||||
|
||||
System.out.println();
|
||||
} // end displayBag
|
||||
} // end LinkedBagDemo1
|
||||
/*
|
||||
Creating an empty bag.
|
||||
|
||||
Testing isEmpty with an empty bag:
|
||||
isEmpty finds the bag empty: OK.
|
||||
The bag contains the following string(s):
|
||||
|
||||
Adding the following 7 strings to the bag: A D B A C A D
|
||||
The bag contains the following string(s):
|
||||
D A C A B D A
|
||||
|
||||
Testing isEmpty with a bag that is not empty:
|
||||
isEmpty finds the bag not empty: OK.
|
||||
*/
|
Reference in New Issue
Block a user