For this project you are to implement a generic dynamic array class. It will be
ID: 3784419 • Letter: F
Question
For this project you are to implement a generic dynamic array class. It will be a templated class that provides the basic features of an array.
/**************************************************************************
* .cpp file*
**************************************************************************/
#include "dynarr.h"
#include <iostream>
#include <string>
int doubleIt(dynarr<int> A)
{
int newsize = 2*A.getCapacity();
A.reserve(newsize);
return newsize;
}
int main()
{
dynarr<int> D(15);
std::cout << "The capacity of D is " << D.getCapacity() << std::endl;
dynarr<int> E(D);
std::cout << "The capacity of E is " << E.getCapacity() << std::endl;
D.reserve(25);
std::cout << "The capacity of D is now " << D.getCapacity() << std::endl;
E.reserve(2*E.getCapacity());
std::cout << "The capacity of E is now " << E.getCapacity() << std::endl;
D[0] = 11;
std::cout << "D[0] = " << D[0] << std::endl;
try
{
std::cout << D[-1] << std::endl;
}
catch(InvalidIndex &ex)
{
std::cout << ex.getMessage() << std::endl;
}
try
{
std::cout << D[30] << std::endl;
}
catch(InvalidIndex &ex)
{
std::cout << ex.getMessage() << std::endl;
}
return 0;
}
/************************************
* .h file (should be completed) *
*************************************/
#include <cassert>
#include <iostream>
#include <string>
class RuntimeException // generic run-time exception
{
protected:
std::string errorMsg;
public:
RuntimeException(const std::string& err) { errorMsg = err; }
std::string getMessage() const { return errorMsg; }
};
class InvalidIndex : public RuntimeException
{
public:
InvalidIndex(const std::string& err): RuntimeException(err) {};
};
template
class dynarr {
private:
int capacity;
dynElem *A;
public:
dynarr() : capacity(0), A(NULL);
dynarr(int N): capacity(N), A(new dynElem[N]){};
dynarr(const dynarr &other);
~dynarr();
dynarr & operator=( const dynarr &other);
dynElem & operator[](int ndx) throw(InvalidIndex);
int getCapacity();
void reserve(int newcap);
// if newcap <= capacity, does nothing;
// if capacity is 0, allocates a dynamic array of
// capacity newcap and makes A point to that array;
// otherwise allocates a new dynamic array newA of capacity
// newcap, copies values in A to newA, deletes A and sets
// A equal to newA
};
template
dynarr::dynarr(const dynarr &other)
{
}
template
dynarr::~dynarr()
{
}
template
dynarr & dynarr::operator=( const dynarr &other)
{
}
template
dynElem & dynarr::operator[](int ndx) throw(InvalidIndex)
{
}
template
int dynarr::getCapacity()
{
return capacity;
}
template
void dynarr::reserve(int newcap)
{
}
/**************
* output: *
**************/
The correct outputis shown below:
The capacity of D is 15
The capacity of E is 15
The capacity of D is now 25
The capacity of E is now 30
D[0] = 11
Array index is negative
Array index is too large
Explanation / Answer
public class GenSet { private E[] a; public GenSet(Class c, int s) { // Use Array native method to create array // of a type only known at run time @SuppressWarnings("unchecked") final E[] a = (E[]) Array.newInstance(c, s); this.a = a; } E get(int i) { return a[i]; } } import java.lang.reflect.Array; public class GenSet { private E[] a; public GenSet(Class clazz, int length) { a = clazz.cast(Array.newInstance(clazz.getComponentType(), length)); } public static void main(String[] args) { GenSet foo = new GenSet(String[].class, 1); String[] bar = foo.a; foo.a[0] = "xyzzy"; String baz = foo.a[0]; } }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.