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

Create a class called Names that has two String type instance variables called f

ID: 3812275 • Letter: C

Question

Create a class called Names that has two String type instance variables called firstName and lastName and one integer type class variable called count. The count variable should be used in the constructor in order to keep track of how many Names objects are instantiated. Make sure to include appropriate "get" and "set" methods. Create a class called OneName that has one String type instance variable called name. Create a method called public String swapName(Names name). This method accepts a Names object as an argument and swaps the value of firstName with the value of name in the OneName class. Make sure to include appropriate "get" and "set" methods. Create a class called TestName that contains the main method. Instantiate both a Names object and a OneName object. Set the values of the instance variables in both objects using user input. Output the values of the instance variables for both objects then use the swapName method and after that, output the values of the instance variables for both objects again.

Explanation / Answer

import java.util.Scanner;

class Names{
   private String firstName;
   private String lastName;
   private int count = 0;
  
  
   public Names(){
       count = count + 1;
   }


   public String getFirstName() {
       return firstName;
   }


   public void setFirstName(String firstName) {
       this.firstName = firstName;
   }


   public String getLastName() {
       return lastName;
   }


   public void setLastName(String lastName) {
       this.lastName = lastName;
   }


   public int getCount() {
       return count;
   }

  
}

class OneName{
   private String name;

   public String getName() {
       return name;
   }

   public void setName(String name) {
       this.name = name;
   }
  
   public String swapName(Names name){
       String temp = name.getFirstName();
       this.name = temp;
       return this.name;
   }
}

public class TestName {

   public static void main(String[] args) {
       Scanner in = new Scanner(System.in);
       Names n = new Names();
       OneName on = new OneName();
       String fname = in.next();
       String lname = in.next();
       n.setFirstName(fname);
       n.setLastName(lname);
      
       fname = n.getFirstName();
       lname = n.getLastName();
       Names newname = new Names();
       newname.setFirstName("Frank");
      
       System.out.println("Name before swapping: " + fname + " " + lname);
       String swap = on.swapName(newname);
       System.out.println("Name after swapping: " + swap + " " + lname);
      
       in.close();
   }

}

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