Hello fellow members,

I have recently started to study Java, but the lecturer went through basics very fast and now I'm having problems with starting to write a class.

I have an idea of how to write the methods, but how to declare the array properly at the start to suit this program is a mystery for me. Can somebody please explain this to me.

I basically need help with the code before the methods like size() and insertAtRank(). Any help/advise is very welcome!

I have the following assignment:

In this second part of the worksheet, you will be expected to write Java code that corresponds to the array based Vector and link based List implementation strategies discussed in the course. Much of this work will require that you transform pseudo code given in the course into Java code.

1)	ArrayVector: Create a new Java class called ArrayVector that implements the Vector interface and realises the array-based Vector implementation strategy. In accordance with previous worksheets, implement a toString() method to help you to debug / visualise the operation of the class.

The Java Vector interface and the RankOutOfBoundsException code are provided via CSI Moodle.

And the interface given is this:

/**
 * This is the interface of the Vector ADT as is outlined in the course notes
 *
 * @author Rem Collier
 */
public interface Vector {
	
	public int size();
	
	public boolean isEmpty();

	public Object elemAtRank(int rank) throws RankOutOfBoundsException;
	
    public Object replaceAtRank(int rank, Object element) throws RankOutOfBoundsException;
    
    public void insertAtRank(int rank, Object element) throws RankOutOfBoundsException;
	
    public Object removeAtRank(int rank) throws RankOutOfBoundsException;
}

Dani AI

Generated

For : the array-based Vector needs two simple pieces of state — an array to hold elements and an int to track how many are in use. Initialize the array in a constructor (choose a small default capacity) and grow it when inserting beyond capacity. The rest is bookkeeping: check ranks, shift elements with System.arraycopy, and keep the logical size correct. As suggested, if the sticking point is “how to declare an array as a variable,” the skeleton below shows the usual pattern.

public class ArrayVector implements Vector {
    private Object[] elements;
    private int size;
    private static final int DEFAULT_CAPACITY = 10;

    public ArrayVector() {
        elements = new Object[DEFAULT_CAPACITY];
        size = 0;
    }

    private void ensureCapacity(int minCapacity) {
        if (minCapacity > elements.length) {
            int newCap = Math.max(elements.length * 2, minCapacity);
            Object[] newArr = new Object[newCap];
            System.arraycopy(elements, 0, newArr, 0, size);
            elements = newArr;
        }
    }

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

    /* insertAtRank, removeAtRank, elemAtRank, replaceAtRank follow the same pattern:
       - check the correct rank range (insert allows 0..size, access/remove allow 0..size-1)
       - call ensureCapacity before shifting on insert
       - use System.arraycopy to shift blocks
       - on remove, set the freed slot to null and decrement size
    */
}

Notes and common pitfalls: use two separate checks — one for access/removal (rank < 0 || rank >= size) and one for insertion (rank < 0 || rank > size). Forgetting to set the emptied slot to null leaves objects referenced and prevents GC. Off-by-one errors when inserting at the end are common; treating capacity versus logical size correctly prevents ArrayIndexOutOfBoundsException. Add a simple toString that iterates 0..size-1 for debugging. If generics are introduced later, switch to a typed array with the standard generic-array creation workaround.

I'm not entirely sure what you are talking about. what exactly are you stuck with, what have you done so far?
as I understand it, you are having trouble to declare an array as a variable?

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.