Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Container.java contains the following code: public abstract class Container { // do not change the value of the following constant. protected final int ORIGINAL_SIZE =

image text in transcribedimage text in transcribedimage text in transcribedContainer.java contains the following code:

public abstract class Container {

// do not change the value of the following constant.

protected final int ORIGINAL_SIZE = 10;

protected Object[] list; // is a container that stores the element of MyList

protected Object[] set; // is a container that stores the element of MySet

protected int size; // this variable holds the actual number of elements that are stored in either of the containers (i.e. MyList or MySet).

/**

* This method adds the obj to the end of the container.

* @param obj is the object that is added to the container.

*/

void add(Object obj) {

System.out.println("The object was added to the contianer");

}

/**

* This method removes the object at the given index

* @param index is the index of the object that is removed.

* @return returns the removed object.

*/

Object remove(int index) {

// Change the return value.

return new Object();

}

/**

* This method returns true if the container is empty.

* @return It returns true if the container is empty, otherwise false.

*/

boolean isEmpty() {

return size == 0 ? true: false;

}

/**

* This method returns the number of elements stored in the container.

* @return It returns the number of elements in the container.

*/

int getSize() {

return size;

}

}

/**

*

* This class simulates an ArrayList, where you can add unlimited number of

* elements to the list.

*

*/

class MyList extends Container{

/**

* This is the default constructor that sets all the instance variables to their defualt value.

*/

public MyList () {

list= new Object[ORIGINAL_SIZE];

size = 0;

}

/**

* This method returns the element that is stored at index index .

* @param index is the index at which the element is accessed and returned.

* @return it returns the element stored at the given index .

*/

public Object get(int index) {

Object obj = list[index];

return obj;

}

/**

* This method overrides the add method defined in class container, by

* adding the obj to the back of list array.

* The original size of the array , is defined by ORIGINAL_SIZE , however, it is possible that

* more elements is added to this array. In case the array does not have enough capacity to add one more element, it grows itself

* by doubling the size of list array.

*/

@Override

void add(Object obj) {

if (size

list[size] = obj;

}

else {

Object[] arrayCopy = new Object[2*list.length];

int i;

for(i = 0; i

arrayCopy[i] = list[i];

}

arrayCopy[i] = obj;

list = arrayCopy;

}

size++;

}

/**

* This method removes the object at the given index.

* items at index + 1, index + 2, ... should be shifted to index, index + 1, ....

* @pre index is in a valid range.

*/

@Override

Object remove(int index) {

// implement this code. You many need to change the return value

return new Object();

}

/**

* This method returns the elements of the MyList in a form of

* [obj1 obj2 obj3 ...]

*/

@Override

public String toString() {

String result= "[";

for(int i = 0; i

result += list[i] + " ";

result = result.trim() + "]";

return result;

}

}

class MySet extends Container{

public MySet() {

set = new Object[ORIGINAL_SIZE];

size = 0;

}

/**

* This method overrides the add method defined in class container, by

* adding the obj to the back of set array.

* The original size of the set , is defined by ORIGINAL_SIZE , however, it is possible that

* more elements is added to this set. In case the set does not have enough capacity to add one more element, it grows itself

* by doubling the size of set array.

*/

@Override

void add(Object obj) {

// if the array is full, double its size

if (size == set.length ) {

Object[] setCopy = new Object[2*set.length];

for(int i = 0; i

setCopy[i] = set[i];

}

set = setCopy;

}

// check if the obj is already exists

boolean found = false;

for (int i = 0; i

if (set[i].equals(obj)) {

found = true;

break;

}

}

// if the obj does not exists, add it to the set

if (!found) {

set[size] = obj;

size++;

}

}

/**

* This method is an optional method, which means it has not been defiend for this class.

* @param index

* @return

*/

@Override

Object remove(int index) {

// Please do not change this code.

throw new UnsupportedOperationException();

}

/**

* This method returns the elements of the MySet in a form of

* [obj1 obj2 obj3 ...]

*/

@Override

public String toString() {

String result= "[";

for(int i = 0; i

result += set[i] + " ";

result = result.trim() + "]";

return result;

}

}

/**

* This class implements a Stack.

*

*/

class Stack{

private Container stack;

// no other instance variable should be defined here.

/**

* This is the constructor that initializes stack .

*/

public Stack() {

// insert your code here. only one line should be added here.

}

/**

* This method adds the object to the top of the stack.

* @param object is the object that is added to the top of the stack.

*/

public void push(Object object) {

// insert your code here. only one line should be added here.

}

/**

* This method removes an object from the top of the stack.

* @return It returns the object that is returned from the top of the stack.

*/

public Object pop() {

// insert your code here. only one line should be added here. You may need to change the return value.

return new Object();

}

/**

* This method returns the object, which is at the top of the stack without removing it from the stack.

* @return It returns the element, which is found on top of the stack.

*/

public Object top() {

// insert your code here. only one line should be added here. You may want to change the return value.

return new Object();

}

/**

* This method shows how many elements are in the stack.

* @return It returns the number of elements in the stack.

*/

public int getSize() {

// change the return value. No any other code should be added.

return 0;

}

}

A container is a storage that can store an unlimited number of objects. There is a special type of a container that is called a stack. A stack follows a LIFO (Last In, First Out) rule to insert or remove to/from the stack. This means that the last item that is added to the stack, will be the first that is removed from the stack. You can imagine a stack as a plate dispenser. When you add a plate, you add it to the top of the dispenser. When you remove a plate, you remove it from the top again. A stack has two main and two auxiliary methods: push (object): This method adds the object to the stack. pop (): removes the last element that was added, from the stack. top(): returns the element, which is at the top of the stack, without removing it. getSize(): returns the number of elements in the stack. Your job for this lab, is to complete Container, MyList and Stack class. Part of the container implementation has been provided. Therefore, you do not need to repeat what you have written for the previous lab. I have written a comment, where you need to insert your code. Please do not change the part of the code that is implemented for you. For example, one thing that you should not touch is the definition of the Container class. Container is defined as an abstract class, which means it cannot be instantiated. At this point you do not need to worry about what an abstract class is, as we will discuss it later in the lecture. All you need to know is that an abstract class cannot be instantiated. Therefore, you have no choice except using polymorphism, to initialize stack instance variable. Task 1: Class MyList Implement method remove () that gets an index and removes the element at that index. Task 2: Class Container Implement remove () method for class Container. This method should correctly get its subclass's remove () method to run. Please note that you should only write one line of code. Task 3: Class Stack Read the documentation of this class and implement all the methods. Please note that you should not add any instance variables

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

2. What items are typically included in the job description?

Answered: 1 week ago