import java.util.*;
//----------------------------------------------------------------------------
// ListInterface.java by Dale/Joyce/Weems Chapter 6
//
// The lists are unbounded and allow duplicate elements, but do not allow
// null elements. As a general precondition, null elements are not passed as
// arguments to any of the methods.
//
// The list has a special property called the current position - the position
// of the next element to be accessed by getNext during an iteration through
// the list. Only reset and getNext affect the current position.
//----------------------------------------------------------------------------
interface ListInterface<T>
{
int size();
// Returns the number of elements on this list.
void add(T element);
// Adds element to this list.
boolean remove (T element);
// Removes an element e from this list such that e.equals(element)
// and returns true; if no such element exists, returns false.
boolean contains (T element);
// Returns true if this list contains an element e such that
// e.equals(element); otherwise, returns false.
T get(T element);
// Returns an element e from this list such that e.equals(element);
// if no such element exists, returns null.
// Returns a nicely formatted string that represents this list.
void reset();
// Initializes current position for an iteration through this list,
// to the first element on this list.
T getNext();
// Preconditions: The list is not empty
// The list has been reset
// The list has not been modified since the most recent reset
//
// Returns the element at the current position on this list.
// If the current position is the last element, then it advances the value
// of the current position to the first element; otherwise, it advances
// the value of the current position to the next element.
}
//----------------------------------------------------------------------------
// ArrayUnsortedList.java by Dale/Joyce/Weems Chapter 6
//
// Implements the ListInterface using an array.
//
// Null elements are not permitted on a list.
//
// Two constructors are provided: one that creates a list of a default
// original capacity, and one that allows the calling program to specify the
// original capacity.
//----------------------------------------------------------------------------
class ArrayUnsortedList<T> implements ListInterface<T>
{
protected final int DEFCAP = 100; // default capacity
protected int origCap; // original capacity
protected T[] list; // array to hold this list’s elements
protected int numElements = 0; // number of elements in this list
protected int currentPos; // current position for iteration
// set by find method
protected boolean found; // true if element found, otherwise false
protected int location; // indicates location of element if found
public ArrayUnsortedList()
{
list
= (T
[]) new Object[DEFCAP
]; origCap = DEFCAP;
}
public ArrayUnsortedList(int origCap)
{
list
= (T
[]) new Object[origCap
]; this.origCap = origCap;
}
protected void enlarge()
// Increments the capacity of the list by an amount
// equal to the original capacity.
{
// Create the larger array.
T
[] larger
= (T
[]) new Object[list.
length + origCap
];
// Copy the contents from the smaller array into the larger array.
for (int i = 0; i < numElements; i++)
{
larger[i] = list[i];
}
// Reassign list reference.
list = larger;
}
protected void find(T target)
// Searches list for an occurence of an element e such that
// e.equals(target). If successful, sets instance variables
// found to true and location to the array index of e. If
// not successful, sets found to false.
{
location = 0;
found = false;
while (location < numElements)
{
if (list[location].equals(target))
{
found = true;
return;
}
else
location++;
}
}
public void add(T element)
// Adds element to this list.
{
if (numElements == list.length)
enlarge();
list[numElements] = element;
numElements++;
}
public boolean remove (T element)
// Removes an element e from this list such that e.equals(element)
// and returns true; if no such element exists, returns false.
{
find(element);
if (found)
{
list[location] = list[numElements - 1];
list[numElements - 1] = null;
numElements--;
}
return found;
}
public int size()
// Returns the number of elements on this list.
{
return numElements;
}
public boolean contains (T element)
// Returns true if this list contains an element e such that
// e.equals(element); otherwise, returns false.
{
find(element);
return found;
}
public T get(T element)
// Returns an element e from this list such that e.equals(element);
// if no such element exists, returns null.
{
find(element);
if (found)
return list[location];
else
return null;
}
// Returns a nicely formatted string that represents this list.
{
for (int i = 0; i < numElements; i++)
listString = listString + "" + list[i] + "\n";
return listString;
}
public void reset()
// Initializes current position for an iteration through this list,
// to the first element on this list.
{
currentPos = 0;
}
public T getNext()
// Preconditions: The list is not empty
// The list has been reset
// The list has not been modified since the most recent reset
//
// Returns the element at the current position on this list.
// If the current position is the last element, it advances the value
// of the current position to the first element; otherwise, it advances
// the value of the current position to the next element.
{
T next = list[currentPos];
if (currentPos == (numElements - 1))
currentPos = 0;
else
currentPos++;
return next;
}
}
//----------------------------------------------------------------------------
// ArraySortedList3.java by Dale/Joyce/Weems Chapter 6
//
// Implements the ListInterface using an array. It is kept in increasing order
// as defined by the compareTo method of the added elements. Only Comparable
// elements may be added to a list.
//
// Null elements are not permitted on a list.
//
// Two constructors are provided: one that creates a list of a default
// original capacity, and one that allows the calling program to specify the
// original capacity.
//----------------------------------------------------------------------------
class ArraySortedList3<T> extends ArrayUnsortedList<T>
implements ListInterface<T>
{
public ArraySortedList3()
{
super();
}
public ArraySortedList3(int origCap)
{
super(origCap);
}
protected void recFind
(Comparable target,
int fromLocation,
int toLocation
) // Searches list between fromLocation and toLocation
// for an occurrence of an element e such that
// target.equals(e). If successful, sets instance variables
// found to true and location to the array index of e. If
// not successful, sets found to false.
{
if (fromLocation > toLocation) // Base case 1
found = false;
else
{
int compareResult;
location = (fromLocation + toLocation) / 2;
compareResult = target.compareTo(list[location]);
if (compareResult == 0) // Base case 2
found = true;
else if (compareResult < 0)
// target is less than element at location
recFind (target, fromLocation, location - 1);
else
// target is greater than element at location
recFind (target, location + 1, toLocation);
}
}
protected void find(T target)
// Searches list for an occurrence of an element e such that
// target.equals(e). If successful, sets instance variables
// found to true and location to the array index of e. If
// not successful, sets found to false.
{
found = false;
recFind(targetElement, 0, numElements - 1);
}
public void add(T element)
// Precondition: element is Comparable.
//
// Adds element to this list.
{
T listElement;
int location = 0;
if (numElements == list.length)
enlarge();
while (location < numElements)
{
listElement = (T)list[location];
if (((Comparable)listElement
).
compareTo(element
) < 0) // list element < add element location++;
else
break; // list element >= add element
}
for (int index = numElements; index > location; index--)
list[index] = list[index - 1];
list[location] = element;
numElements++;
}
public boolean remove (T element)
// Removes an element e from this list such that e.equals(element)
// and returns true; if no such element exists, returns false.
{
find(element);
if (found)
{
for (int i = location; i <= numElements - 2; i++)
list[i] = list[i+1];
list[numElements - 1] = null;
numElements--;
}
return found;
}
}
class Athlete implements Comparable<Athlete>
{
//variables needed
protected int rank;
//default constructor
public Athlete()
{
name = "";
country = "";
event = "";
rank = 0;
}
//overloaded constructor
{
this.name = name;
this.country = country;
this.event = event;
this.rank = rank;
}
//setName method
public void setName
(String name
) {
this.name = name;
}
//getName method
{
return name;
}
//setCountry method
public void setCountry
(String country
) {
this.country = country;
}
//getCountry method
{
return country;
}
//setEvent method
public void setEvent
(String event
) {
this.event = event;
}
//getEvent method
{
return event;
}
//setRank method
public void setRank(int rank)
{
this.rank = rank;
}
//getRank method
public int getRank()
{
return rank;
}
//compareTo method
public int compareTo(Athlete other)
{
if(this.rank > other.rank)
return -1;
else
if(this.rank == other.rank)
return 0;
else
return +1;
}
//toString method
{
str += "Name: " + name + "\nCountry: " + country + "\nEvent: " + event + "\nRank: " + rank + "\n";
return str;
}
}
class OlympicAthletes<Athlete>
{
//create list to store athletes
ListInterface<Athlete> Athletes = new ArraySortedList3<Athlete>();
//addAthlete method
public void addAthlete(Athlete newAthlete)
{
Athletes.add(newAthlete);
}
//deleteAthlete method
public void deleteAthlete(Athlete delAthlete)
{
if(Athletes.contains(delAthlete))
{
Athletes.remove(delAthlete);
}
}
//printParticularAthlete given game and country of the athlete
{
for(int i = 0; i < Athletes.size(); i++)
{
if(Athletes.contains((Athlete)name))
{
str += "true";
}
}
return str;
}
}