Question

In: Computer Science

2 a. Write a new method for the Greeter class,    public void swapNames(Greeter other) {...}...

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);
   }
}

Solutions

Expert Solution

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:


Related Solutions

public class OOPExercises {     public static void main(String[] args) {         A objA = new...
public class OOPExercises {     public static void main(String[] args) {         A objA = new A();         B objB = new B();         System.out.println("in main(): ");         System.out.println("objA.a = "+objA.getA());         System.out.println("objB.b = "+objB.getB());         objA.setA (222);         objB.setB (333.33);       System.out.println("objA.a = "+objA.getA());         System.out.println("objB.b = "+objB.getB());     } } Output: public class A {     int a = 100;     public A() {         System.out.println("in the constructor of class A: ");         System.out.println("a = "+a);         a =...
Name the project pa3 [Method 1] In the Main class, write a static void method to...
Name the project pa3 [Method 1] In the Main class, write a static void method to print the following text by making use of a loop. Solutions without a loop will receive no credit. 1: All work and no play makes Jack a dull boy. 2: All work and no play makes Jack a dull boy. 3: All work and no play makes Jack a dull boy. 4: All work and no play makes Jack a dull boy. [Method 2]...
3. [Method 1] In the Main class, write a static void method to print the following...
3. [Method 1] In the Main class, write a static void method to print the following text by making use of a loop. Solutions without a loop will receive no credit. 1: All work and no play makes Jack a dull boy. 2: All work and no play makes Jack a dull boy. 3: All work and no play makes Jack a dull boy. 4: All work and no play makes Jack a dull boy. 4. [Method 2] In the...
JAVA please write this method public static void recursiveSelectionSort(int[] arr) { }
JAVA please write this method public static void recursiveSelectionSort(int[] arr) { }
JAVA please write this method public static void recursiveMergeSort(int[] arr) { }
JAVA please write this method public static void recursiveMergeSort(int[] arr) { }
Create a new Java file, containing this code public class DataStatsUser { public static void main...
Create a new Java file, containing this code public class DataStatsUser { public static void main (String[] args) { DataStats d = new DataStats(6); d.append(1.1); d.append(2.1); d.append(3.1); System.out.println("final so far is: " + d.mean()); d.append(4.1); d.append(5.1); d.append(6.1); System.out.println("final mean is: " + d.mean()); } } This code depends on a class called DataStats, with the following API: public class DataStats { public DataStats(int N) { } // set up an array (to accept up to N doubles) and other member...
Write program in Java import java.util.Scanner; public class Lab7Program { public static void main(String[] args) {...
Write program in Java import java.util.Scanner; public class Lab7Program { public static void main(String[] args) { //1. Create a double array that can hold 10 values    //2. Invoke the outputArray method, the double array is the actual argument. //4. Initialize all array elements using random floating point numbers between 1.0 and 5.0, inclusive    //5. Invoke the outputArray method to display the contents of the array    //6. Set last element of the array with the value 5.5, use...
Task 2/2: Java program Based upon the following code: public class Main {   public static void...
Task 2/2: Java program Based upon the following code: public class Main {   public static void main( String[] args ) {     String alphabet = "ABCDEFGHIJKLMNMLKJIHGFEDCBA";     for( <TODO> ; <TODO> ; <TODO> ) {       <TODO>;     } // Closing for loop   } // Closing main() } // Closing class main() Write an appropriate loop definition and in-loop behavior to determine if the alphabet string is a palindrome or not. A palindrome is defined as a string (or more generally, a token) which...
public class StackTest { public static void main(String[] args) { StackX theStack = new StackX(10); //...
public class StackTest { public static void main(String[] args) { StackX theStack = new StackX(10); // make new stack theStack.push(20); // push items onto stack theStack.push(30); theStack.push(40); theStack.push(40); theStack.push(60); theStack.push(80); theStack.showStack(); System.out.println("removeDownTo(40)"); theStack.removeDownTo(40); theStack.showStack(); } // end main() } public class QueueTest { public static void main(String[] args) { Queue theQueue = new Queue(20); // queue holds 5 items theQueue.insert(10); // insert 4 items theQueue.insert(20); theQueue.insert(30); theQueue.insert(40); theQueue.showQueue(); System.out.println("Removing 3 items"); theQueue.remove(); // remove 3 items theQueue.remove(); // (10, 20,...
import java.util.Scanner; public class Squaring { public static void main(String[] args) { Scanner sc = new...
import java.util.Scanner; public class Squaring { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int num=0; String s = ""; while (true) { System.out.println("Enter an integer greater than 1: "); try { // reading input s = sc.nextLine(); // converting into int num = Integer.parseInt(s); break; } catch (Exception e) { System.out.println(s + " is not valid input."); } } // Now we have a valid number // putting into square int square = num; int count...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT