UQ Students should read the Disclaimer & Warning
Note: This page dates from 2005, and is kept for historical purposes.
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>COMP2500 - Assignment Two</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<style type="text/css">
<!--
.wrong {
background: #FF9999;
}
body {
background: url(_img/DSC04989.jpg) fixed center;
font-family: "Arial Unicode MS", Arial, Helvetica, sans-serif;
}
th, td, textarea {
border: 1px solid #000000;
padding: 0 1ex;
background: transparent;
overflow: hidden;
}
table {
border: none;
}
-->
</style>
</head>
<body>
<h1>COMP2500 – Programming in the Large – Assignment Two</h1>
<table border="1" summary="Criteria and results achieved for COMP2500 assignment one">
<thead>
<tr>
<th colspan="3">Criteria and Results</th>
</tr>
</thead>
<tfoot>
<tr>
<td colspan="2">Average class result</td>
<td>—</td>
</tr>
</tfoot>
<tbody>
<tr>
<th colspan="3" scope="col">Quantity and quality of comments (0-4)</th>
</tr>
<tr>
<td>accurate, clear and succint comments</td>
<td>4</td>
<td>4 ✔</td>
</tr>
<tr>
<td>a number of problems with comments</td>
<td>2</td>
<td> </td>
</tr>
<tr>
<td>work with little or no academic merit</td>
<td>0</td>
<td> </td>
</tr>
<tr>
<th colspan="3" scope="col">Review of test plan (0-6)</th>
</tr>
<tr>
<td>test plan that is adequate, clear and succint</td>
<td>6</td>
<td> </td>
</tr>
<tr>
<td>test plan with a number of problems</td>
<td>4</td>
<td>4½ ✔</td>
</tr>
<tr>
<td>test plan that is clearly insufficient, too complex or hard to
understand </td>
<td>2</td>
<td> </td>
</tr>
<tr>
<td>work with little or no academic merit</td>
<td>0</td>
<td> </td>
</tr>
<tr>
<th colspan="3" scope="col">Review of test implementation (0-4)</th>
</tr>
<tr>
<td>test implementation that is correct and follows test plan</td>
<td>4</td>
<td>4 ✔</td>
</tr>
<tr>
<td>test implementation with a number of problems</td>
<td>2</td>
<td> </td>
</tr>
<tr>
<td>work with little or no academic merit</td>
<td>0</td>
<td> </td>
</tr>
<tr>
<th colspan="3" scope="col">Our execution of your test cases in IntBagDriver.script
(0-6)</th>
</tr>
<tr>
<td>testing can handle any correct implementation (independent of
order in which elements are returned by next) and all faulty implementations
are detected</td>
<td>6</td>
<td>6 ✔</td>
</tr>
<tr>
<td>all faulty implementations detected, but testing only works for
one specific iteration order</td>
<td>5</td>
<td> </td>
</tr>
<tr>
<td>one or two faulty implementations not detected</td>
<td>4</td>
<td> </td>
</tr>
<tr>
<td>majority of faulty implementations not detected</td>
<td>2</td>
<td> </td>
</tr>
<tr>
<td>work with little or no academic merit</td>
<td>0</td>
<td> </td>
</tr>
<tr>
<th scope="row">Total Possible Marks</th>
<td>15</td>
<td>18½/20</td>
</tr>
</tbody>
</table>
<h2>Submitted Code</h2>
<p>IntBag.java<br />
View the <a href="JavaDoc/IntBag" title="JavaDoc documentation for this code">JavaDoc</a><br />
<textarea name="textarea" cols="82" rows="222" readonly="readonly" title="Java Code - Copyright 2003 Ned Martin">/**
* Stores a bounded collection of integers.
* Duplicates are allowed.
* @author Paul Strooper
* @author Jack the Hacker
* @author Ned Martin (40529927)
* @version 1.1 10-SEP-2003
* Modified by Jack the Hacker to allow duplicate entries and basic
* iteration
*/
public class IntBag
{
/**
* Maximum size of the set.
*/
public static final int MAX_SIZE = 100;
// array to store the elements in the collection
private int [] elements;
private int count, // number of elements in the collection
current; // current element in collection
/**
* Creates an empty collection.
*/
public IntBag()
{
// allocate elements in array and initialise count
elements = new int[MAX_SIZE];
count = 0;
current = -1;
}
/**
* Insert the integer x into the collection.
* @param x the integer to be added
* @throws FullExc there are MAX_SIZE elements in the set
*/
public void insert
(
int x // integer to insert into collection
)
throws FullExc // the integer x is already present
{
// check if set is full
if (count == MAX_SIZE)
{
throw new FullExc();
}
// copy x to end of array and increment count
elements[count++] = x;
current = -1;
}
/**
* Remove the first found integer x from the collection.
* @param x the integer to be removed
* @throws NotFoundExc x is not in the collection
*/
public void remove
(
int x // integer to remove from collection
)
throws NotFoundExc // x is not in the collection
{
if (!removeOne(x))
{
throw new NotFoundExc();
}
}
/**
* Removes all occurences of integer x from the collection.
* @param x the integer to be removed
* @throws NotFoundExc x is not in the collection
*/
public void removeAllOccurrences
(
int x // integer to remove
)
throws NotFoundExc // x is not in the collection
{
if (!removeOne(x))
{
throw new NotFoundExc();
}
while (removeOne(x));
}
/**
* Determine whether or not the integer x is in the collection.
* @param x the integer to be checked
* @return true if and only if x is in the set
*/
public boolean isMember
(
int x // integer to check
)
{
return (findPosition(x) != -1);
}
/**
* Determine the size of the collection.
* @return the size of the collection
*/
public int size()
{
return count;
}
/**
* Determines number of times integer x is found in the collection
* @return number of times element found in collection
* @param x the integer to find
* @throws NotFoundExc integer x not found in collection
*/
public int numberOfOccurrences
(
int x // integer to find
)
throws NotFoundExc // x not found in collection
{
int cnt = 0;
for (int i = 0; i < count; i++)
{
if (elements[i] == x)
{
cnt ++;
}
}
if (cnt == 0)
{
throw new NotFoundExc();
}
return cnt;
}
/**
* Resets current to zero
*/
public void start()
{
current = 0;
}
/**
* Checks if collection has another element
* @return true when collection has next element
* @throws UnstableIteratorExc Iterator out of bounds
*/
public boolean hasNext()
throws UnstableIteratorExc // Iterator out of bounds
{
if (current < 0 )
{
throw new UnstableIteratorExc();
}
return current < count;
}
/**
* Gets next integer from collection
* @return next integer from collection
* @throws UnstableIteratorExc Iterator out of bounds
*/
public int getNext()
throws UnstableIteratorExc // Iterator out of bounds
{
if (current < 0 || current >= count)
{
throw new UnstableIteratorExc();
}
return elements[current++];
}
/**
* Find the position of x in the first count elements of the array
* elements.
* @return first position element found in
* @return -1 element not found
*/
private int findPosition
(
int x
)
{
for (int i = 0; i < count; i++)
{
if (elements[i] == x)
{
return(i);
}
}
return -1;
}
/**
* Removes integer x from collection
* @return true when integer removed, false otherwise
*/
private boolean removeOne
(
int x // integer to remove
)
{
// find position of element in the collection
int pos = findPosition(x);
// check if x not in the collection
if (pos == -1)
{
return false;
}
// copy last element to position pos and decrement count
elements[pos] = elements[count-1];
count--;
current = -1;
return true;
}
}
</textarea>
</p>
<p>IntBagDriver.script<br />
<textarea name="textarea2" cols="82" rows="163" readonly="readonly" title="Java Code - Copyright 2003 Ned Martin">import roast.*;
/*
* This class implements a Roast script for testing the IntBag class.
*/
public class IntBagDriver {
/*
* Main function implements all the test cases.
*/
public static void main(String[] args)
{
try
{
Roast.parseArgs(null,args);
}
catch (ParameterException pe)
{
Roast.logUtilityMessage("Test failed: no exception"+
" expected",0);
}
Roast.logUtilityMessage("Testing empty collection",1);
IntBag s0 = new IntBag();
#excMonitor s0.remove(0); # new NotFoundExc() #end
#excMonitor s0.removeAllOccurrences(0); # new NotFoundExc() #end
#excMonitor s0.numberOfOccurrences(0); # new NotFoundExc() #end
#excMonitor s0.hasNext(); # new UnstableIteratorExc() #end
#excMonitor s0.getNext(); # new UnstableIteratorExc() #end
#valueCheck s0.isMember(0) # false #end
#valueCheck s0.size() # 0 #end
Roast.logUtilityMessage("Testing collection with 1 element",1);
IntBag s1 = new IntBag();
Roast.logUtilityMessage("load collection",2);
#excMonitor s1.insert(10); #end
Roast.logUtilityMessage("test collection",2);
#excMonitor s1.remove(0); # new NotFoundExc() #end
#valueCheck s1.isMember(0) # false #end
#valueCheck s1.isMember(10) # true #end
#excMonitor s1.numberOfOccurrences(0); # new NotFoundExc() #end
#valueCheck s1.numberOfOccurrences(10) # 1 #end
#excMonitor s1.hasNext(); # new UnstableIteratorExc() #end
#excMonitor s1.getNext(); # new UnstableIteratorExc() #end
#valueCheck s1.size() # 1 #end
Roast.logUtilityMessage("remove element and test again",2);
#excMonitor s1.remove(10); #end
#excMonitor s1.remove(10); # new NotFoundExc() #end
#valueCheck s1.isMember(10) # false #end
#valueCheck s1.size() # 0 #end
#excMonitor s1.numberOfOccurrences(10); # new NotFoundExc() #end
// test sets of sizes {3,MAX_SIZE/2,MAX_SIZE}
testSet(3, false);
testSet(IntBag.MAX_SIZE/2, false);
testSet(IntBag.MAX_SIZE, false);
// include duplicates
testSet(IntBag.MAX_SIZE, true);
Roast.printCounts();
}
/*
* Function that tests a collection of size elements
*/
static void testSet(int size, boolean duplicates)
{
Roast.logUtilityMessage("Testing collection with "
+ size + " elements",1);
IntBag s = new IntBag();
Roast.logUtilityMessage("load collection",2);
// populate collection
for (int i = 0; i < size; i++)
{
// if duplicates are used
if (duplicates)
{
#excMonitor s.insert(10); #end
}
else
{
#excMonitor s.insert(10*(i+1)); #end
}
}
Roast.logUtilityMessage("test collection",2);
// if collection is full
if (size == IntBag.MAX_SIZE) {
#excMonitor s.insert(0); # new FullExc() #end
}
#excMonitor s.remove(0); # new NotFoundExc() #end
#excMonitor s.start(); #end
#valueCheck s.hasNext() # true #end
#excMonitor s.start(); #end
#valueCheck s.getNext() # 10 #end
#valueCheck s.isMember(0) # false #end
#excMonitor s.numberOfOccurrences(0); # new NotFoundExc() #end
#valueCheck s.isMember(10) # true #end
// testing specific to non duplicated collections
if (!duplicates)
{
#valueCheck s.isMember(10*((size+1)/2)) # true #end
#valueCheck s.isMember(10*size) # true #end
#valueCheck s.numberOfOccurrences(10) # 1 #end
#valueCheck s.numberOfOccurrences(10*size) # 1 #end
#valueCheck s.numberOfOccurrences(10*((size+1)/2)) # 1
#end
}
#valueCheck s.size() # size #end
Roast.logUtilityMessage("remove some elements"+
" and test again",2);
#excMonitor s.remove(10); #end
// testing specific to non duplicated collections
if (!duplicates)
{
#excMonitor s.remove(10*((size+1)/2)); #end
#excMonitor s.remove(10*size); #end
#valueCheck s.isMember(10) # false #end
#valueCheck s.isMember(10*((size+1)/2)) # false #end
#valueCheck s.isMember(10*size) # false #end
#excMonitor s.numberOfOccurrences(10); #
new NotFoundExc() #end
#excMonitor s.numberOfOccurrences(10*((size+1)/2)); #
new NotFoundExc() #end
#excMonitor s.numberOfOccurrences(10*size); #
new NotFoundExc() #end
#valueCheck s.size() # size-3 #end
}
// testing specific to collections containing duplicates
if (duplicates)
{
#valueCheck s.numberOfOccurrences(10) # size - 1 #end
Roast.logUtilityMessage("remove all duplicate elements"+
" and test again",2);
#excMonitor s.removeAllOccurrences(10); #end
#excMonitor s.removeAllOccurrences(10); #
new NotFoundExc() #end
#excMonitor s.numberOfOccurrences(10); #
new NotFoundExc() #end
#valueCheck s.size() # 0 #end
}
}
}
</textarea>
<br />
Code © Copyright 2003 Ned Martin</p>
<p>01-Nov-2003</p>
</body>
</html>