Question: 7.22 from A Crash Course in Java Turn the MessageQueue class of Chapter 3 into a generic class Queue import java.util.ArrayList; 2 3 /** 4

7.22 from A Crash Course in Java

Turn the MessageQueue class of Chapter 3 into a generic class Queue

import java.util.ArrayList; 2 3 /** 4 A first-in, first-out collection of messages. This 5 implementation is not very efficient. We will consider 6 a more efficient implementation in chapter 3. 7 */ 8 public class MessageQueue 9 { 10 /** 11 Constructs an empty message queue. 12 */ 13 public MessageQueue() 14 { Fall 2017 Students of Suneuy Kim 43 15 queue = new ArrayList<>(); 16 } 17 18 /** 19 Remove message at head. 20 @return message that has been removed from the queue 21 */ 22 public Message remove() 23 { 24 return queue.remove(0); 25 } 26 27 /** 28 Append message at tail. 29 @param newMessage the message to be appended 30 */ 31 public void add(Message newMessage) 32 { 33 queue.add(newMessage); 34 } 35 36 /** 37 Get the total number of messages in the queue. 38 @return the total number of messages in the queue 39 */ 40 public int size() 41 { 42 return queue.size(); 43 } 44 45 /** 46 Get message at head. 47 @return message that is at the head of the queue, or null 48 if the queue is empty 49 */ 50 public Message peek() 51 { 52 if (queue.size() == 0) return null; 53 else return queue.get(0); 54 } 55 56 private ArrayList queue; 57 }

Below are files given to complete this question

TestGenericQueue.java

public class TestGenericQueue {

public static void main(String[] args) {

GenericQueue queue = new

GenericQueue(10);

GenericQueue squeue = new

GenericQueue(10);

queue.add(1);

queue.add(2);

queue.add(3);

squeue.add("a");

squeue.add("b");

squeue.add("c");

System.out.println(queue);

System.out.println(squeue);

}

}

Output7.22.txt

[1 2 3]

[a b c]

A Crash Course in Java is the book this question is from

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!