Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Complete the implementation of the class ArrayList (a list of integers): Method

ID: 3747029 • Letter: C

Question

Complete the implementation of the class ArrayList (a list of integers):

Method signatures must be as shown

Make sure you do appropriate error checking, and throw exceptions on

error (and catch them! – see below)

Include an additional method: void show(), that will print the elements of

the list in brackets as follows: [ 23 42 14 33 ]

Create a main program (in yet another file) that does the following:

Construct an empty list.

Print the result of a call to isEmpty();

Add 23, 42, 14, and 33 (in that order) to an empty list using the

add(item) method.

Print the list using the show() method.

Add 99 at position 1 using the add(pos, item) method.

Print the list using the show() method.

Add 100 at position 5 using the add(pos, item) method. This should

place the 100 at the end of the list.

Print the list using the show() method.

Remove the element at position 2 using the remove(pos) method.

Print the list using the show() method.

Remove the element at position 0 using the remove(pos) method.

Print the list using the show() method.

Remove the element at position 3 using the remove(pos) method.

Print the list using the show() method.

Print the result of a call to contains(33);

Print the result of a call to contains(111);

Print the result of a call to size();

Print the result of a call to isEmpty();

Print the result of a call to get(2);

Call get(99): this should generate an exception and cause the program

to terminate with an error message

*

*

*

typedef int E;

class ArrayList

{

private:

E* data;

int num_items;

int capacity;

public:

ArrayList(int cap);

~ArrayList();

void add(E item);

void add(int pos, E item);

bool contains(E item);

int size();

bool isEmpty();

E get(int pos);

E remove(int pos);

void show();

};

//------------------------------------

// This should be in a cpp file

ArrayList::ArrayList(int cap) : capacity(cap), num_items(0) {

data = new E[capacity];

}

ArrayList::~ArrayList() {

delete data;

}

void ArrayList::add(E item) {

// code goes here

}

void ArrayList::add(int pos, E item) {

// code goes here

}

bool ArrayList::contains(E item) {

// code goes here

}

int ArrayList::size() {

// code goes here

}

bool ArrayList::isEmpty() {

// code goes here

}

E ArrayList::get(int pos) {

// code goes here

}

E ArrayList::remove(int pos) {

// code goes here

Explanation / Answer

Header:

#include <string>

using namespace std;

const int MAX_LIST = 10;

typedef string List_Item_type;

class Listt

{

public:

Listt();

bool is_Empty() const;

int get_Length() const;

void insertion(int index, const List_Item_type& newItem, bool& success);

void retriving(int index, List_Item_type& dataItem, bool & success) const;

void del(int index, bool& success);

private:

List_Item_type item[10];

int siz;

int translatee(int index) const;

};

Implementation:

#include "ArrayList.h" // header file

#include <iostream>

#include <fstream>

Listt::Listt() : siz(0)

{

}

bool Listt::is_Empty() const

{

return siz == 0;

}

int Listt::get_Length() const

{

return siz;

}

void Listt::insertion(int indx, const List_Item_type& newItem,

bool& successs)

{

successs = (indx >= 1) &&

(indx <= siz + 1) &&

(siz < MAX_LIST);

if (successs)

{

for (int position = siz; position >= indx; --position)

item[translatee(position + 1)] = item[translatee(position)];

item[translatee(indx)] = newItem;

++siz; // increase the siz of the list by one

}  

}  

void Listt::del(int indx, bool& successs)

{

successs = (indx >= 1) && (indx <= siz);

if (successs)

{

for (int fromPosition = indx + 1;

fromPosition <= siz;

++fromPosition)

item[translatee(fromPosition - 1)] = item[translatee(fromPosition)];

--siz; // decrease the siz of the list by one

} // end if

} // end del

void Listt::retriving(int indx, List_Item_type& dataItem,

bool& successs) const

{

successs = (indx >= 1) && (indx <= siz);

if (successs)

dataItem = item[translatee(indx)];

}

int Listt::translatee(int indx) const

{

return indx - 1;

}

int main()

{

int var1 = 1;

int Num_Of_items;

int n = 0;

int p = 0;

cout << "Please enter the number of data item:" << endl;

cin >> Num_Of_items;

cout << endl;

cout << "Please enter the data item, one per line:" << endl;

int listofitems[10];

//string mainlistitemptype = "int";

Listt myArrayList;

cout << myArrayList.get_Length() << endl;

if (myArrayList.is_Empty()) // tests before

{

cout << "This list is empty " << endl;

}

else

{

cout << "Listt is not empty! "<< endl;

}

bool mainsucc = false;

int main_array_list_size = myArrayList.get_Length();

for (int i = 0; i<Num_Of_items; i++)

{

cout << "Enter number " << i + 1 << " : " ;

cin >> listofitems[i];

myArrayList.insertion(listofitems[i], "int", mainsucc);

}

for (int i=0; i<main_array_list_size; i++)

{

cout << myArrayList.retriving(0, "int", mainsucc);

}

return 1;

}