Question: /** attempting to implement a dynamic array Todo: 1) get an element at any given valid index 2) complete resize method as designed 3) handle

/** attempting to implement a dynamic array Todo: 1) get an element at any given valid index 2) complete resize method as designed 3) handle exception 4) make sure to document your code appropriately

*/

class DA{ private int[] buffer; private int size; private final int EXTRA = 5;

/** */ public DA(int size){ this.size = size; this.buffer = new int[this.size + EXTRA]; } /** */ public int length(){ return this.size; }

//max capacity before reallocation public int maxLength(){ return this.buffer.length; }

//setter public void put(int value, int loc){ /* if(loc < 0) throw new Exception("Invalid index"); */ if(loc >= this.buffer.length) resize(loc+1); this.buffer[loc] = value; if(loc > this.size && loc < this.buffer.length) this.size = loc+1; }

/** resize method */ public void resize(int size){ /* size < this.size size > this.size size >= this.buffer.length */ } }

/** testing Dynamic array */

public class DA_Driver{ public static void main(String[] args){ DA a = new DA(20); System.out.println("my array length: " + a.length()); System.out.println("my array max length: " + a.maxLength()); a.put(5, 21); System.out.println("my array length: " + a.length()); System.out.println("my array max length: " + a.maxLength()); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!