In: Computer Science
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);
   }
}
Java Code:
File 1: Greeter.java
/**
   A class for producing simple greetings. (Revised to
include swapNames and createQualifiedGreeter)
*/
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 + "!";
   }
   /**
      swaps the names of this greeter and
another instance
   */
   public void swapNames(Greeter other)
   {
      String temp;
  
      //Swapping names using temporary
variable
      temp = this.name;
      this.name = other.name;
      other.name = temp;
   }
   /**
      Greet with a qualifier
message.
      @returns a new Greeter object with
its name being the qualifier string followed by
       " " and the executing greeter's
name
   */
   public Greeter createQualifiedGreeter(String
qualifier)
   {
       String temp;
      
       //Constructing string
       temp = "\"" + qualifier + " " +
this.name + "\"";
      
       //Creating a new Greeter object and
returning newly created object
       return new Greeter(temp);
   }
   private String name;
}
File 2: GreeterTester.java
/**
      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)
   {
      //Creating new Greeter class
objects
      Greeter greeter1 = new
Greeter("ABC");
      Greeter greeter2 = new
Greeter("PQR");
    
      //Fetching names
      String greeting1 =
greeter1.sayHello();
      String greeting2 =
greeter2.sayHello();
  
      //Printing names
      System.out.println("\n Before swap:
\n\n Greeting 1: " + greeting1 + " \n Greeting 2: " + greeting2 + "
\n");
  
      //Swapping names
      greeter1.swapNames(greeter2);
  
      //Fetching names
      greeting1 = greeter1.sayHello();
      greeting2 = greeter2.sayHello();
      //Swapping names
      System.out.println("\n After swap: \n\n
Greeting 1: " + greeting1 + " \n Greeting 2: " + greeting2 + "
\n");
  
      //Creating greeter3 object
      Greeter greeter3 = new
Greeter("world");
  
      //Creating qualified greeter
      Greeter g4 =
greeter3.createQualifiedGreeter("beautiful");
  
      //Fetching greeting
      String greeting =
g4.sayHello();
  
      //Printing string
      System.out.println("\n\n Qualified
String: " + greeting);
   }
}
------------------------------------------------------------------------------------------------------------------------------------------------------------------
Sample Output:
