Write And Test An Array Class [DynamicArray.h and DynamicArray.TestDriver.cpp] W
ID: 3798005 • Letter: W
Question
Write And Test An Array Class [DynamicArray.h and DynamicArray.TestDriver.cpp] Write and test a data structures template. The resulting template can be used in any program in place of a C++ array. Requirements. Develop DynamicArray.h as you write DynamicArray.TestDriver.cpp with class DynamicArray. defined and fully tested. Write the public interface exactly as specified below -- do not add to, or change the public interface as specified. Write the template for an array of capacity =2 (default constructor) of unspecified type. Include a public square bracket getter and setter pair, both with index range-checking, returning whatever value you wish if out of range. But apply capacity auto-adjusting for the setter if out of range high. Include a public getter named DynamicArray::capacity() to return the data structure's now-variable capacity Include a public setter named DynamicArray::capacity(int) to change the capacity. Do tests with int, double, or char. Also do tests with an object, like string. Note that there is no good reason to copy the "dummy" value in the dynamic memory management functions, so don't include it in your testing of const object copy or object assignment.Explanation / Answer
import java.util.Scanner;
import java.util.ArrayList;
class DynamicArray
{
private ArrayList<String> al;
public DynamicArray()
{
al = new ArrayList<String>();
}
public void clear()
{
al.clear();
}
public int size()
{
return al.size();
}
public void insert(String key)
{
al.add(key);
}
public String get(int index)
{
if (index >= al.size())
return "";
return al.get(index);
}
public void remove(int index)
{
if (index >= al.size())
return ;
al.remove(index);
}
public void remove(String key)
{
al.remove(key);
}
public void display()
{
System.out.println(" Dynamic Array : "+ al);
System.out.println();
}
}
public class DynamicArrayTest
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Dynamic Array Test ");
DynamicArray da = new DynamicArray();
char ch;
do
{
System.out.println(" Dynamic Array ");
System.out.println("1. insert ");
System.out.println("2. remove by index");
System.out.println("3. remove by val");
System.out.println("4. clear");
System.out.println("5. size");
int choice = scan.nextInt();
switch (choice)
{
case 1 :
System.out.println("Enter value to insert");
da.insert(scan.next() );
break;
case 2 :
System.out.println("Enter index");
da.remove(scan.nextInt() );
break;
case 3 :
System.out.println("Enter value");
da.remove(scan.next() );
break;
case 4 :
System.out.println(" Dynamic Array Cleared");
da.clear();
break;
case 5 :
System.out.println(" Size = "+ da.size() );
break;
default :
System.out.println("Wrong Entry ");
break;
}
da.display();
System.out.println(" Do you want to continue (Type y or n) ");
ch = scan.next().charAt(0);
} while (ch == 'Y'|| ch == 'y');
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.