All pastes #254204 Raw Edit

Anonymous

public text v1 · immutable
#254204 ·published 2006-11-21 16:47 UTC
rendered paste body
/*
Derik VanPelt
CS111
/home/dvanpelt/Unordered.java
Unordered.java
Lab Instructor: Dugald Brown
11/9/2006
*/

import java.util.Scanner;
import java.lang.reflect.Array;


public class Unordered<E>
{
  E[] array = (E[]) new Object[10];
  int maxSize = 10;
  int currentSize = 0;
 
  //default constructor
  public Unordered()
  {
  }
 
 
   //handles exception
  public static class EmptyArrayException extends Exception
  {  
   /*
   * EmptyArrayException
   * post - displays message if exception is thrown
   * @param msg - message to be displayed
   */
    public EmptyArrayException(String msg)
    {
      super(msg);
    }
  }
 
  //handles exception
  public static class FullArrayException extends Exception
  {  
   /*
   * FullArrayException
   * post - displays message if exception is thrown
   * @param msg - message to be displayed
   */
    public FullArrayException(String msg)
    {
      super(msg);
    }
  }
 
 
  /*
   * insert
   * pre -
   * post - an element is added to the array
   */
  public void insert(E o) throws FullArrayException
  {
    if (this.currentSize > 9)
      throw new FullArrayException("The Array is full");
    else
    {
      array[currentSize] = o;
      this.currentSize++;
    }
  }
 
 
 
  /*
   * remove
   * pre -
   * post - removes the element out of the array, then moves the last element in the array to the empty slot
   * @param o - element to be removed
   * @return - Returns the element that was removed
   */
  public E remove(E o) throws EmptyArrayException
  {
    int value = 99;
    if (this.currentSize < 1)
      throw new EmptyArrayException("The Array is Empty");
    else
    {
      for (int b = 0; b< this.currentSize; b++)
      {
        if (array[b] == o)
          value = b;
      }
      array[value] = array[this.currentSize-1];
      this.currentSize--;
    }
    return o;
  }
 
 
  /*
   * maxSize
   * pre -
   * post - the array is unchanged
   * @return - Returns the max size of the array
   */
  public int maxSize()
  {
    return maxSize;
  }
 
 
  /*
   * currentSize
   * pre -
   * post - the array is unchanged
   * @return - Returns the current size of the array
   */
  public int currentSize()
  {
    return this.currentSize;
  }
 
 
  /*
   * toString
   * pre -
   * post - the array is unchanged
   * @return - Displays the list of chores
   */
  public String toString()
  {
    String ret = "";
    for (int i = 0; i < this.currentSize; i++)
    {
      ret = ret + array[i] + "\n";
    }
    return (ret);
  }
 
 
  /*
   * isMember
   * pre -
   * post - the array is unchanged
   * @param o - Element to be checked if it is in the array
   * @return - Returns true is the element is in the array, false if not
   */
  public boolean isMember(E o)
  {
    int counter = 0;
    for (int j = 0; j< this.currentSize; j++)
    {
      if (array[j] == o)
        counter++;
    }
    if (counter > 0)
      return true;
    else
      return false;
  }
     
  /*
   * empty
   * pre -
   * post - the array is unchanged
   * @return - Returns true is array is empty, false if it isn't
   */
  public boolean empty()
  {
    if (this.currentSize == 0)
      return true;
    else
      return false;
  }
 
 
  /*
   * full
   * pre -
   * post - the array is unchanged
   * @return - Returns true is the array is full, false if not
   */
  public boolean full()
  {
    if (this.currentSize == 10)
      return true;
    else
      return false;
  }
 
 
  /*
   * pre -
   * post - This function asks the user to add and remove chores, will continue to run until
   * the unser finishes all of the chores
   */
  public static void main(String[] args) throws FullArrayException, EmptyArrayException
  {
    int choice;
    String chore1 = "wash dishes";
    String chore2 = "clean windows";
    String chore3 = "dust furnature";
    String chore4 = "Vaccum carpet";
    Unordered list = new Unordered();
    Scanner console = new Scanner(System.in);
    do
    {
      System.out.println("Would you like to add a new chore, or take one off? type 5 to add a chore and 6 to take one off");
      choice = console.nextInt();
      if (choice == 6)
      {
        System.out.println("Please enter a chore to remove (type an integer 1-4)");
        choice = console.nextInt();
        if (choice == 1)
        {
          if (list.isMember(chore1))
            list.remove(chore1);
        }
        if (choice == 2)
        {
          if (list.isMember(chore2))
            list.remove(chore2);
        }
        if (choice == 3)
        {
          if(list.isMember(chore3))
            list.remove(chore3);
        }
        if (choice == 4)
        {
          if(list.isMember(chore4))
            list.remove(chore4);
        }
      }
      if (choice == 5)
      {
        System.out.println("Please enter a chore to insert (type an integer 1-4)");
        choice = console.nextInt();
        if (choice == 1)
          list.insert(chore1);
        if (choice == 2)
          list.insert(chore2);
        if (choice == 3)
          list.insert(chore3);
        if (choice == 4)
          list.insert(chore4);
      }
      System.out.println(list.toString());
    }while (!list.empty());
    System.out.println("Congratulations, you have finished your chores!!!");
    
  }
}