Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Design a class Mailbox that stores e-mail messages, using the Message class of L

ID: 3886336 • Letter: D

Question

Design a class Mailbox that stores e-mail messages, using the Message class of Lab 5.1. Implement the following methods:

• public void addMessage(Message m)
• public int getNumberOfMessages()
• public Message getMessage(int position)
• public void removeMessage(int position)


Copy the highlighted code below to your main method (replace code from Lab 5.1) to test your Mailbox class:

public static void main(String[] args)
{
    Mailbox box = new Mailbox(); // empty mailbox
    
    Message msg = new Message("Harry Morgan", "Rudolf Reindeer");
    msg.append("Hello, how are you?");
    msg.append("Are you working on the assignment yet?");
    msg.append("If so, can we meet this weekend to discuss the assignment?");
    msg.append("Thanks for the help");
    box.addMessage(msg);
    
    msg = new Message("Harry Morgan", "Stella Clause");
    msg.append("It's getting closer to the holidays");
    msg.append("Want to do some shopping next weekend?");
    msg.append("Let me know");
    box.addMessage(msg);
    
    msg = new Message("Harry Morgan", "Esther Bunny");
    msg.append("I'm out of eggs, and the stores are closed");
    msg.append("Do you have any I can borrow?");
    msg.append("Thanks a lot!");
    box.addMessage(msg);
    
    int m = box.getNumberOfMessages();
    for (int count=1; count<=m; count++)
    {
        Message M = box.getMessage(count);
        System.out.println(M.toString());
    }
    /*
        Expected output:
        
            From: Harry Morgan
            To: Rudolf Reindeer
            
            Hello, how are you?
            Are you working on the assignment yet?
            If so, can we meet this weekend to discuss the assignment?
            Thanks for the help
  
  
            From: Harry Morgan
            To: Stella Clause
            
            It's getting closer to the holidays
            Want to do some shopping next weekend?
            Let me know
  
  
            From: Harry Morgan
            To: Esther Bunny
            
                I'm out of eggs, and the stores are closed
                Do you have any I can borrow?
                Thanks a lot!
  
  
    */
    
    box.removeMessage(2); // remove second message added
    
    m = box.getNumberOfMessages();
    for (int count=1; count<=m; count++)
    {
        Message M = box.getMessage(count);
        System.out.println(M.toString());
    }
    /*
        Expected output:
        
            From: Harry Morgan
            To: Rudolf Reindeer
            
                Hello, how are you?
                Are you working on the assignment yet?
                If so, can we meet this weekend to discuss the assignment?
                Thanks for the help
  
  
            From: Harry Morgan
            To: Esther Bunny
            
                I'm out of eggs, and the stores are closed
                Do you have any I can borrow?
                Thanks a lot!
  
  
    */
} // end main method

Explanation / Answer

please find the code

mailbox class

import java.util.ArrayList;

public class Mailbox {

ArrayList<Message> mailbox;

public Mailbox(){

mailbox = new ArrayList<Message>();

}

public void addMessage(Message m) {

mailbox.add(m);

}

public int getNumberOfMessages() {

return mailbox.size();

}

public Message getMessage(int position) {

return mailbox.get(position);

}

public void removeMessage(int position) {

mailbox.remove(position);

}

}

class to test the mail box

public class TestMailBox {

public static void main(String args[]) {

Mailbox box = new Mailbox(); // empty mailbox

Message msg = new Message("Harry Morgan", "Rudolf Reindeer");

msg.append("Hello, how are you?");

msg.append("Are you working on the assignment yet?");

msg.append("If so, can we meet this weekend to discuss the assignment?");

msg.append("Thanks for the help");

box.addMessage(msg);

msg = new Message("Harry Morgan", "Stella Clause");

msg.append("It's getting closer to the holidays");

msg.append("Want to do some shopping next weekend?");

msg.append("Let me know");

box.addMessage(msg);

msg = new Message("Harry Morgan", "Esther Bunny");

msg.append("I'm out of eggs, and the stores are closed");

msg.append("Do you have any I can borrow?");

msg.append("Thanks a lot!");

box.addMessage(msg);

int m = box.getNumberOfMessages();

for (int count=0; count<m; count++)

{

Message M = box.getMessage(count);

System.out.println(M.toString());

}

/*

Expected output:

From: Harry Morgan

To: Rudolf Reindeer

Hello, how are you?

Are you working on the assignment yet?

If so, can we meet this weekend to discuss the assignment?

Thanks for the help

From: Harry Morgan

To: Stella Clause

It's getting closer to the holidays

Want to do some shopping next weekend?

Let me know

From: Harry Morgan

To: Esther Bunny

I'm out of eggs, and the stores are closed

Do you have any I can borrow?

Thanks a lot!

*/

box.removeMessage(2); // remove second message added

m = box.getNumberOfMessages();

for (int count=0; count<m; count++)

{

Message M = box.getMessage(count);

System.out.println(M.toString());

}

/*

Expected output:

From: Harry Morgan

To: Rudolf Reindeer

Hello, how are you?

Are you working on the assignment yet?

If so, can we meet this weekend to discuss the assignment?

Thanks for the help

From: Harry Morgan

To: Esther Bunny

I'm out of eggs, and the stores are closed

Do you have any I can borrow?

Thanks a lot!

*/

} // end main method

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote