//******************************************************************** // ClimbingTeamIsEmptyException.java // // Checked exception that is called from the remove method the // ClimbingTeam class. //******************************************************************* public class ClimbingTeamIsEmptyException extends Exception { public ClimbingTeamIsEmptyException() { super ("There are no climbers (left to kill) in this climbing team."); } } //******************************************************************** // Climber.java // // Represents a single climber. //******************************************************************* public class Climber { private String name; private int birthYear; private String nationality; //---------------------------------------------------------------- // Sets up the new climber with its information. //---------------------------------------------------------------- public Climber (String name, int birth, String country) { this.name = name; birthYear = birth; nationality = country; } //---------------------------------------------------------------- // Returns this climber as a string. //---------------------------------------------------------------- public String toString () { return name + ", born in " + birthYear + " in " + nationality; } //---------------------------------------------------------------- // Returns whether name is same as passed climberName. //---------------------------------------------------------------- public boolean equals (String climberName) { return (this.name).equals(climberName); } } //******************************************************************** // ClimbingTeam.java // // Represents a team of climbers. //******************************************************************* public class ClimbingTeam { private ClimberNode team; //---------------------------------------------------------------- // Sets up an initially empty list of climbers. //---------------------------------------------------------------- ClimbingTeam() { team = null; } //---------------------------------------------------------------- // Creates a new ClimberNode object and adds it to the end of // the linked list. //---------------------------------------------------------------- public void add (Climber newClimber) { ClimberNode node = new ClimberNode (newClimber); ClimberNode current; if (team == null) team = node; else { current = team; while (current.next != null) current = current.next; current.next = node; } } //---------------------------------------------------------------- // Finds the ClimberNode that holds a Climber with climberName as // their name, removes it from the linked list, and returns the Climber. //---------------------------------------------------------------- public Climber remove (String climberName) throws ClimbingTeamIsEmptyException { ClimberNode current = team; Climber result = null; if (current == null) //if list is empty throw a ClimbingTeamIsEmptyException throw new ClimbingTeamIsEmptyException(); else if (current.equals(climberName)) //our climber is the first one in the list { result = current.climber; team = current.next; } else //look through the rest of the list { while (current.next != null && !(current.next).equals(climberName)) current = current.next; if (current.next!=null) { result = (current.next).climber; current.next = (current.next).next; } } return result; } //---------------------------------------------------------------- // Returns this list of climbers as a string. //---------------------------------------------------------------- public String toString () { String result = ""; ClimberNode current = team; while (current != null) { result += current.climber.toString() + "\n"; current = current.next; } return result; } //***************************************************************** // An inner class that represents a node in the climbing team. The // public variables are accessed by the ClimbingTeam class. //***************************************************************** private class ClimberNode { public Climber climber; public ClimberNode next; //-------------------------------------------------------------- // Sets up the node //-------------------------------------------------------------- public ClimberNode (Climber theClimber) { climber = theClimber; next = null; } public boolean equals (String climberName) { return (this.climber).equals(climberName); } } } //******************************************************************** // K2Ascent.java // // Driver to exercise the ClimbingTeam collection. //******************************************************************* public class K2Ascent { //---------------------------------------------------------------- // Creates a ClimbingTeam object, adds several climbers to the // team, prints them, removes several climbers, prints the remaining. //---------------------------------------------------------------- public static void main (String[] args) { ClimbingTeam expedition1954 = new ClimbingTeam(); expedition1954.add (new Climber("Ardito Desio",1897,"Italy")); expedition1954.add (new Climber("Mario Puchoz",1918,"Italy")); expedition1954.add (new Climber("Lino Lacedelli",1925,"Italy")); expedition1954.add (new Climber("Achille Compagnoni",1914,"Italy")); System.out.println("Original team to attempt the first successful ascent of K2:\n"); System.out.println (expedition1954); // must deal with exception that might be thrown by the remove method try { expedition1954.remove ("Ardito Desio"); expedition1954.remove ("Mario Puchoz"); } catch (ClimbingTeamIsEmptyException theyAreAllDead) {} System.out.println("\nAscenders to summit K2 (8611m):\n"); System.out.println (expedition1954); } } //******************************************************************** // Queue.java // // A queue ADT's interface //******************************************************************** interface Queue { public int size(); public boolean isEmpty(); public Object dequeue(); public void enqueue(Object obj); } //******************************************************************** // ArrayQueue.java // // Implementation of the queue ADT using an array //******************************************************************** public class ArrayQueue implements Queue { private int front, rear; private Object[] theArray; public static final int SIZE = 100; public ArrayQueue() { front = 0; rear = 0; theArray = new Object[SIZE]; } public int size() { return (SIZE-front+rear)%SIZE; } public boolean isEmpty() { return (front==rear); } public Object dequeue() { if (isEmpty()) return null; Object temp = theArray[front]; theArray[front] = null; front = (front+1)%SIZE; return temp; } public void enqueue(Object obj) { if (size() != SIZE-1) { theArray[rear] = obj; rear = (rear+1)%SIZE; } } } //******************************************************************** // LinkedQueue.java // // Implementation of the queue ADT using a linked list //******************************************************************** public class LinkedQueue implements Queue { private Node head; private Node tail; private int size; public LinkedQueue() { head = null; tail = head; size = 0; } public int size() { return size; } public boolean isEmpty() { if (head==tail) return true; else return false; } public void enqueue(Object obj) { Node node = new Node(obj); if (size == 0) head = node; else tail.next = node; tail = node; size++; } public Object dequeue() { Object obj; if (size == 0) return null; obj = head.element; size--; head = head.next; if (size == 0) tail = head; return obj; } private class Node { public Object element; public Node next; public Node(Object o) { element = o; next = null; } } } //******************************************************************** // Farewell.java //******************************************************************** public class Farewell { public static void main(String[] args) { Queue goodbyeList = new ArrayQueue(); //or = new LinkedQueue() SuperCat frisky = new SuperCat(3,4.1f,false); Bus luckyBus = new Bus(100,6); Hyena fisi = new Hyena("Fisi", 80, "anything and Everything", 8.0, true); goodbyeList.enqueue(luckyBus); goodbyeList.enqueue(frisky); goodbyeList.enqueue(fisi); Bus theBus = (Bus) goodbyeList.dequeue(); theBus.minimumTime(430); SuperCat theCat = (SuperCat) goodbyeList.dequeue(); System.out.println("\n" + goodbyeList.dequeue() + "\n"); System.out.println(theCat); System.out.println("\n\n ...On behalf of all of us, I would like to wish you:" + "\n******All the best in the rest of your studies!******"); } }