Using a class to find errors in data structures

55 views Asked by At

I need to use the class ReturnObjectImpl to basically find errors when I add an element to the ArrayList data structure class (this is a university assignment).

I am not sure how I get my functions in the ArrayList class to return ReturnObject. I need some way of passing everything through to ReturnObject, checking if there is an error (which I am not sure how to do either) and then providing either an error message or the object.

public interface ReturnObject {
    /**
     * Returns whether there has been an error
     * @return whether there has been an error
     */
    public boolean hasError();

    /**
     * Returns the error message. 
     * 
     * This method must return NO_ERROR if and only if
     * {@hasError} returns false.
     * 
     * @return the error message
     */
    public ErrorMessage getError(); //Changes the return to a String has in the interface ErrorMessage is throwing an error - if marks are deducted, please discuss with me

    /**
     * Returns the object wrapped in this ReturnObject, i.e. the
     * result of the operation if it was successful, or null if
     * there has been an error.
     * 
     * Note that the output of this method must be null if {@see
     * hasError} returns true, but the opposite is not true: if
     * {@see hasError} returns false, this method may or may not
     * return null.
     * 
     * @return the return value from the method or null if there has been an error
     */
    public Object getReturnValue();


}

And then there is the class itself (which in currently incomplete):

public class ReturnObjectImpl implements ReturnObject {

    // Constructor for successful operation
        ReturnObjectImpl (Object c){
            if (!hasError()){
                getReturnValue(c);
            }
        }

        // Constructor for error
        ReturnObjectImpl (){
            if (hasError()){
            //  getError();
            }
        }


    public boolean hasError() {
        //returns true or false depending on if there is an error

        return null;
    }


    public ErrorMessage getError() { //Changes the return to a String has in the interface ErrorMessage is throwing an error - if marks are deducted, please discuss with me
        //returns the error message is hasError == true or NO_ERROR if hasError() returns false

        return ErrorMessage;
    }



    public Object getReturnValue() {
        // TODO Auto-generated method stub
        return null;
    }

}

And finally the ArrayList class

public class ArrayList implements List{
    public static final int CAPACITY=16;
    private int size = 0;
    private Object[] data;


    //constructors
    public ArrayList() {
         data = new Object[CAPACITY];
        }                             //Constructs arraylist with default capacity
    public ArrayList(int capacity) { // Constructs arraylist with given capacity
        data = new Object[capacity];
        System.out.println("Created an ArrayList of capacity " + capacity);
    }



    public boolean isEmpty(){
        return (size == 0);
    }

    public int size(){
        System.out.println("The ArrayList is not full, but currently has " + size + " indexs");
        return size;
    }

    public ReturnObject get(int index){
        return null; //INCOMPLETE

    }

    public ReturnObject remove(int index){
        return null;

    }

    public ReturnObject add(int index, Object item){
        if(index <= size && index < data.length){
            for (int x = size-1; x >= index; x--){
                data[x+1] = data[x];
                size++; 
            }
            data[index] = item;
            System.out.println("Added to array at " + index);
        }
        return null;

    }

    public ReturnObject add(Object item){
        if (data[0] == null){
            data[0] = item;
        } 
        //int adding = size + 1;
        data[size] = item;
        System.out.println("Added item to index " + size);
        size++;
        return null;
    }
    //added - but DELETE BEFORE SUBMITTING
    public void printAll(){
        for(int x = 0; x < data.length; x++){
            System.out.println(data[x]);
        }
    }


}

In short, I have two issues: 1. What the functions for error checking should look like in returnObjectImpl 2. More significantly, how I supposed to send the results of, say, public ReturnObject add(Object item) from ArrayList class to ReturnObjectImpl.

1

There are 1 answers

1
Stephen C On

If List is the java.util.List interface, you have some serious mistakes here:

  1. List is a generic interface, but your ArrayList is not generic. (If you were coding this for Java 1.4.x, that would be OK. But Java 1.4.x was retired > 10 years ago!)

  2. Your ArrayList methods are incompatible with the java.util.List API. For example, add should return a boolean, get should return the element type (or Object if you are ignoring generics) not some other type.

If not ... then calling the interface List is a bad idea. Ditto for ArrayList. You shouldn't "borrow" the names of standard classes and interfaces like this. It will get you into trouble.


To answer your questions:

1) What the functions for error checking should look like in returnObjectImpl.

There is no returnObjectImpl method. And the error checking does not belong in the ReturnObjectImpl class. That class (per its name and API design) is simply a "holder" that represents either a value or an error condition. The actual error checking code belongs in your array list class; e.g. some thing like this.

if (/* some error */) {
    return new ReturnObjectImpl(/* some error message */);
}

2) More significantly, how I supposed to send the results of, say, public ReturnObject add(Object item) from ArrayList class to ReturnObjectImpl.

if (/* not an error */) {
    return new ReturnObjectImpl(/* the value */);
}

Obviously you are going to have to redesign the ReturnObjectImpl constructors to make that work.


Opinion: I think someone may have gotten into the "don't use exceptions" mindset that afflicts1 some people who have learned to program in (say) C or C++. Without getting into the debate over whether exceptions are "good" or "bad", the fact remains that they are an integral part of the Java language, and they are used consistently throughout the the Java language and class libraries. You cannot avoid them, and you will hurt yourself if you try.

If this was your design, and you really want to avoid exceptions this much ... you should be using a different programming language2.


1 - That isn't intended to be perjorative, but I'm afraid that "avoid exceptions" thinking rarely (if ever) gives a good outcome in terms of Java productivity or maintainability.

2 - I'm reminded of the line "Real programmers can write FORTRAN in any language".