Question
2 a. Write a new method for the Greeter class, public void swapNames(Greeter other) {...} that swaps the names of this greeter and another instance.
2 a. Write a new method for the Greeter class,
public void swapNames(Greeter other) {...}
that swaps the names of this greeter and another instance.
b. write a new method for the Greeter class: public Greeter createQualifiedGreeter(String qualifier) { ..... } that returns a new Greeter object with its name being the qualifier string followed by " " and the executing greeter's name (i.e. this.name). For example: Greeter g = new Greeter("world"); Greeter g2 = g.createQualifiedGreeter("beautiful");
g2.name will be the string "beautiful world"
c. Write a GreeterTester class that shows how the swapNames() and the createQualifiedGreeter() methods are used.
Write javadoc comments. Include both java files in your solution document.
Here is the original greeter class code:
/** A class for producing simple greetings. (Revised to include sayGoodbye) */ public class Greeter { /** Constructs a Greeter object that can greet a person or entity. @param aName the name of the person or entity who should be addressed in the greetings. */ public Greeter(String aName) { name = aName; } /** Greet with a "Goodbye" message. @return a message containing "Goodbye" and the name of the greeted person or entity. */ public String sayGoodbye() { return "Goodbye, " + name + "!"; } /** Greet with a "Hello" message. @return a message containing "Hello" and the name of the greeted person or entity. */ public String sayHello() { return "Hello, " + name + "!"; } private String name; }
-----------------------------
Here is the original greeterTester code:
/** A class for testing the methods of class Greeter. */ public class GreeterTester { /** This method creates a greeter object and prints the strings produced by calling both sayHello and sayGoodbye with that object. @param args unused */ public static void main(String[] args) { Greeter worldGreeter = new Greeter("World"); String greeting = worldGreeter.sayHello(); System.out.println(greeting); greeting = worldGreeter.sayGoodbye(); System.out.println(greeting); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started