The Java class below defines a queue data structure with a fixed capacity. public class FixedCapacityQueue {
Question:
The Java class below defines a queue data structure with a fixed capacity. public class FixedCapacityQueue { int[] mData = null; int mHead=0; // index of first full cell int mTail=0; // index of next empty cell public FixedCapacityQueue(int capacity) { mData = new int[capacity]; } public boolean enqueue(int x) { if (mTail==mData.length) return false; mData[mTail]=x; mTail++; return true; } public int dequeue() { if (mTail==mHead) return -1; int v=mData[mHead]; mHead++; return v; } } (a) Explain the risks posed by the lack of access modifiers specified on the state. Which access modifier is appropriate for these entities? [3 marks] (b) Discuss the choice to signal enqueue and dequeue errors using return values of false and -1, respectively. Suggest a better alternative and modify enqueue and dequeue to use it. [5 marks] (c) Explain how an enqueue operation on a FixedCapacityQueue object could fail with an error even when there are fewer items in the queue than the assigned capacity of the object. Show how to fix this. [3 marks] (d) Rewrite the class to use Java's Generics so that it can be used to represent queues of arbitrary objects (Integers, Strings, etc). Use the java.util.ArrayList class instead of int[] for the type of mData. [4 marks] (e) Explain why it was necessary to replace the int[] type of mData in part (d), and why ArrayList does not suffer from the same issue.
(a) Describe the four access regimes from public to private that may be applied to Java fields and methods. Why are they useful? [4 marks] (b) When you extend a class, the constructor for your new class will reference the constructor of the parent class, and this latter constructor may have any of the four possible access regimes. Comment on the consequences of each of the four possibilities. [4 marks] (c) If the only constructor for a class is marked as private, is it ever possible to have an instance of that class or any subclass of it? Explain why or why not.
Financial Management for Public Health and Not for Profit Organizations
ISBN: 978-0132805667
4th edition
Authors: Steven A. Finkler, Thad Calabrese