fork download
  1. import java.util.*;
  2. //----------------------------------------------------------------------------
  3. // ListInterface.java by Dale/Joyce/Weems Chapter 6
  4. //
  5. // The lists are unbounded and allow duplicate elements, but do not allow
  6. // null elements. As a general precondition, null elements are not passed as
  7. // arguments to any of the methods.
  8. //
  9. // The list has a special property called the current position - the position
  10. // of the next element to be accessed by getNext during an iteration through
  11. // the list. Only reset and getNext affect the current position.
  12. //----------------------------------------------------------------------------
  13. interface ListInterface<T>
  14. {
  15. int size();
  16. // Returns the number of elements on this list.
  17.  
  18. void add(T element);
  19. // Adds element to this list.
  20.  
  21. boolean remove (T element);
  22. // Removes an element e from this list such that e.equals(element)
  23. // and returns true; if no such element exists, returns false.
  24.  
  25. boolean contains (T element);
  26. // Returns true if this list contains an element e such that
  27. // e.equals(element); otherwise, returns false.
  28.  
  29. T get(T element);
  30. // Returns an element e from this list such that e.equals(element);
  31. // if no such element exists, returns null.
  32.  
  33. String toString();
  34. // Returns a nicely formatted string that represents this list.
  35.  
  36. void reset();
  37. // Initializes current position for an iteration through this list,
  38. // to the first element on this list.
  39.  
  40. T getNext();
  41. // Preconditions: The list is not empty
  42. // The list has been reset
  43. // The list has not been modified since the most recent reset
  44. //
  45. // Returns the element at the current position on this list.
  46. // If the current position is the last element, then it advances the value
  47. // of the current position to the first element; otherwise, it advances
  48. // the value of the current position to the next element.
  49. }
  50.  
  51.  
  52.  
  53. //----------------------------------------------------------------------------
  54. // ArrayUnsortedList.java by Dale/Joyce/Weems Chapter 6
  55. //
  56. // Implements the ListInterface using an array.
  57. //
  58. // Null elements are not permitted on a list.
  59. //
  60. // Two constructors are provided: one that creates a list of a default
  61. // original capacity, and one that allows the calling program to specify the
  62. // original capacity.
  63. //----------------------------------------------------------------------------
  64. class ArrayUnsortedList<T> implements ListInterface<T>
  65. {
  66. protected final int DEFCAP = 100; // default capacity
  67. protected int origCap; // original capacity
  68. protected T[] list; // array to hold this list’s elements
  69. protected int numElements = 0; // number of elements in this list
  70. protected int currentPos; // current position for iteration
  71.  
  72. // set by find method
  73. protected boolean found; // true if element found, otherwise false
  74. protected int location; // indicates location of element if found
  75.  
  76. public ArrayUnsortedList()
  77. {
  78. list = (T[]) new Object[DEFCAP];
  79. origCap = DEFCAP;
  80. }
  81.  
  82. public ArrayUnsortedList(int origCap)
  83. {
  84. list = (T[]) new Object[origCap];
  85. this.origCap = origCap;
  86. }
  87.  
  88. protected void enlarge()
  89. // Increments the capacity of the list by an amount
  90. // equal to the original capacity.
  91. {
  92. // Create the larger array.
  93. T[] larger = (T[]) new Object[list.length + origCap];
  94.  
  95. // Copy the contents from the smaller array into the larger array.
  96. for (int i = 0; i < numElements; i++)
  97. {
  98. larger[i] = list[i];
  99. }
  100.  
  101. // Reassign list reference.
  102. list = larger;
  103. }
  104.  
  105. protected void find(T target)
  106. // Searches list for an occurence of an element e such that
  107. // e.equals(target). If successful, sets instance variables
  108. // found to true and location to the array index of e. If
  109. // not successful, sets found to false.
  110. {
  111. location = 0;
  112. found = false;
  113.  
  114. while (location < numElements)
  115. {
  116. if (list[location].equals(target))
  117. {
  118. found = true;
  119. return;
  120. }
  121. else
  122. location++;
  123. }
  124. }
  125.  
  126. public void add(T element)
  127. // Adds element to this list.
  128. {
  129. if (numElements == list.length)
  130. enlarge();
  131. list[numElements] = element;
  132. numElements++;
  133. }
  134.  
  135. public boolean remove (T element)
  136. // Removes an element e from this list such that e.equals(element)
  137. // and returns true; if no such element exists, returns false.
  138. {
  139. find(element);
  140. if (found)
  141. {
  142. list[location] = list[numElements - 1];
  143. list[numElements - 1] = null;
  144. numElements--;
  145. }
  146. return found;
  147. }
  148.  
  149. public int size()
  150. // Returns the number of elements on this list.
  151. {
  152. return numElements;
  153. }
  154.  
  155. public boolean contains (T element)
  156. // Returns true if this list contains an element e such that
  157. // e.equals(element); otherwise, returns false.
  158. {
  159. find(element);
  160. return found;
  161. }
  162.  
  163. public T get(T element)
  164. // Returns an element e from this list such that e.equals(element);
  165. // if no such element exists, returns null.
  166. {
  167. find(element);
  168. if (found)
  169. return list[location];
  170. else
  171. return null;
  172. }
  173.  
  174. public String toString()
  175. // Returns a nicely formatted string that represents this list.
  176. {
  177. String listString = "";
  178. for (int i = 0; i < numElements; i++)
  179. listString = listString + "" + list[i] + "\n";
  180. return listString;
  181. }
  182.  
  183. public void reset()
  184. // Initializes current position for an iteration through this list,
  185. // to the first element on this list.
  186. {
  187. currentPos = 0;
  188. }
  189.  
  190. public T getNext()
  191. // Preconditions: The list is not empty
  192. // The list has been reset
  193. // The list has not been modified since the most recent reset
  194. //
  195. // Returns the element at the current position on this list.
  196. // If the current position is the last element, it advances the value
  197. // of the current position to the first element; otherwise, it advances
  198. // the value of the current position to the next element.
  199. {
  200. T next = list[currentPos];
  201. if (currentPos == (numElements - 1))
  202. currentPos = 0;
  203. else
  204. currentPos++;
  205. return next;
  206. }
  207. }
  208.  
  209.  
  210. //----------------------------------------------------------------------------
  211. // ArraySortedList3.java by Dale/Joyce/Weems Chapter 6
  212. //
  213. // Implements the ListInterface using an array. It is kept in increasing order
  214. // as defined by the compareTo method of the added elements. Only Comparable
  215. // elements may be added to a list.
  216. //
  217. // Null elements are not permitted on a list.
  218. //
  219. // Two constructors are provided: one that creates a list of a default
  220. // original capacity, and one that allows the calling program to specify the
  221. // original capacity.
  222. //----------------------------------------------------------------------------
  223. class ArraySortedList3<T> extends ArrayUnsortedList<T>
  224. implements ListInterface<T>
  225. {
  226.  
  227. public ArraySortedList3()
  228. {
  229. super();
  230. }
  231.  
  232. public ArraySortedList3(int origCap)
  233. {
  234. super(origCap);
  235. }
  236.  
  237. protected void recFind(Comparable target, int fromLocation, int toLocation)
  238. // Searches list between fromLocation and toLocation
  239. // for an occurrence of an element e such that
  240. // target.equals(e). If successful, sets instance variables
  241. // found to true and location to the array index of e. If
  242. // not successful, sets found to false.
  243. {
  244. if (fromLocation > toLocation) // Base case 1
  245. found = false;
  246. else
  247. {
  248. int compareResult;
  249. location = (fromLocation + toLocation) / 2;
  250. compareResult = target.compareTo(list[location]);
  251.  
  252. if (compareResult == 0) // Base case 2
  253. found = true;
  254. else if (compareResult < 0)
  255. // target is less than element at location
  256. recFind (target, fromLocation, location - 1);
  257. else
  258. // target is greater than element at location
  259. recFind (target, location + 1, toLocation);
  260. }
  261. }
  262.  
  263. protected void find(T target)
  264. // Searches list for an occurrence of an element e such that
  265. // target.equals(e). If successful, sets instance variables
  266. // found to true and location to the array index of e. If
  267. // not successful, sets found to false.
  268. {
  269. Comparable targetElement = (Comparable)target;
  270. found = false;
  271. recFind(targetElement, 0, numElements - 1);
  272. }
  273.  
  274. public void add(T element)
  275. // Precondition: element is Comparable.
  276. //
  277. // Adds element to this list.
  278. {
  279. T listElement;
  280. int location = 0;
  281.  
  282. if (numElements == list.length)
  283. enlarge();
  284.  
  285. while (location < numElements)
  286. {
  287. listElement = (T)list[location];
  288. if (((Comparable)listElement).compareTo(element) < 0) // list element < add element
  289. location++;
  290. else
  291. break; // list element >= add element
  292. }
  293.  
  294. for (int index = numElements; index > location; index--)
  295. list[index] = list[index - 1];
  296.  
  297. list[location] = element;
  298. numElements++;
  299. }
  300.  
  301. public boolean remove (T element)
  302. // Removes an element e from this list such that e.equals(element)
  303. // and returns true; if no such element exists, returns false.
  304. {
  305. find(element);
  306. if (found)
  307. {
  308. for (int i = location; i <= numElements - 2; i++)
  309. list[i] = list[i+1];
  310. list[numElements - 1] = null;
  311. numElements--;
  312. }
  313. return found;
  314. }
  315. }
  316.  
  317.  
  318.  
  319. class Athlete implements Comparable<Athlete>
  320. {
  321. //variables needed
  322. protected String name;
  323. protected String country;
  324. protected String event;
  325. protected int rank;
  326.  
  327. //default constructor
  328. public Athlete()
  329. {
  330. name = "";
  331. country = "";
  332. event = "";
  333. rank = 0;
  334. }
  335.  
  336. //overloaded constructor
  337. public Athlete(String name, String country, String event, int rank)
  338. {
  339. this.name = name;
  340. this.country = country;
  341. this.event = event;
  342. this.rank = rank;
  343. }
  344.  
  345. //setName method
  346. public void setName(String name)
  347. {
  348. this.name = name;
  349. }
  350.  
  351. //getName method
  352. public String getName()
  353. {
  354. return name;
  355. }
  356.  
  357. //setCountry method
  358. public void setCountry(String country)
  359. {
  360. this.country = country;
  361. }
  362.  
  363. //getCountry method
  364. public String getCountry()
  365. {
  366. return country;
  367. }
  368.  
  369. //setEvent method
  370. public void setEvent(String event)
  371. {
  372. this.event = event;
  373. }
  374.  
  375. //getEvent method
  376. public String getEvent()
  377. {
  378. return event;
  379. }
  380.  
  381. //setRank method
  382. public void setRank(int rank)
  383. {
  384. this.rank = rank;
  385. }
  386.  
  387. //getRank method
  388. public int getRank()
  389. {
  390. return rank;
  391. }
  392.  
  393. //compareTo method
  394. public int compareTo(Athlete other)
  395. {
  396. if(this.rank > other.rank)
  397. return -1;
  398. else
  399. if(this.rank == other.rank)
  400. return 0;
  401. else
  402. return +1;
  403. }
  404.  
  405. //toString method
  406. public String toString()
  407. {
  408. String str = "";
  409.  
  410. str += "Name: " + name + "\nCountry: " + country + "\nEvent: " + event + "\nRank: " + rank + "\n";
  411.  
  412. return str;
  413. }
  414. }
  415.  
  416.  
  417. class OlympicAthletes<Athlete>
  418. {
  419. //create list to store athletes
  420. ListInterface<Athlete> Athletes = new ArraySortedList3<Athlete>();
  421.  
  422. //addAthlete method
  423. public void addAthlete(Athlete newAthlete)
  424. {
  425. Athletes.add(newAthlete);
  426. }
  427.  
  428. //deleteAthlete method
  429. public void deleteAthlete(Athlete delAthlete)
  430. {
  431. if(Athletes.contains(delAthlete))
  432. {
  433. Athletes.remove(delAthlete);
  434. }
  435. }
  436.  
  437. //printParticularAthlete given game and country of the athlete
  438. public String printParticularAthlete(String name, String country)
  439. {
  440. String str = "";
  441. for(int i = 0; i < Athletes.size(); i++)
  442. {
  443. if(Athletes.contains((Athlete)name))
  444. {
  445. str += "true";
  446. }
  447. }
  448. return str;
  449. }
  450. }
  451.  
  452.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Note: Main.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
spoj: The program compiled successfully, but main class was not found.
      Main class should contain method: public static void main (String[] args).
stdout
Standard output is empty