COP 3530 Section U03 Spring 2017 HOMEWORK # 1 ============ Due: Wednesday, February 1, 2017 You are given the interface Queue shown below. public interface Queue { // add an item void add(AnyType item); // remove an item, throw NoSuchElementException if the queue is empty AnyType remove(); // get the first element without removing it // throw NoSuchElementException if the queue is empty AnyType getFirst(); // get the size int getSize(); // check if the queue is empty boolean isEmpty(); // empty the queue void clear(); } Write a generic class CircularQueue that implements this interface as a circular queue. The class must have 3 private fields, the data array, and the indices f and r that point to the first and the last element of the queue, in this order. You may use an extra field, the size of the queue. The class has 2 constructors, one that takes as input the size of data, and one that uses a default size, in this case 3. Add checks that there is space in data, i.e. size < data.length. If not, add creates a new array with twice as many elements as data, copies the contents of data starting with location f and ending with r, onto the new array, starting with location 0. Then, add performs the insertion at the end of the queue. If data is empty before the insertion, initialize f and r. Recall that in a circular queue, both the front and the rear wrap arround the end of the data array. remove() also checks that the queue has 1 item. If that is the case, return the item and empty the queue. You must also write the public method public void display() that displays f, r, the contents of data and the size of the queue. Use the test below to check your program. public static void main(String[] args) { System.out.println("Testing Queues"); System.out.println("==============\n\n\n"); System.out.println("We form a queue with the names Papa Bill, Mark," + " Ali Baba."); CircularQueue profs = new CircularQueue(); profs.add("Papa Bill"); profs.add("Mark"); profs.add("Ali Baba"); System.out.println("We add the names Shrek, Jeff the Chef, Hex"); profs.add("Shrek"); profs.add("Jeff the Chef"); profs.add("Hex"); System.out.println("The queue has " + profs.getSize() + " persons."); System.out.println("We delete 4 names."); for (int i = 1; i <= 4; i++) System.out.println("The delete name is " + profs.remove()); System.out.println("The name on top is " + profs.getFirst()); System.out.println("We delete 2 more names."); for (int i = 1; i <= 2; i++) System.out.println("The delete name is " + profs.remove()); System.out.println("Now, isEmpty() is " + profs.isEmpty()); System.out.println("We try to remove a name."); try { String name = profs.remove(); } catch (NoSuchElementException e) { System.out.println("We get a NoSuchElementException"); } } // end main The output must be the one beneath. Testing Queues ============== We form a queue with the names Papa Bill, Mark, Ali Baba. We add the names Shrek, Jeff the Chef, Hex The queue has 6 persons. We delete 4 names. The delete name is Papa Bill The delete name is Mark The delete name is Ali Baba The delete name is Shrek The name on top is Jeff the Chef We delete 2 more names. The delete name is Jeff the Chef The delete name is Hex Now, isEmpty() is true We try to remove a name. We get a NoSuchElementException Some Style Guidelines --------------------- Start the program with the author block below. /* COURSE : COP 3530 * Section : U03 * Semester : Spring 2017 * Instructor : Alex Pelin * Author : (your name) * Assignment #: 1 * Due date : February 1, 2017 * Description : Insert a 2-3 line description of the assignment * * */ Recall that the names of classes and of the variables are nouns, and the names of the methods are verbs. The variables and the methods use only lower case letters except for the first letter of the words of a compound word. The constants have only capital letters and the words are separated by underscores. Put header comments to describe what the method does and end comments to describe the variables. You may use Eclipse or Netbeans to develop your program. Email the program(s) to pelina@cis.fiu.edu by the due date. Send the program(s) as a text file. You may insert it in the text of the email message or put it an attachment, but not send me zipped files or Word files.