In JAVA, we are to create a class A uml diagram that helps with the Java coding
ID: 644152 • Letter: I
Question
In JAVA, we are to create a class
A uml diagram that helps with the Java coding process:
Write your own version of a generic class named MyQueue that will create a static queue of any data type. Submit only the class itself, do not include a driver program. I will import your class into my own driver program and test it myself as part of grading. Be sure to include the following methods: ??Constructor ? accepts an integer argument specifying the length of the queue. ??enqueue ? Accepts a value to put in the queue. Throws an exception if the queue is full. ?-.dequeue ? Returns a value from the queue. Throws an exception if the queue is empty. ??isFull ? Returns true if the queue is full, false otherwise. ??isEmpty - Returns true if the queue is empty, false otherwise. ??toString ? Returns a string representation of the queue. You may not use the Java API queue container(java.util.Queue). For the exception, Just throw an Exception, i.e., new Exception(Something)Explanation / Answer
public class MyQueue<E> implements Queue<E> {
private Object[] container;
private int startOfQueueIndex;
private int numberOfElements;
public MyQueue(int size) {
container = new Object(size);
startOfQueueIndex = 0;
numberOfElements = 0;
}
public boolean enqueue(E e) {
if (container.IsFull()) throw new java.lang.IllegalStateException: Queue full();
else
container[(startOfQueueIndex + numberOfElements++) %container .length] = e;
}
public E deqeue() {
if (container.IsEmpty())
throw new new java.lang.IllegalStateException: Queue empty();
else
E contain = (E)container[startOfQueueIndex];
startOfQueueIndex = (startOfQueueIndex + 1) %container .length;
return contain ;
}
public boolean IsFull()
{
if(container.length==size)
return 1;
else
return 0;
}
public boolean IsEmpty()
{
if(container.length==0)
return 1;
else
return 0;
}
String toString(E q)
{
String str=q.toString();
return str;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.