Using Python 3.5: 2. The following problems relate to an email messaging system.
ID: 3767142 • Letter: U
Question
Using Python 3.5:
2. The following problems relate to an email messaging system. Work all problems in the same file.:
(a) Design a class Message that models an e-mail message. A message has a recipient, a sender, and a message text. Support the following methods:
- A constructor that takes the sender and recipient
- A method append that appends a line of text to the message body
- A method toString that makes the message into one long string like this (notice that each line is in a new line):
"From: Harry Morgan To: Rudolf Reindeer . . ."
(b) Design a class Mailbox that stores e-mail messages, using the Message class of Exercise 2a. Implement the following methods:
- def addMessage(self, message)
- def getMessage(self, index)
- def removeMessage(self, index)
(c) Write a program that uses the Message and Mailbox classes from problems 2a and 2b to make a message, print it and store it in a Mailbox.
Explanation / Answer
package emailmodel; 02 03 import java.util.Scanner; 04 public static void main (String[] args) 05 { 06 public class Message 07 { 08 private String senderemail; 09 private String receiveremail; 10 11 //The constructor method 12 public Message(String sender, String receiver) 13 { 14 senderemail = sender; 15 receiveremail = receiver; 16 } 17 //Method to display the sender/receiver 18 public void displayMessageDeatails() 19 { 20 System.out.println("To"+ receiveremail +" "); 21 System.out.println("From"+ senderemail + " "); 22 } 23 public class MessageExample//Call method to constructor 24 { 25 public void main(String[] args) 26 { 27 Message email = new Message(receiveremail, senderemail); 28 email.displayMessageDeatails(); 29 } 30 } 31 /** 32 * Adds a sender email to the message 33 * @param senderemail the sender email 34 */ 35 public void addsenderemail() 36 { 37 System.out.print("Please enter sender:"); 38 Scanner se = new Scanner(System.in); 39 senderemail = se.nextLine(); 40 se.close(); 41 } 42 /** 43 * Adds a receiver email to the message 44 * @param receiveremail the receiver email 45 */ 46 public void addreceiveremail() 47 { 48 System.out.print("Please enter receiver:"); 49 Scanner re = new Scanner(System.in); 50 receiveremail = re.nextLine(); 51 re.close(); 52 } 53 /** 54 * Gets the sender email in the current email 55 * @return the senderemail 56 */ 57 public String getsenderemail() 58 { 59 return senderemail; 60 } 61 /** 62 * Gets the receiver email in the current email 63 * @return the receiveremail 64 */ 65 public String getreceiveremail() 66 { 67 return receiveremail; 68 } 69 70 public class Append 71 { 72 int messagebody; 73 public void main(String args[]) 74 { 75 //Appending text of message body 76 System.out.print("Please enter message body:"); 77 Scanner in = new Scanner(System.in); 78 messagebody = in.nextInt(); 79 //Appended message 80 String s; 81 StringBuffer sb = new StringBuffer(40); 82 s = sb.append(messagebody).toString(); 83 System.out.println(s + " "); 84 in.close(); 85 } 86 } 87 }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.