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

Object-Oriented Programming - C++ and Java - Comparative Study Construct an arra

ID: 3567008 • Letter: O

Question

Object-Oriented Programming - C++ and Java - Comparative Study

Construct an array manipulation class named arrayPlay in C++ with following features:

1. The private data members of your class be an array, its size, and other members according to your need.

2. Two constructors:

a. A default constructor with a default size (e.g. 15).

b. The second constructor will take an array as argument as initial values. You can also define its capacity for your private array. If you need other parameters, you can also pass so.

3. The following accessor methods

a. capacity () - return its capacity

b. isEmpty () - return true if there is no item in the array, false otherwise

c. noOfItems () - total number of items in the array

d. printArray () - print out all array items

4. The following operations

a. add ( ...) - if there is free space, add the item (passed through parameter) at the end of the array.

b. add ( ...) - it will take an array as a parameter and add the items at the end of the private array of your class; if there is not enough space for all items, you will add as many items as you can.

c. sort () - sort the array.

d. find (...) - search an item in your array, return index if success, -1 otherwise

e. deleteItem ( ...) - delete the item if present.

An example class:
class arrayPlay{
private:
int *a;
int size;
int last;
public:
arrayPlay(

Explanation / Answer

// C++ CODE STARTS

#include <iostream>

using namespace std;

class arrayPlay{
    private:
        int *a;
        int size;
        int lastIndex;
    public:
        arrayPlay(int initialCapacity){
            size = initialCapacity;
            a = new int[size];
            lastIndex = -1;
        }
        arrayPlay(int passedArray[], int length){
            size = length;
            a = new int[size];
            lastIndex = -1;
            int i;
            for(i=0;i<size;i++){
                a[i] = passedArray[i];
                lastIndex++;
            }
        }
        int capacity(){
            // Current capacity to which it can hold further
            return (size-1)-lastIndex;
        }
        bool isEmpty(){
            return (lastIndex==-1);
        }
        int noOfItems(){
            return lastIndex+1;
        }
        void add(int item){
            if(capacity()>0){
                lastIndex++;
                a[lastIndex] = item;
            }else{
                // Do nothing
            }
        }
        void add(int item[], int itemSize){
            int currentCapacity = capacity();
            if(currentCapacity == 0) return;
            if(currentCapacity>=itemSize){
                int i;
                for(i=0;i<itemSize;i++){
                    lastIndex++;
                    a[lastIndex] = item[i];
                }
            }else{
                int i;
                for(i=0;i<currentCapacity;i++){
                    lastIndex++;
                    a[lastIndex] = item[i];
                }
            }
        }
        void printArray(){
            int i;
            cout << "Printing ARRAY from top to bottom" << endl;
            for(i=0;i<=lastIndex;i++){
                cout << a[i] << endl;
            }
            cout << "ARRAY bottom reached" << endl;
        }
        void sort(){
            int historyVar;
            int outer;
            int inner;
            // Bubble Sort for ascending order
            for (outer=0; outer<lastIndex; outer++){
                for (inner=0 ; inner<lastIndex-outer; inner++){
                    if (a[inner] > a[inner+1]){
                        historyVar = a[inner];
                        a[inner] = a[inner+1];
                        a[inner+1] = historyVar;
                    }
                }
            }
        }
        int find(int x){
            int i;
            for(i=0;i<=lastIndex;i++){
                if(x == a[i]) return i;
            }
            return -1;
        }
        void deleteItem(int x){
            int index = find(x);
            if(index>-1){
                int i;
                for(i=index;i<lastIndex;i++){
                    a[i] = a[i+1];
                }
                lastIndex--;
            }else{
                // Do nothing
            }
        }
};

int main()
{
    int myArray[] = {4,3,6,100,3,6,82};
    arrayPlay test = arrayPlay(myArray, 7);

    test.printArray();
    cout << test.capacity() <<endl;
    cout << test.isEmpty() <<endl;
    cout << test.noOfItems() <<endl;
    cout << test.find(100) << endl;

    test.sort();

    test.printArray();
    cout << test.capacity() <<endl;
    cout << test.isEmpty() <<endl;
    cout << test.noOfItems() <<endl;
    cout << test.find(100) << endl;

    test.deleteItem(100);

    test.printArray();
    cout << test.capacity() <<endl;
    cout << test.isEmpty() <<endl;
    cout << test.noOfItems() <<endl;
    cout << test.find(100) << endl;

    test.deleteItem(3);
    test.deleteItem(6);
    int newArray[] = {8200, 7300, 934};
    test.add(newArray, 3);

    test.printArray();
    cout << test.capacity() <<endl;
    cout << test.isEmpty() <<endl;
    cout << test.noOfItems() <<endl;
    cout << test.find(8200) << endl;

    return 0;
}

// C++ CODE ENDS

// Java CODE STARTS

public class AjavaClass {

   public static void main(String[] args) {
       // TODO Auto-generated method stub
       int myArray[] = { 4, 3, 6, 100, 3, 6, 82 };
       arrayPlay test = new arrayPlay(myArray, 7);

       test.printArray();
       System.out.println(test.capacity());
       System.out.println(test.isEmpty());
       System.out.println(test.noOfItems());
       System.out.println(test.find(100));

       test.sort();

       test.printArray();
       System.out.println(test.capacity());
       System.out.println(test.isEmpty());
       System.out.println(test.noOfItems());
       System.out.println(test.find(100));

       test.deleteItem(100);

       test.printArray();
       System.out.println(test.capacity());
       System.out.println(test.isEmpty());
       System.out.println(test.noOfItems());
       System.out.println(test.find(100));

       test.deleteItem(3);
       test.deleteItem(6);
       int newArray[] = { 8200, 7300, 934 };
       test.add(newArray, 3);

       test.printArray();
       System.out.println(test.capacity());
       System.out.println(test.isEmpty());
       System.out.println(test.noOfItems());
       System.out.println(test.find(8200));
   }

}

class arrayPlay {
   private int a[];
   private int size;
   private int lastIndex;

   public arrayPlay(int initialCapacity) {
       size = initialCapacity;
       a = new int[size];
       lastIndex = -1;
   }

   public arrayPlay(int passedArray[], int length) {
       size = length;
       a = new int[size];
       lastIndex = -1;
       int i;
       for (i = 0; i < size; i++) {
           a[i] = passedArray[i];
           lastIndex++;
       }
   }

   public int capacity() {
       // Current capacity to which it can hold further
       return (size - 1) - lastIndex;
   }

   public boolean isEmpty() {
       return (lastIndex == -1);
   }

   public int noOfItems() {
       return lastIndex + 1;
   }

   public void add(int item) {
       if (capacity() > 0) {
           lastIndex++;
           a[lastIndex] = item;
       } else {
           // Do nothing
       }
   }

   public void add(int item[], int itemSize) {
       int currentCapacity = capacity();
       if (currentCapacity == 0)
           return;
       if (currentCapacity >= itemSize) {
           int i;
           for (i = 0; i < itemSize; i++) {
               lastIndex++;
               a[lastIndex] = item[i];
           }
       } else {
           int i;
           for (i = 0; i < currentCapacity; i++) {
               lastIndex++;
               a[lastIndex] = item[i];
           }
       }
   }

   public void printArray() {
       int i;
       System.out.println("Printing ARRAY from top to bottom");
       for (i = 0; i <= lastIndex; i++) {
           System.out.println(a[i]);
       }
       System.out.println("ARRAY bottom reached");
   }

   public void sort() {
       int historyVar;
       int outer;
       int inner;
       // Bubble Sort for ascending order
       for (outer = 0; outer < lastIndex; outer++) {
           for (inner = 0; inner < lastIndex - outer; inner++) {
               if (a[inner] > a[inner + 1]) {
                   historyVar = a[inner];
                   a[inner] = a[inner + 1];
                   a[inner + 1] = historyVar;
               }
           }
       }
   }

   public int find(int x) {
       int i;
       for (i = 0; i <= lastIndex; i++) {
           if (x == a[i])
               return i;
       }
       return -1;
   }

   public void deleteItem(int x) {
       int index = find(x);
       if (index > -1) {
           int i;
           for (i = index; i < lastIndex; i++) {
               a[i] = a[i + 1];
           }
           lastIndex--;
       } else {
           // Do nothing
       }
   }
}

// Java CODE ENDS