JAVA Programming Design a class Message that models an email

JAVA Programming

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. A constructor that takes the sender and recipient

b. A method append that appends a line of text to the message body

c. A method toString that makes the message into one long string like this: “From: Fname Lname\ To: Some Recipient\ …”

Write a driver program that uses this class to make a message and print it.

Solution

MessageTest.java

import java.util.Scanner;


public class MessageTest {

  
   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       System.out.println(\"Enter Recipient Name: \");
       String recipientName = scan.nextLine();
       System.out.println(\"Enter Sender Name: \");
       String senderName = scan.nextLine();
       System.out.println(\"Enter the body text: \");
       String text = scan.nextLine();
      
       Message m = new Message(recipientName, senderName);
       m.append(text);
       System.out.println(m.toString());

   }

}

Message.java


public class Message {
   private String recipient;
   private String sender;
   private String text;
   public Message(String r, String s){
       this.recipient = r;
       this.sender = s;
       text = \"\";
   }
   public void append(String t){
       text = text + t +\"\ \";
   }
   public String toString(){
       return \"From: \"+sender+\"\ \ \"+text+\"\ To: \"+recipient+\"\ \";
   }
}

JAVA Programming 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

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site