Results 1 to 2 of 2
  1. #1
    fleg is offline Member
    Join Date
    Oct 2010
    Posts
    12
    Rep Power
    0

    Default Some Exceptions issues

    Hello i should write a program which simulates a desk and you are putting workItems on it (array have to be used) and there should be used some Exceptions with which i cant move... if someone would be so kind and help me with it

    <code>
    public interface WorkStack
    {

    /**
    * Checks whether the stack is empty.
    *
    * @return true if the stack is empty,
    * false otherwise
    */
    boolean isEmpty();

    /**
    * Inserts a new work item to the stack.
    *
    * @param item work item which will be inserted to the stack
    * @return true if the item is successfully inserted,
    * false otherwise (e.g. if the stack is full)
    * @throws NullPointerException if the parameter item is null.
    */
    boolean push(WorkItem item);

    /**
    * It retrieves and removes the first item (top item) of the stack.
    * Returns null if the stack is empty.
    *
    * @return the top item of the stack,
    * or null if the stack is empty.
    */
    WorkItem pop();

    /**
    * It returns a position of the given work item in "to do list".
    * When the work item is on the top of the stack the position is 1,
    * when the work item is on the bottom of the stack, the position is number of items in the stack.
    *
    * @param item a work item
    * @return position of the work item
    * @throws NullPointerException if the parameter item is null.
    * @throws IllegalArgumentException if the stack does not contain the given work item.
    */
    int getItemPosition(WorkItem item);
    }
    </code>

    <code>
    public class Table implements WorkStack
    {
    private WorkItem toDoList[];
    private int stack;
    private WorkItem item;
    public Table (int stack){

    if (stack<0) {
    throw new IllegalArgumentException("Cant be such a tiny table");
    }

    this.stack=stack;


    }

    public boolean isEmpty(){
    return (toDoList[0]==null);
    }

    public boolean push(WorkItem item){
    if (toDoList.length<=stack){
    toDoList[toDoList.length+1]=item;
    return true;
    }else{
    return false;
    }
    }

    public WorkItem pop(){
    if (isEmpty()){
    return null;
    }else{
    return toDoList[0];
    }
    }

    public int getItemPosition(WorkItem item){
    for(int i=0;i<toDoList.length;i++){
    try { //these 2 exceptions are pain in the ass
    if (equals(item)){
    return i+1;
    }else{

    return null;

    }
    }catch (NullPointerException npe){
    System.err.println("Here should be something written");
    }


    }
    }
    }
    </code>

    and it would be nice if you check that code above if there arent some bullshits... this is the last part

    <code>
    public class WorkItem
    {

    private String name;

    /**
    * It creates a new work item with the specified name.
    *
    * @param name name of the work item
    * @throws NullPointerException if the name is null
    */
    public WorkItem(String name) {
    if (name == null) {
    throw new NullPointerException("The name of the work item can't be set to NULL");
    }
    this.name = name;
    }

    /**
    * It returns true if the given object is instance of this class
    * and has the same name.
    *
    * @param object
    * @return true if the given object equals to this one
    * false otherwise
    */
    public boolean equals(Object o) {
    if (o == null) return false;
    if (!(o instanceof WorkItem)) return false;
    WorkItem item = (WorkItem) o;
    return item.getName().equals(getName());
    }

    /**
    * It returns the hashcode of the name of the work item
    *
    * @return hash code
    */
    public int hashCode() {
    return getName().hashCode();
    }

    /**
    * @return name of the work item
    */
    public String getName() {
    return name;
    }

    @Override
    public String toString() {
    return getName();
    }
    }
    </code>

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,185
    Rep Power
    20

    Default

    The brackets around code should be [] not <>.

    What is your problem?

Similar Threads

  1. Exceptions
    By Nerijus in forum New To Java
    Replies: 8
    Last Post: 05-18-2010, 02:44 PM
  2. Exceptions & More
    By besweeet in forum New To Java
    Replies: 12
    Last Post: 04-29-2010, 10:06 PM
  3. Exceptions
    By hedonist in forum New To Java
    Replies: 10
    Last Post: 09-08-2009, 09:38 AM
  4. Exceptions in thread
    By Goodwater in forum New To Java
    Replies: 4
    Last Post: 03-16-2009, 04:00 AM
  5. Need Help With Exceptions
    By maggie_2 in forum New To Java
    Replies: 5
    Last Post: 12-15-2008, 08:12 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •