LinkedList Exercise

  • ExLinkedListSample
    import java.util.*;
    import java.io.*;
    import coffeemaker.*;
    
    /**
     * ExLinkedListSample 
     * 

    * This class implements some methods to practice the * use of the class {@link LinkedList}. *

    * * @author Alfonso Rodruez * @version 1.0.0 * @see CoffeeMaker * @see CoffeeMakerFormatException * @see LinkedList */ public class ExLinkedListSample { /** * Creates a LinkedList with three elements. *

    * The first element of the LinkedList is the object * s1, the second element of the LinkedList is * the object s2, and the third element of the * LinkedList is the object s3 *

    * * @param cm1 a CoffeeMaker object * @param cm2 a CoffeeMaker object * @param cm3 a CoffeeMaker object * @return a LinkedList with the objects cm1, * cm2, and cm3 */ public static LinkedList makeLinkedList3(CoffeeMaker cm1, CoffeeMaker cm2, CoffeeMaker cm3) { LinkedList v = new LinkedList(); v.add(cm1); v.add(cm2); v.add(cm3); return v; } /** * Creates a LinkedList from an array of * CoffeeMaker *

    * The elements in the LinkedList have the same order that in * the array *

    * * @param anArray an array of CoffeeMaker * @return a LinkedList with the objects stored in the array * anArray */ public static LinkedList makeLinkedListFromArray(CoffeeMaker anArray[]) { LinkedList v = new LinkedList(); for (int i = 0; i < anArray.length; i++) { v.add(anArray[i]); } return v; } /** * Creates a LinkedList object from an input stream *

    * The data of each student is stored in one line in the input stream, * and each field is separated by a the delimiter specified in the parameter * DELIM, for example. *

    *
    	 *    Super486_1000_
    	 *    PentiumIV_1500_
    	 *    HotCoffee_900_
    	 * 
    *

    * This method uses {@link StringTokenizer} to obtain each field from a line *

    * * @param br a {@link Reader} from which CoffeeMaker * data are read * @param DELIM a String with the delimiter used in the stream * referenced by br * @return a LinkedList with the CoffeeMaker data * stored in the stream br * @throws {@link IOException} if cannot read from the input stream * @throws {@link CoffeeMakerFormatException} if receives a badly formed data */ public static LinkedList makeLinkedListFromReader(Reader r, final String DELIM) throws IOException, CoffeeMakerFormatException { BufferedReader br = new BufferedReader(r); LinkedList v = new LinkedList(); StringTokenizer st; String line = ""; String name; int cap; try { line = br.readLine(); while (line != null) { st = new StringTokenizer(line, DELIM); name = st.nextToken(); cap = Integer.parseInt(st.nextToken()); if (st.hasMoreTokens()) { throw new CoffeeMakerFormatException(line); } v.add(new CoffeeMaker(name, cap)); line = br.readLine(); } } catch (NoSuchElementException nsee) { throw new CoffeeMakerFormatException(line); } catch (NumberFormatException nfe) { throw new CoffeeMakerFormatException(line); } return v; } /** * Tests if exists an empty CoffeeMaker in a * LinkedList. * * @param list a LinkedList that contains objects of * the class CoffeeMaker * @return true if list contains a empty * CoffeeMaker object; * otherwise return false */ public static boolean existEmptyCoffeeMaker(LinkedList list) { for (ListIterator i = list.listIterator() ; i.hasNext(); ) { if (((CoffeeMaker) i.next()).isEmpty()) { return true; } } return false; } /** * Obtains the number of empty CoffeeMaker objects stored in a * LinkedList. * * @param list a LinkedList that contains objects of * the class CoffeeMaker * @return the number of empty CoffeeMaker objects with a * grade greater o equal than n */ public static int countEmptyCoffeeMakers(LinkedList list) { int count = 0; for (ListIterator i = list.listIterator() ; i.hasNext(); ) { if (((CoffeeMaker) i.next()).isEmpty()) { count++; } } return count; } /** * Fills each CoffeeMaker objects stored in a * LinkedList * * @param list a LinkedList that contains objects of * the class CoffeeMaker */ public static void fillCoffeeMakers(LinkedList list) { for (ListIterator i = list.listIterator() ; i.hasNext(); ) { ((CoffeeMaker) i.next()).fill(); } } /** * Obtains the sum of the quantity of coffee of the CoffeeMaker * objects stored in a LinkedList * * @param list a LinkedList that contains objects of * the class CoffeeMaker * @return a int with the sum of the quantity of coffee */ public static int sumQuantities(LinkedList list) { int sum = 0; for (ListIterator i = list.listIterator() ; i.hasNext(); ) { sum += ((CoffeeMaker) i.next()).getQuantity(); } return sum; } /** * Serves coffee of each CoffeeMaker object stored in a * LinkedList * * @param list a LinkedList that contains objects of * the class CoffeeMaker * @param qty a int with the quantity of coffee to be served * for each CoffeeMaker */ public static void serveCoffee(LinkedList list, int qty) { for (ListIterator i = list.listIterator() ; i.hasNext(); ) { ((CoffeeMaker) i.next()).serveACup(qty); } } /** * Serve a cup of coffee of a specific CoffeeMaker object * stored in a LinkedList * * @param list a LinkedList that contains objects of * the class CoffeeMaker * @param index a int that specify the location of the * CoffeeMaker object * @param qty a int with the quantity of coffee to be served * for the CoffeeMaker */ public static void serveCoffee(LinkedList list, int index, int qty) { ((CoffeeMaker) list.get(index)).serveACup(qty); } /** * Serve a cup of coffee of each CoffeeMaker stored in a * LinkedList that have a specific model * * @param list a LinkedList that contains objects of * the class CoffeeMaker * @param model a String that specify the model of the * CoffeeMaker object * @param qty a int with the quantity of coffee to be served * for each CoffeeMaker */ public static void serveCoffee(LinkedList list, String model, int qty) { CoffeeMaker cm; for (ListIterator i = list.listIterator() ; i.hasNext(); ) { cm = (CoffeeMaker) i.next(); if (model.equals(cm.getModel())) { cm.serveACup(qty); } } } /** * Removes from a LinkedList the empty CoffeeMaker * objects * * @param list a LinkedList that contains objects of * the class CoffeeMaker */ public static void removeEmptyCoffeeMakers(LinkedList list) { for (ListIterator i = list.listIterator() ; i.hasNext(); ) { if (((CoffeeMaker) i.next()).isEmpty()) { i.remove(); } } } /** * Display in a java.awt.TextArea component the information of * the CoffeeMaker objects contained in a * LinkedList *

    * This method calls the method {@link CoffeeMaker#toString()} to display * the information of each CoffeeMaker. *

    *

    * The information of each CoffeeMaker is displayed in a * diferent line. No extra lines and white space is displayed *

    * * @param list a LinkedList that contains objects of * the class CoffeeMaker * @param ta a java.awt.TextArea component where the * information will be displayed */ public static void displayInTextArea(LinkedList list, java.awt.TextArea ta) { int size = list.size(); if (size > 0) { ListIterator i = list.listIterator(); ta.setText(i.next().toString()); while(i.hasNext()) { ta.append("\n" + i.next().toString()); } } else { ta.setText(""); } } /** * Display in a java.awt.List component the information of the * CoffeeMaker objects contained in a LinkedList. *

    * The component List is cleared at the begining, so the * component lst has the same size that the list. *

    *

    * This method calls the method {@link CoffeeMaker#toString()} to display * the information of each CoffeeMaker. *

    * * @param linkedList a LinkedList that contains objects of * the class CoffeeMaker * @param lst a java.awt.List component where the elements * of list will be displayed */ public static void displayInList(LinkedList linkedList, java.awt.List lst) { lst.removeAll(); for (ListIterator i = linkedList.listIterator() ; i.hasNext(); ) { lst.add(i.next().toString()); } } /** * Obtains a sorted array with values of the field model of the * CoffeeMaker objects contained in the specified * LinkedList. *

    * This method calls the method {@link Arrays#sort(String[])} to sort the * array. *

    * * @param list a LinkedList that contains objects of * the class CoffeeMaker * @return the sorted array with the CoffeeMaker objects * contained in list. */ public static String[] getModels(LinkedList list) { String models[] = new String[list.size()]; int i = 0; for (ListIterator it = list.listIterator() ; it.hasNext();) { models[i++] = ((CoffeeMaker) it.next()).getModel(); } Arrays.sort(models); return models; } }

    Previou page Next page