Embed presentation
Download to read offline
![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;](https://image.slidesharecdn.com/javaprogrammingdesignaclassmessagethatmodelsane-mailmessa-230706013525-5e202d33/75/JAVA-ProgrammingDesign-a-class-Message-that-models-an-e-mail-messa-pdf-1-2048.jpg)

The document outlines the design of a Java class named 'Message' that models an email, including attributes for recipient, sender, and message text. It specifies methods for constructing a message, appending text, and converting the message into a formatted string. A driver program is included that demonstrates the functionality of the Message class by taking user input and displaying the final message.
![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;](https://image.slidesharecdn.com/javaprogrammingdesignaclassmessagethatmodelsane-mailmessa-230706013525-5e202d33/75/JAVA-ProgrammingDesign-a-class-Message-that-models-an-e-mail-messa-pdf-1-2048.jpg)
