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...
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...
//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...
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 +...
Create a Java class named Package that contains the following: Package should have three private instance...
Create a Java class named Package that contains the following: Package should have three private instance variables of type double named length, width, and height. Package should have one private instance variable of the type Scanner named input, initialized to System.in. No-args (explicit default) public constructor, which initializes all three double instance variables to 1.0.   Initial (parameterized) public constructor, which defines three parameters of type double, named length, width, and height, which are used to initialize the instance variables of...
class A { public: //constructors // other members private: int a; int b; }; Give declatations...
class A { public: //constructors // other members private: int a; int b; }; Give declatations of operator functions for each of the following ways to overload operator + You must state where the declatation goes, whether within the class in the public or private section or outside the class. The operator + may be overloaded. a) as friend function b) as member function c) as non-friend, non-member function
public class SinglyLikedList {    private class Node{        public int item;        public...
public class SinglyLikedList {    private class Node{        public int item;        public Node next;        public Node(int item, Node next) {            this.item = item;            this.next = next;        }    }       private Node first;    public void addFirst(int a) {        first = new Node(a, first);    } } 1. Write the method add(int item, int position), which takes an item and a position, and...
public class StringNode { private String item; private StringNode next; } public class StringLL { private...
public class StringNode { private String item; private StringNode next; } public class StringLL { private StringNode head; private int size; public StringLL(){ head = null; size = 0; } public void add(String s){ add(size,s); } public boolean add(int index, String s){ ... } public String remove(int index){ ... } } In the above code add(int index, String s) creates a StringNode and adds it to the linked list at position index, and remove(int index) removes the StringNode at position...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT