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

(ii) Consider the following code fragment 3 of 4 public class ArrayStack impleme

ID: 3741120 • Letter: #

Question

(ii) Consider the following code fragment 3 of 4 public class ArrayStack implements Stack protected int capacity; protected static final int CAPACITY 1000;// default array capacity protected Object S[]; protected int top--1; // actual capacity of the stack array // array used to implement the stack // index for the top of the stack public ArrayStack0 // default constructor: creates stack with default capacity this(CAPACITY); public void push(Object element) if (isFull0) return top++; S[top] - element; Answer the following questions ArrayStack is a public class. What does this mean in terms of the overall visibility of the class? Why is this a good design decision? - All class variables are of type protected. What does this mean in terms of their overall visibility? ArrayStack stores objects of type Object rather than specific types like int or float. Why is this good idea? How is this design decision linked to the concept of an explicit cast as a means to store and retrieve any kind of object types on such a stack? (iii) What is meant by a self-referential structure type? Provide an example to illustrate your answer

Explanation / Answer

2.1.)ArrayStack is a public class and as we know this means that we can access this class from classes belonging to different packages,given that they, i.e, the class using it, should also be public. That is, its object can be created outside the class and friend class(classes in same package are friend classes), in other functions. For class visibility directly impacts its usability inside a program, and our program is to implement stack, much a data structure, and therefore should be accessible by all the functions in a program who wish to create a stack data structure type.

Making the class public we make this possible and save the hazard of writing the code to implement stack for every function using them.

Thus we make the code compact and less complex.

2. Protected members have a little less restrictedness on them, they allow special access permissions for subclasses.That is, these members,in addition to be be accessed from other classes on same package, can be accessed by the class' subclasses, even if they are on different packages.

3. A class holds different data type variables in it, thus when we instantiate a class, i.e, create an object, the object is made to be the cast that can make use of this class.Bcoz we have to create a stack here, which may be used contain many different data types, thus, declaring the object of any primitive data type will defeat the purpose of having a class. That's why objects of class are ade of type object and not any specified data type and this mskes sure that we can incorporate different data types in the same cast, that is, the object.

3.) A self referential structure is a structure that contains two parts, data, and a pointer that is used to point to another instance of the same structure. This makes a list comprising the different structure instances as its members and joined by the pointer data type. The data can be anything, int, string, float, and with the help of a pointer we keep referencing to the structure variables, thus making a list.

Eg: class list{

list (int value){data = value; }

list next;

int data;

}