Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Intoduction to Java Programming 1. In two to three short sentences, explain what

ID: 3708915 • Letter: I

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 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.

Explanation / Answer

Hi Dear,

Pelase find my answer.

1)

Encapsulation is defined as the wrapping up of data under a single unit. It is the mechanism that binds together code and the data it manipulates.Other way to think about encapsulation is, it is a protective shield that prevents the data from being accessed by the code outside this shield.

2)

States:

msg, to, from

Behaviours:

getFrom, getTo, append, getMessage, displayMessage

3)

Accessors: getFrom, getTo, getMessage

Mutators: append

Constructors: public Message(String source, String dest)

Q4)

Output:

Enter message: this is first message
Enter message: this is second message
Enter message:
To: xyz@g.com
From: abc@g.com
--------------------------------
this is first messagethis is second message

Process finished with exit code 0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote