The Message Class Every message contains some content (\"The British are coming!
ID: 3814107 • Letter: T
Question
The Message Class
Every message contains some content ("The British are coming! The British are coming!") and a creator, or author ("Paul Revere").
1. Write the Message class. The class should have the following:
Public Static Class Constants
Define a full set of limits and defaults, like MAX_MSG_LENGTH and DEFAULT_AUTHOR, for both min, max lengths and default data of every member. Set the maximum message length to a large number (at least a million) and the maximum author length to a reasonable value like 40 or 65, not 5 or 200.
Private Member Data
Public Methods
Default and 2-parameter constructors.
Mutator and Accessor for each member.
a toString() method that provides a formatted return String.
Private Methods
private static validation helpers to filter client parameters. These will support your public methods.
Test of Class Message
2. Write a program TestMessage.java. Instantiate two or more Message objects, some using the default constructor and some using the parameter-taking constructor. Mutate one or more of the members, and after that use the toString() to assist a screen output so we can see what all of your objects contain.
Next, test one or more accessors. Finally, test two or more mutators, providing both legal and illegal arguments and testing the return values (thus demonstrating that the mutators do the right thing).
3. Create a zip file which contains the following files:
Message.java
TestMessage.java
Explanation / Answer
import java.io.BufferedReader;
import java.io.InputStreamReader;
class TestMessage {
public static void main(String args[] ) throws Exception {
Message m1 = new Message();
Message m2 = new Message("Hi Everyone","Bhaskar Kumar");
Message m3 = new Message("Hi Five","Tim");
System.out.println(m1.toString());
m1.setMessage("Changed Message");
m1.setAuthor("Changed Author");
System.out.println(m1.toString());
System.out.println(m2.getMessage());
System.out.println(m2.getAuthor());
m3.setMessage("");
m3.setAuthor("Bhaskar Kumar");
m2.setAuthor("Tim");
System.out.println(m2.toString());
System.out.println(m3.toString());
}
}
class Message {
public static final int MAX_MSG_LENGTH = 1000000 ;
public static final int MIN_MSG_LENGTH = 1 ;
public static final int MAX_AUTHOR_LENGTH = 70 ;
public static final int MIN_AUTHOR_LENGTH = 5 ;
public static final String DEFAULT_AUTHOR = "Author";
public static final String DEFAULT_MESSAGE = "I";
private String message;
private String author;
public Message()
{
message = DEFAULT_MESSAGE;
author = DEFAULT_AUTHOR;
}
public Message(String message,String author)
{
if(messageValidation(message))
this.message =message;
else
this.message = DEFAULT_MESSAGE;
if(authorValidation(author))
this.author = author;
else
this.author = DEFAULT_AUTHOR;
}
public void setMessage(String message)
{
if(messageValidation(message))
this.message = message;
}
public String getMessage()
{
return message;
}
public void setAuthor(String author)
{
if(authorValidation(author))
this.author = author;
}
public String getAuthor()
{
return author;
}
public String toString()
{
return ("Author : "+author+" has given the Message : "+message);
}
private Boolean messageValidation(String message)
{
if(message.length()<MIN_MSG_LENGTH||message.length()>MAX_MSG_LENGTH)
{
System.out.println(("Message size cannot be less than "+MIN_MSG_LENGTH+" character or greater than "+MAX_MSG_LENGTH));
return false;
}
return true;
}
private Boolean authorValidation(String author)
{
if(author.length()<MIN_AUTHOR_LENGTH||author.length()>MAX_AUTHOR_LENGTH)
{
System.out.println(("Author size cannot be less than "+MIN_AUTHOR_LENGTH+" character or greater than "+MAX_AUTHOR_LENGTH));
return false;
}
return true;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.