Question: Intoduction to Java Programming 1. In two to three short sentences, explain what encapsulation means when we discuss object-oriented programming. 2. For the class provided
Intoduction to Java Programming
1. In two to three short sentences, explain what encapsulation means when we discuss object-oriented programming.
2. For the class provided below, identify which elements are part of the class's behavior and which are part of the class's state.
public class Message {
private String msg;
private String to;
private String from;
/**
* Constructs a new Message object from a source to
* a destination.
* @param source the "From" source of the message
* @param dest the "To" destination of the message
*/
public Message(String source, String dest) {
this.from = source;
this.to = dest;
this.msg = "";
}
/**
* Returns the source of the message
* @return the message source
*/
public String getFrom() {
return this.from;
}
/**
* Returns the destination of the message
* @return the message destination
*/
public String getTo() {
return this.to;
}
/**
* Adds a line to the end of the message
* @param the line to add
*/
public void append(String msg) {
this.msg = this.msg + msg;
}
/**
* Returns the current text of the message
* @return the current text
*/
public String getMessage() {
return this.msg;
}
/**
* Displays the current message to standard output
*/
public void displayMessage() {
System.out.println("To: " + this.to);
System.out.println("From: " + this.from);
System.out.println("--------------------------------");
System.out.println(this.msg);
}
}
3. For the class above, identify which methods are accessors, which methods are mutators, and which methods are constructors.
4. The above Message class can be used as a simple object to construct an e-mail message. The to and from e-mail addresses are set by the constructor, and the message is assembled one string at a time using the append method. Using the above Message class, write a simple main method that prompts the user for a source, a destination, and lines of their message, using the methods of the Message class to construct an e-mail message object. When the user enters an empty line for the message body, the program should use the displayMessage() method to display the finished e-mail message to the console and then end.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
