Q2 *************Base_A11Q2.java***************** Q2 [SB1: Create a \"message buf
ID: 3776853 • Letter: Q
Question
Q2
*************Base_A11Q2.java*****************
Q2 [SB1: Create a "message buffer" that manages and incoming phone calls and messages in an answering machine. Implement it using the LinkedList API. (Do not import any other packages.) See Base A11Q2.java for a starting place. In particular, see the MessageBufferADT interface which contains a description of the meth required in the message buffer. Create a Message class that implements Comparable and contains the name of the caller, the date they called, the time, and the length of the call in seconds (as an integer. Make getters and a compare lo method that passes in another Message and compares the length of the messages. This method should compare the length of the two messages and return -1 if the first message is shorter than the second message, 0 if the length of the messages is equal, and 1 if the first message is longer than the second message All phone messages are added into the message buffer. The highest priority message in a message buffer is the message with the shortest length. When removing messages, your message buffer should return the highest priority message. Messages should be removed from the buffer when the phone call is returned. The attached file already contains a test main that creates three Message objects, adds them to a message buffer, and removes/displays them one by one. 125 points Sample output: output SER200 HW11 Message: HName: Steve Date: 09/20/2016 Time 11:45PM Length: 60 Message: IName: Joseph Date: 09/05/2016 Time 35 PM Length: 120 Message IName: Bill Date: 08/07/2016 Time 8:08AM Length: 1601Explanation / Answer
package com.mb.test;
import java.util.ArrayList;
import java.util.List;
public class Base_A11Q2 {
static class Message implements Comparable {
public String name;
public String date;
public String time;
public int lenOftheCallInSeconds;
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Message [name=" + name + ", date=" + date + ", time=" + time
+ ", lenOftheCallInSeconds=" + lenOftheCallInSeconds + "] ";
}
public Message(String name, String date, String time,
int lenOftheCallInSeconds) {
this.name = name;
this.date = date;
this.time = time;
this.lenOftheCallInSeconds = lenOftheCallInSeconds;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the date
*/
public String getDate() {
return date;
}
/**
* @param date the date to set
*/
public void setDate(String date) {
this.date = date;
}
/**
* @return the time
*/
public String getTime() {
return time;
}
/**
* @param time the time to set
*/
public void setTime(String time) {
this.time = time;
}
/**
* @return the lenOftheCallInSeconds
*/
public int getLenOftheCallInSeconds() {
return lenOftheCallInSeconds;
}
/**
* @param lenOftheCallInSeconds the lenOftheCallInSeconds to set
*/
public void setLenOftheCallInSeconds(int lenOftheCallInSeconds) {
this.lenOftheCallInSeconds = lenOftheCallInSeconds;
}
@Override
public int compareTo(Object o) {
Message other=(Message) o;
if(this.lenOftheCallInSeconds==other.lenOftheCallInSeconds)
{
return 0;
}if(this.lenOftheCallInSeconds>other.lenOftheCallInSeconds)
return 1;
else
return -1;
}
}
static interface MessageBufferADT {
/**
* Adds one message to the buffer.
* @param m the message to be added to the buffer
*/
public void add(Message m);
/**
* Returns true if this buffer contains no elements.
* @return true if this buffer is empty
*/
public boolean isEmpty();
/**
* Returns the number of elements in this buffer.
* @return the integer representation of the size of the buffer
*/
public int size();
}
static class MessageBuffer implements MessageBufferADT {
List<Message> list=new ArrayList<Message>();
@Override
public void add(Message m) {
list.add(m);
}
@Override
public boolean isEmpty() {
if(list.isEmpty()==true)
{
return true;
}
return false;
}
@Override
public int size() {
return list.size();
}
}
public static void main(String[] args) {
MessageBuffer messageBuffer = new MessageBuffer();
Message m3 = new Message("Bill", "08/07/2016", "8:08AM", 160);
Message m2 = new Message("Joseph", "09/05/2016", "2:35PM", 120);
Message m1 = new Message("Steve", "09/20/2016", "11:45PM", 60);
messageBuffer.add(m1);
messageBuffer.add(m2);
messageBuffer.add(m3);
System.out.println(messageBuffer.list);
}
}
output
[Message [name=Steve, date=09/20/2016, time=11:45PM, lenOftheCallInSeconds=60]
, Message [name=Joseph, date=09/05/2016, time=2:35PM, lenOftheCallInSeconds=120]
, Message [name=Bill, date=08/07/2016, time=8:08AM, lenOftheCallInSeconds=160]
]
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.