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

Consider the following java class: class Student {    private int student_Number;    private String student_Name;    public Student(int...
Consider the following java class: class Student {    private int student_Number;    private String student_Name;    public Student(int stNo,String name) {         student_Number=stNo;         student_Name=name;      }     public String getName() {       return student_Name;     }      public int getNumber() {       return student_Number;      }     public void setName(String st_name) {       student_Name = st_name;     } } Write a Tester class named StudentTester which contains the following instruction: Use the contractor to create a student object where student_Number =12567, student_Name = “Ali”. Use the setName method to change the name of...
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...
public class Clock { private int hr; private int min; private int sec; public Clock() {...
public class Clock { private int hr; private int min; private int sec; public Clock() { setTime(0, 0, 0); } public Clock(int hours, int minutes, int seconds) { setTime(hours, minutes, seconds); } public void setTime(int hours, int minutes, int seconds) { if (0 <= hours && hours < 24) hr = hours; else hr = 0; if (0 <= minutes && minutes < 60) min = minutes; else min = 0; if(0 <= seconds && seconds < 60) sec =...
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(); } /*...
Java Create an abstract Product Class Add the following private attributes: name sku (int) price (double)...
Java Create an abstract Product Class Add the following private attributes: name sku (int) price (double) Create getter/setter methods for all attributes Create a constructor that takes in all attributes and sets them Remove the default constructor Override the toString() method and return a String that represents the building object that is formatted nicely and contains all information (attributes) Create a FoodProduct Class that extends Product Add the following private attributes: expDate (java.util.Date) for expiration date refrigerationTemp (int) if 70...
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...
In Java, design and implement a class called Cat. Each Cat class will contain three private...
In Java, design and implement a class called Cat. Each Cat class will contain three private variables - an integer representing its speed, a double representing its meowing loudness, and a String representing its name. Using the Random class, the constructor should set the speed to a random integer from 0 to 9, the meowing loudness to a random double, and the name to anything you want; the constructor should take no parameters. Write “get” and “set” methods for each...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT