171k views
0 votes
Assume that the classes listed in the Java Quick Reference have been imported where appropriate.

Unless otherwise noted in the question, assume that parameters in method calls are not null and that methods are called only when their preconditions are satisfied.

In writing solutions for each question, you may use any of the accessible methods that are listed in classes defined in that question. Writing significant amounts of code that can be replaced by a call to one of these methods will not receive full credit.

The following class represents an invitation to an event. The variable hostName represents the name of the host of the event and the variable address represents the location of the event.

public class Invitation

{

private String hostName;

private String address;

public Invitation(String n, String a)

{

hostName = n;

address = a;

}

}
(c) Write a method for the Invitation class that will accept the name of a person who will be invited as a string parameter and return a string consisting of the name of the person being invited along with name of the host and location of the event.

For example, if the host name is "Karen", the party location is "1234 Walnut Street", and the person invited is "Cheryl", the method should return a string in the following format.

Dear Cheryl, please attend my event at 1234 Walnut Street. See you

then, Karen.

Write the method below. Your implementation must conform to the example above.

1 Answer

4 votes

Final answer:

To create an invitation string in the Invitation class, implement a method named 'invite' that returns a formatted invitation string incorporating the person's name, event address, and host's name.

Step-by-step explanation:

To answer your question, we need to write a method for the Invitation class. This method will generate an invitation string for a specified person. Below is an implementation of the method:

public String invite(String inviteeName) {
return "Dear " + inviteeName + ", please attend my event at " + address + ". See you then, " + hostName + ".";
}
The method takes in one parameter, inviteeName, which is the name of the person being invited. It returns a string that includes the invitee's name, the event location, and the host's name, formatted as requested.

User Maximilian Lindsey
by
8.5k points