THE MESSAGE CLASS IS BELOW AND WORKS JUST FINE, thank you. import java.util.*; p
ID: 3684552 • Letter: T
Question
THE MESSAGE CLASS IS BELOW AND WORKS JUST FINE, thank you.
import java.util.*;
public class MessageTeaserP8_21
{
//Declare main function
public static void main (String[] args)
{
Scanner input = new Scanner(System.in);
String sender, recipient;
// Read the inputs
System.out.println("Enter the name of the Sender: ");
sender = input.nextLine();
System.out.println("Enter the name of the Recipient: ");
recipient = input.nextLine();
Message m1 = new Message(sender, recipient);
m1.setMessage();
System.out.println(m1);
}
// Now lets declare the constructor
public static class Message
{
private String sender;
private String recipient;
private String message;
public Message (String s, String r)
{
sender = s;
recipient = r;
}
public void setMessage()
{
Scanner input = new Scanner(System.in);
System.out.println("What do you want to put in the Message?: ");
message = input.nextLine();
}
// Declare a method to read the message
public String toString()
{
// return " Sender:" + sender + "Recipient: " + recipient + "Message:" + message + " ";
return " Sender: " + sender + " Recipient: " + recipient + " Message: " + message;
}
}
}
Explanation / Answer
Answer -
import java.util.*;
public class MessageTester
{
public static void main (String[] args)
{
Scanner input = new Scanner(System.in);
String sender, recipient;
// Read the inputs
System.out.println("Enter the name of the Sender: ");
sender = input.nextLine();
System.out.println("Enter the name of the Recipient: ");
recipient = input.nextLine();
Message m1 = new Message(sender, recipient);
m1.setMessage();
System.out.println(m1);
}
public static class Message
{
private String sender;
private String recipient;
private String message;
public Message (String s, String r)
{
sender = s;
recipient = r;
}
public void setMessage()
{
Scanner input = new Scanner(System.in);
System.out.println("What do you want to put in the Message?: ");
message = input.nextLine();
}
public String toString()
{
return " Sender: " + sender + " Recipient: " + recipient + " Message: " + message;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.