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

So I\'m having trouble with my Java program and not sure which parts to modify.

ID: 3671442 • Letter: S

Question

So I'm having trouble with my Java program and not sure which parts to modify. Heres the instructions: Modify program 3.8 so that it declares only one string instance variable named name. Then rewrite the swap() method so that it accepts an EncapsulateNames as a parameter and swaps the value referenced by the parameter with the value referenced by the object used when the method is called. For example the method call AName.swap(BName) should switch the values contained in the objects AName and BName. the method header should be public void swap(EncapsulateNames secName).

here's my code so far:

// Program 3.8

public class EncapsulateNames
{
   // Data Declarations Section
   private String nameOne;       // First name is referenced by this variable
   private String nameTwo;       // Second name is referenced by this variable
  
   // Method definitions section
   public EncapsulateNames(String s1, String s2)   // constructor
   {
   nameOne = s1;
   nameTwo = s2;
   }
  
   //Methods to get and set individual string values
   public String getFirst(){return nameOne;}
   public String getSecond(){return nameTwo;}
   public void setAName(String s1){nameOne = s1;}
   public void SetBName(String s2){nameTwo = s2;}
  
   // Swap method
   public void swap()
   {
       String temp; // Declare a third variable for temporarily holding a double
      
       temp = nameOne; // Step 1. Store the first variable's value in the temporary location
       nameOne = nameTwo; // Step 2. Store the second variable's value in the first variable
       nameTwo = temp; // Step 3. Store the temporary value in the second variable
   }
  
}

public class UseEncapsulateNames {

   public static void main(String[] args)
   {
       String firstName = "Billings";
       String secName = "Ajax";
       EncapsulateNames namePair;   // Declare an object type
      
       namePair = new EncapsulateNames(firstName, secName);   // Create an object
       // Verify the accessors
       System.out.println("The first name is " + namePair.getFirst());
       System.out.println("The second name is " + namePair.getSecond());
       // Verify the mutators
       namePair.setAName("Calisto");
       namePair.SetBName("Breyer");
       System.out.println(" The first name is " + namePair.getFirst());
       System.out.println("The second name is " + namePair.getSecond());
       // 2nd round of mutators
       namePair.setAName("Andrew");
       namePair.SetBName("Sanchez");
       System.out.println(" The first name is " + namePair.getFirst());
       System.out.println("The second name is " + namePair.getSecond());
      
   }

}

Explanation / Answer


//EncapsulateNames.java
public class EncapsulateNames
{
   // Data Declarations Section
   private String nameOne;       // First name is referenced by this variable
   private String nameTwo;       // Second name is referenced by this variable

   // Method definitions section
   public EncapsulateNames(String s1, String s2)   // constructor
   {
       nameOne = s1;
       nameTwo = s2;
   }

   //Methods to get and set individual string values
   public String getFirst(){return nameOne;}
   public String getSecond(){return nameTwo;}
   public void setAName(String s1){nameOne = s1;}
   public void SetBName(String s2){nameTwo = s2;}

   /* Swap method that swaps the values of the passing class object with
       current class */
   public void swap(EncapsulateNames secName)
   {

       //Create a temporary object temp of EncapsulateNames class
       EncapsulateNames temp
       =new EncapsulateNames(secName.getFirst(),secName.getSecond()); // Declare a third variable for temporarily holding a double

       //call setAName method
       secName.setAName(nameOne);
       //call setBName method
       secName.SetBName(nameTwo);

       //set temp object values to this object
       nameOne=temp.getFirst();
       nameTwo=temp.getSecond();
   }
}


----------------------------------------------------------------------------------------------------------------------------------------------------------------

/**The test java program UseEncapsulateNames that calls swap method
* and print the values of the objects of EncapsulateNames before
* and after method calling*/
//UseEncapsulateNames.java
public class UseEncapsulateNames
{

   public static void main(String[] args)
   {
       String first = "Billings";
       String second = "Ajax";
       EncapsulateNames namePair;   // Declare an object type           
       namePair = new EncapsulateNames(first, second);   // Create an object
       System.out.println("Before swapping ");     
       System.out.println("namePair object ");     
       System.out.println("The first name is " + namePair.getFirst());
       System.out.println("The second name is " + namePair.getSecond());     
       //Create an instance of EncapsulateNames
       EncapsulateNames secName=new EncapsulateNames("Calisto","Breyer");     
       System.out.println("secName object ");     
       System.out.println("The first name is " + secName.getFirst());
       System.out.println("The second name is " + secName.getSecond());     
     
       //calling swap method with object of EncapsulationNames
       namePair.swap(secName);
     
       System.out.println("After swapping");     
       System.out.println("namePair object ");     
       System.out.println("The first name is " + namePair.getFirst());
       System.out.println("The second name is " + namePair.getSecond());     
       System.out.println("secName object ");     
       System.out.println("The first name is " + secName.getFirst());
       System.out.println("The second name is " + secName.getSecond());     
   }
}//end of the class UseEncapsulateNames

------------------------------------------------------------------------------------------------------------------------------------------------------------

Sample Output:

Before swapping
namePair object

The first name is Billings
The second name is Ajax
secName object

The first name is Calisto
The second name is Breyer
After swapping
namePair object

The first name is Calisto
The second name is Breyer
secName object

The first name is Billings
The second name is Ajax

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