Question

In: Computer Science

JAVA -The next three questions use the following class: class Identification { private int idNum;   ...

JAVA -The next three questions use the following class:


class Identification {
private int idNum;
  
public Identification() {
this(0);
}
  
public Identification(int startingIdNum) {
idNum = startingIdNum;
}
  
public int getIdNum() { return idNum; }
  
public void setIdNum(int idNum) {
this.idNum = idNum;
}
}


Here is one program using the above class:


public class Main {
   public static void main(String[] args) {
       Identification i1 = new Identification();
       Identification i2 = new Identification(7);
       swap(i1.getIdNum(), i2.getIdNum());
       System.out.print(i1.getIdNum() + " " +
i2.getIdNum());
   }
  
   public static void swap(int c1, int c2) {
   int temp = c1;
   c1 = c2;
   c2 = temp;
   }
}

If you run the program, you’ll see that the output is:
0 7
Explain using words and/or pictures why that is the output.


Here is a second program using the above class:


public class Main {
   public static void main(String[] args) {
       Identification i1 = new Identification();
       Identification i2 = new Identification(7);
       swap(i1, i2);
       System.out.print(i1.getIdNum() + " " +
i2.getIdNum());
   }
  
   public static void swap(Identification i1,
Identification i2) {
   int temp = i1.getIdNum();
   i1.setIdNum(i2.getIdNum());
   i2.setIdNum(temp);
   }
}

If you run the program, you’ll see that the output is:
7 0
Explain using words and/or pictures why that is the output.

Here is a third program using the above class:


public class Main {
   public static void main(String[] args) {
       Identification i1 = new Identification();
       Identification i2 = new Identification(7);
       swap(i1, i2);
       System.out.print(i1.getIdNum() + " " +
i2.getIdNum());
   }
  
   public static void swap(Identification i1,
Identification i2) {
   Identification temp = i1;
   i1 = i2;
   i2 = temp;
   }
}

If you run the program, you’ll see that the output is:
0 7
Explain using words and/or pictures why that is the output.

Solutions

Expert Solution

PLEASE GIVE IT A THUMBS UP, I SERIOUSLY NEED ONE, IF YOU NEED ANY MODIFICATION THEN LET ME KNOW, I WILL DO IT FOR YOU

Here is one program using the above class:
public class Main {
   public static void main(String[] args) {
       Identification i1 = new Identification();
       Identification i2 = new Identification(7);
       swap(i1.getIdNum(), i2.getIdNum());
       System.out.print(i1.getIdNum() + " " +
i2.getIdNum());
   }
  
   public static void swap(int c1, int c2) {
   int temp = c1;
   c1 = c2;
   c2 = temp;
   }
}

Explanation:

In problem 1, inside the swap method their is no statement to set the identation number, so basically here swap method is of no use, as the values outside the swap function remains same.

So, since their is no statement of setIdNum inside swap function, so the values remains same after the swap function executed that's why the output comes 0 7

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

Here is a second program using the above class:
public class Main {
   public static void main(String[] args) {
       Identification i1 = new Identification();
       Identification i2 = new Identification(7);
       swap(i1, i2);
       System.out.print(i1.getIdNum() + " " +
i2.getIdNum());
   }
  
   public static void swap(Identification i1,
Identification i2) {
   int temp = i1.getIdNum();
   i1.setIdNum(i2.getIdNum());
   i2.setIdNum(temp);
   }
}

Explanation:

In this swap method, the values are getting exchanged and get updated using the setIdNum(), so after the swap function ends, the values are updated via setUdNum in swap function.

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

Here is a third program using the above class:


public class Main {
   public static void main(String[] args) {
       Identification i1 = new Identification();
       Identification i2 = new Identification(7);
       swap(i1, i2);
       System.out.print(i1.getIdNum() + " " +
i2.getIdNum());
   }
  
   public static void swap(Identification i1,
Identification i2) {
   Identification temp = i1;
   i1 = i2;
   i2 = temp;
   }
}

Explanation:

In this also inside swap function, the swapping is done locally,i.e, only restricted to the swap function, outside swap function, the values would be same as before or earlier, that's why after the swap function , the values printed are same ,i.e, 0 7


Related Solutions

Java Implement a class named “Fraction” with the following properties: numerator: int type, private denominator: int...
Java Implement a class named “Fraction” with the following properties: numerator: int type, private denominator: int type, private and the following methods: one default constructor which will create a fraction of 1/1. one constructor that takes two parameters which will set the values of numerator and denominator to the specified parameters. int getNum() : retrieves the value of numerator int getDenom(): retrieves the value of the denominator Fraction add(Fraction frac): adds with another Fraction number and returns the result in...
Java program to implement circular linked list. public class CircularLinkedList { private Node tail; private int...
Java program to implement circular linked list. public class CircularLinkedList { private Node tail; private int size; public CircularLinkedList() { tail= null; size = 0; } public int size(){ return size; } public boolean isEmpty() { return size==0; } //if list is not empty return the first element public E first() { if (isEmpty()) return null; //code here return 0; } //if list not empty return last element public E last() { if (isEmpty()) return null; return tail.getElement(); } /*...
Research "Const Correctness" and answer the following questions: Given: class SimpleClass { private: int _x; public:...
Research "Const Correctness" and answer the following questions: Given: class SimpleClass { private: int _x; public: SimpleClass(int x) : _x(x){} int getX() const { return _x; } void setX(int newX) { _x = newX; } void displayDataWithCustomMessage(const string &customMessage) { cout<<"Data: "<<_x<<endl; cout<<"Custom Message: "<<customMessage<<endl; } }; What is the usefulness of the "const" keyword in the definition of the "getX" member function? What is the usefulness of the "const" keyword in the definition of the "displayDataWithCustomMessage" member function? Why...
For Questions 1-3: consider the following code: public class A { private int number; protected String...
For Questions 1-3: consider the following code: public class A { private int number; protected String name; public double price; public A() { System.out.println(“A() called”); } private void foo1() { System.out.println(“A version of foo1() called”); } protected int foo2() { Sysem.out.println(“A version of foo2() called); return number; } public String foo3() { System.out.println(“A version of foo3() called”); Return “Hi”; } }//end class A public class B extends A { private char service; public B() {    super();    System.out.println(“B() called”);...
public class Date { private int dMonth; //variable to store the month private int dDay; //variable...
public class Date { private int dMonth; //variable to store the month private int dDay; //variable to store the day private int dYear; //variable to store the year //Default constructor //Data members dMonth, dDay, and dYear are set to //the default values //Postcondition: dMonth = 1; dDay = 1; dYear = 1900; public Date() { dMonth = 1; dDay = 1; dYear = 1900; } //Constructor to set the date //Data members dMonth, dDay, and dYear are set //according to...
Coding in Java private int getLastDayOfMonth(int m, int y)– return the last day of the month...
Coding in Java private int getLastDayOfMonth(int m, int y)– return the last day of the month and year entered as input parameter. Remember to invoke the isLeapYear method when appropriate. If the month entered as input parameter is 2 (February) and the year passed as input parameter is a leap year this method should return 29. private boolean isDateValid (int m, int d, int y) – return true if day, month, and year entered as input parameters form a valid...
public class IntNode               {            private int data;            pri
public class IntNode               {            private int data;            private IntNode link;            public IntNode(int data, IntNode link){.. }            public int     getData( )          {.. }            public IntNode getLink( )           {.. }            public void    setData(int data)     {.. }            public void    setLink(IntNode link) {.. }         } All questions are based on the above class, and the following declaration.   // Declare an empty, singly linked list with a head and a tail reference. // you need to make sure that head always points to...
//Question1: What is the name of the following lines in the data type class? private int...
//Question1: What is the name of the following lines in the data type class? private int intVariable; private float floatVariable; private String stringVariable; //Question2: What is the name of the following lines in the data type class? public DataTypeClass_Smith() { intVariable = 0; floatVariable = 0.0; stringVariable = “aString”; } //Question3: What is the name of the following lines in the data type class? public DataTypeClass_Smith( int intVar, float floatVar, String stringVar) { intVariable = intVar; floatVariable = floatVar; stringVariable...
public class SumMinMaxArgs { private int[] array; // You will need to write the following: //...
public class SumMinMaxArgs { private int[] array; // You will need to write the following: // // 1. A constructor that takes a reference to an array, // and initializes an instance variable with this // array reference // // 2. An instance method named sum, which will calculate // the sum of the elements in the array. If the array // is empty (contains no elements), then this will // will return 0. You will need a loop for...
java programing Q: Given the following class: public class Student { private String firstName; private String...
java programing Q: Given the following class: public class Student { private String firstName; private String lastName; private int age; private University university; public Student(String firstName, String lastName, int age, University university) { this.firstName = fisrtName; this.lastName = lastName; this.age = age; this.university = university; } public String getFirstName(){ return firstName; } public String getLastName(){ return lastName; } public int getAge(){ return age; } public University getUniversity(){ return university; } public String toString() { return "\nFirst name:" + firstName +...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT