1. For the class provided below, identify which elements are part of the class\'
ID: 3690405 • Letter: 1
Question
1. For the class provided below, identify which elements are part of the class's behavior and which are part of the class's state.
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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);
}
}
2. For the class above, identify which methods are accessors, which methods are mutators, and which methods are constructors.
3. 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.
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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);
}
}
Explanation / Answer
DATA MEMBERS part is the class's state
private String msg;
private String to;
private String from;
METHODS PART ARE THE class's behavior
/**
* 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);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.