I'm totally lost. I have a programming partner and this is what he sent me. I can't get the program to compile. I don't know where to start. Any suggested place to start?

package Assignment2;

public class TooBigException extends Exception 
    {
    private static final long serialVersionUID = 1L;

    public TooBigException(String mess)
        {
        super(mess);
        }   
    }



package Assignment2;

import java.util.Random;

public class ListOfInts 
    {
    protected int[] data;
    public ListOfInts(int[] data) 
        {
    if (data.length <= 0)
            {
            throw new IllegalArgumentException("this is an error called by error to small");
            }
    this.data = data;
        } // constructor

    public ListOfInts(int n, int max) 
        {
        if (n<=0) 
            {
        throw new IllegalArgumentException("this is an error called by error to small");
            }
        data = new int[n];
        Random prng = new Random();
        for (int index = 0; index < n; index++)
            data[index] = prng.nextInt(max);
        } // constructor

    protected ListOfInts() 
        {
           // placeholder for subclasses
            // we'll discuss this in class
        }  // no-arg constructor

    public void set(int index, int val) 
        {
        data[index] = val;
        } // set()

    public int get(int index) 
        {
        return data[index];
        } // get()

        // Test driver
    public static void main(String[] args) 
        {
        final int n = 1;
        ListOfInts aList = new ListOfInts(n, 10);
        for (int index = 0; index < n; index++)
          System.out.println(aList.get(index));
        System.out.println("Element 3: " + aList.get(3));
        aList.set(3, -aList.get(3));
        System.out.println("Element 3 negated: " + aList.get(3));
        } // main()
    } // class ListOfInts

Recommended Answers

All 5 Replies

Any suggested place to start?

Post the full text of the error messages so we can see what the problems are.

>  ----jGRASP exec: javac -g Assignment2\ExpandableList.java
> 
> ExpandableList.java:46: error: unreported exception TooBigException; must be caught or declared to be thrown
>       expand();
>             ^
> ExpandableList.java:78: error: unreported exception TooBigException; must be caught or declared to be thrown
>           expand();
>                 ^
> 2 errors
> 
>  ----jGRASP exec: javac -g Assignment2\Homework02.java
> 
> Homework02.java:65: error: exception TooBigException is never thrown in body of corresponding try statement
>       catch (TooBigException t)
>       ^
> 1 error
> 

Daniweb's not allowing me to post the entire code here so he's a link so you can read it all together. Thanks!

Click Here to see the entire code. I accidentally didn't post the entire code and it won't let me post it again. >_<

Thanks! Btw NormR1 I remember you helped me with my last homework. Thanks to you and a few others, I was able to apply the theory you brokedown and I passed my test...I think. I'll find out tomorrow. lol I felt confident though. Thanks again.

Where is the expand() method defined and called in the code you posted?
Are the error messages you posted for some other program?

Try posting the code again in a new post.

package Assignment2;

import java.util.Random;

public class ListOfInts 
    {
    protected int[] data;
    public ListOfInts(int[] data) 
        {
    if (data.length <= 0)
            {
            throw new IllegalArgumentException("this is an error called by error to small");
            }
    this.data = data;
        } // constructor

    public ListOfInts(int n, int max) 
        {
        if (n<=0) 
            {
        throw new IllegalArgumentException("this is an error called by error to small");
            }
        data = new int[n];
        Random prng = new Random();
        for (int index = 0; index < n; index++)
            data[index] = prng.nextInt(max);
        } // constructor

    protected ListOfInts() 
        {
           // placeholder for subclasses
            // we'll discuss this in class
        }  // no-arg constructor

    public void set(int index, int val) 
        {
        data[index] = val;
        } // set()

    public int get(int index) 
        {
        return data[index];
        } // get()

        // Test driver
    public static void main(String[] args) 
        {
        final int n = 1;
        ListOfInts aList = new ListOfInts(n, 10);
        for (int index = 0; index < n; index++)
          System.out.println(aList.get(index));
        System.out.println("Element 3: " + aList.get(3));
        aList.set(3, -aList.get(3));
        System.out.println("Element 3 negated: " + aList.get(3));
        } // main()
    } // class ListOfInts

/****************************************************************************************/
package Assignment2;

public class ExpandableList extends ListOfInts 
    {
    protected int size;
    public final int DEFAULT_CAPACITY = 16;

    public ExpandableList(int[] data) 
        {
        super(data);
        size = data.length;
        } // constructor

    public ExpandableList(int capacity, int max) 
        {
        super(capacity, max);
        size = capacity;
        } // constructor

    public ExpandableList(int capacity) 
        {
        super();
        if (capacity <=0)
            {
            throw new IllegalArgumentException("This is an error for being too small");
            }   
        size = 0;           // space for capacity, but nothing there
        data = new int[capacity];
        } // constructor

    public ExpandableList() 
        {
    // If the client doesn't specify an initial size, default to
    // DEFAULT_CAPACITY.
        this(16);
        } // ExpandableList()

    public int size() 
        {
        return size;
        } // size()

    public void add(int val) 
        {
        if (size >= data.length)
      expand();
        data[size++] = val;
        } // add()

    protected void expand() throws TooBigException
        {
    // The array is too small to add to. Replace it with a bigger one.
        int capacity = 1 + 3 * data.length / 2;
        if (capacity <=6) 
            {
            TooBigException t = new TooBigException("capacity is too big");
            throw t;  
            }
        int[] newData = new int[capacity];


    // Copy the old values to the new array
        for (int index = 0; index < data.length; index++)
        newData[index] = data[index];

    // Replace the old array with the new one
        data = newData;
        } // expand()


  // Issue: ListOfInts has get() and set() methods. What if we try to
  // get or set an array element past the end of an expandable list?

    public void set(int index, int val) 
        {
        while (index >= data.length) 
            {
        expand();
            } // while
        super.set(index, val);
        if (index >= size)
        size = index + 1;
        } // set()

    public int get(int index) 
        {
        if (index >= size) 
            {
        System.out.println("Attempt to access element " + index + " of a list of size " + size);
          Error e = new Error("Attempt to access element " + index + " of a list of size " + size);
        System.exit(1);
        throw e;
        } // if
        return super.get(index);
        } // get() 



  // Test driver
    public static void main(String[] args) 
        {
        ExpandableList aList = new ExpandableList(1);
        for (int index = 0; index < 10; index++)
        aList.add(index);
        for (int index = 0; index < 10; index++)
        System.out.println(aList.get(index));

        aList = new ExpandableList(2);
        aList.set(3, 3);
        aList.set(9, 9);
        for (int index = 0; index < aList.size(); ++index)
        System.out.println(index + ": " + aList.get(index));

        // Should abort:
        System.out.println(aList.get(10));
        } // main()

} // class ExpandableList

/****************************************************************************************/
package Assignment2;

public class Homework02 
    {
    private void run() 
        {
    // Create one IterableExpandableList that contains the integers 1
    // through 9. 
        IterableExpandableList first = new IterableExpandableList(1);
        for (int index = 1; index < 10; index++)
        first.add(index);

    // Create a second IterableExpandableList by iterating over the
    // first and, for each integer in the first list, insert three times
    // that integer in the second. 
        IterableExpandableList second = new IterableExpandableList(first.size());
        for (Integer value: first)
        second.add(3 * value);

    // Call the ... method [ displayList() ] twice to display both of
    // your lists on two lines. 
        displayList(first);
        displayList(second);
        } // run()

  // Write a method that receives an IterableExpandableList and displays
  // all the elements of the the list on a single line separated by
  // commas. 
    private static void displayList(IterableExpandableList aList) 
        {
        boolean first = true;
        for (Integer value: aList) 
            {
        if (!first) 
                System.out.print(", ");
        else
                first = false;
        System.out.print(value);
            } // for-each
        System.out.println();
        } // displayList()

  // Your main() method cannot have any code except to create an object
  // and call a single method within that object. Thus main() will
  // contain exactly two statements.

  // Other than main() and perhaps the method you write to display a
  // list, nothing in your class can be static. 

    public static void main(String[] args) 
        {
        try
            {
            Homework02 solution = new Homework02();
            solution.run();
            }
        catch (IllegalArgumentException l)
            {
            System.err.print("IllegalArgumentException:" + l);
            }
        catch (Error e)
            {
            System.err.print("Error:" + e);
            }
        catch (TooBigException t)
            {
            System.err.print("TooBigException: + t");
            }
        }

        {

        } // main()

    }// class Homework01

/****************************************************************************************/
package Assignment2;

import java.util.Iterator;

public class IterableExpandableList extends ExpandableList implements Iterable<Integer>
    {
    public IterableExpandableList(int[] data) 
        {
        super(data);
        } // constructor

    public IterableExpandableList(int n, int max) 
        {
        super(n, max);
        } // constructor

    public IterableExpandableList(int n) 
        {
        super(n);
        } // constructor

    public Iterator<Integer> iterator() 
        {
        return new ExpandableListIterator(data, size);
        } // iterator()

    class ExpandableListIterator implements Iterator<Integer> 
        {
        private int[] data;
        private int   current;
        private int   size;

        ExpandableListIterator(int[] data, int size) 
            {
        if (size > data.length) 
                {
                System.err.println("Inconsistent request. The array length (" + data.length + ") shorter than claimed size (" + size + ").");
                System.exit(1); // error exit
            } // if bad
        current = 0;
        this.size = size;
        this.data = data;
            } // constructor

        public boolean hasNext() 
            {
        return (current < size);
            } // hasNext() 

        public Integer next() 
            {
        return data[current++];
            } // next()

        public void remove() 
            {
        System.err.println("remove() not implemented");
        System.exit(2);
            } // remove()
        } // class ExpandableListIterator


  // Test driver
    public static void main(String[] args) 
        {
        IterableExpandableList aList = new IterableExpandableList(2);
        for (int index = 0; index < 10; index++)
        aList.add(index);
        for (int item: aList)
        System.out.println(item);

        aList = new IterableExpandableList(2);
        aList.set(3, 3);
        aList.set(9, 9);

        for (int index = 0; index < aList.size(); ++index)
            System.out.println(index + ": " + aList.get(index));
        System.out.println();
        int[] evens = {0, 2, 4, 6, 8, 10};
        aList = new IterableExpandableList(evens);
        for (int item: aList)
        System.out.println(item);
        // Should abort:
        System.out.println(aList.get(10));
        } // main()
    } // class IterableExpandableList
/****************************************************************************************/
package Assignment2;

public class TooBigException extends Exception 
    {
    private static final long serialVersionUID = 1L;

    public TooBigException(String mess)
        {
        super(mess);
        }   
    }

/****************************************************************************************/

unreported exception TooBigException; must be caught or declared to be thrown

When a method is defined to throw an exception (see expand()) the call to the method must
either be inside a try catch block for that exception
or the method the call is inside of must be defined to throw that exception.

See the tutorial: http://docs.oracle.com/javase/tutorial/essential/exceptions/index.html

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.