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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started