You need to create a class Message that represents an email message. This class,
ID: 3819864 • Letter: Y
Question
You need to create a class Message that represents an email message. This class, for instance, can be used in implementation of a mail server (beyond the scope of this course).
Attributes and Methods
The class must declare the following attributes:
Sender (type String)
List of recipients (array of Strings)
Timestamp (type String)
and methods:
void setSender(String S)
String getSender()
void addRecipient(String S)
String[] getRecipients()
int getNumOfRecipients()
String getTimeStamp()
String toString()
Message()
Note: Last entry in the list represents default class constructor (it doesn't take any parameters)
Other Details
It is assumed that maximum number of recipients is set to 10 (hint: to be used as size of the array where you store recipients). Also it is assumed that timestamp must be evaluated at the time of object creation (functionality to be implemented in class constructor). Method toString() is expected to return String representation of the Message object, formatted as follows:
+--
| From: aaa@bbb.ccc
| To: bbb@ccc.ddd, www@rrr.ttt
| Date: Fri Apr 18 11:05:58 EDT 2014
+--
| Hello,
|
| This is a test message
| It has:
| - one sender (entry in the "From" field)
| - two recipients (multiple entries in the "To" filed, separated by commas)
| - timestamp (can be generated by (new java.util.Date()).toString())
| - and multi-line message body (this portion)
|
+--
Driver Program
You also need to create a driver program that illustrates creation of an object of class Message, populating such an object with exampled data (see above), and print out result of toString() method invocation (call).
Explanation / Answer
I HAVE DONE THIS IN ECLIPSE, I PUT SOME COMMENTS FOR BETTER UNDERSTANDING.
HERE IS THE CODE:
//Message.java
import java.util.Date;
public class Message {
// Three attributes as mentioned in the question
String sender;
String[] recipients=new String[10]; // Array with size 10
String timeStamp;
// Methods
public void setSender(String s)
{
sender=s; // setting sender with the passed value s
}
public String getSender()
{
return sender; // returning sender of this object
}
public void addRecipient(String s)
{
// To add recipients we must know number of recipients present in array
// so using getNumOfRecipients to determine
if(getNumOfRecipients()==10)
{
System.out.println("Exceeded maximum recipients 10"); // if recipients list has 10 elements
}
else
recipients[getNumOfRecipients()]=s; // else adding recipient
}
public String[] getRecipients() // returns the total recipients
{
return recipients;
}
public int getNumOfRecipients() // retrevs number of recipients present in array
{
int i=0;
while(recipients[i]!=null) // loop to check for null value from 0 index to 10 to determine no of recipients
{
i++;
}
return i;
}
public String getTimeStamp()
{
return timeStamp; //returning time stamp
}
public String toString() // toString method
{
String recip=""; // String for reciepents
int i=0;
while(recipients[i]!=null) // recipients are present in array so to retreve all using loop
{
if(i==1)
recip=recip+","+recipients[i]; // Gathering recipients
if(i==0)
recip=recipients[i];
i++;
}
// returning the format
return "+-- | From: "+getSender()+" | To: "+recip+" | Date: "+getTimeStamp()+" +-- | Hello, | This is a test message | It has:"
+ " | -one sender "+getSender()+" | -"+getNumOfRecipients()+" recipients "+recip+" | -"+getTimeStamp()+" |- multi line message body | +--";
}
public Message() // Constructor sets time stamp
{
timeStamp=new Date().toString(); // setting time stamp
}
}
//Driver.java
public class Driver {
public static void main(String[] args) { // Executtion starts here
// TODO code application logic here
Message object=new Message(); // creating object for the message class
object.setSender("samsmitail.com"); // setting sender first
object.addRecipient("selenagomezahoo.com"); // adding recipients here
object.addRecipient("justinmail.com"); // adding one more recipient
// Prints the format
System.out.println(object.toString()); // Printing to String of the object
}
}
OUTPUT:
NOTE:
Chegg is not allowing to post files with the contents of mails kind of things, so i couldn't post it. U LL get it.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.