Write the output of the following demo code fragment: Patient p1 - new Patient(9
ID: 3825316 • Letter: W
Question
Write the output of the following demo code fragment: Patient p1 - new Patient(999, "Josh", 77, "Dr. Mike", 101);fun1(p1); sop(p1); public static Patient void(Patient p){p = new Patient();} Write the output of the following demo code fragment: Patient p1 - new Patient(999, "Josh", 77, "Dr. Mike", 101); p1 = fun2(); sop(p1); public static Patient fun2(){return new Patient();} Complete the demo code fragment to test setId method.//In Person class: public void setId(int id){if(id 999) throw new IllegalArgumentException("Invalid id"); this.id - id;}//In Demo class: Patient p = new Patient();Explanation / Answer
Answers to the question is provided below. Please don't forget to rate the answer if it helped. Thank you very much.
Q13:
In this case, fun1() receives a copy of the Patient object into a local variable p. This local variable p is reassigned a new Patient() with default constructor. Since p is local , no changes are seen in the calling function. Therefore sop(p1) prints the values of the Patient based on its toString( ) function ... (the values printed will be 999, Josh, 77 etc.)
Q14
Initially p1 is assigned a Patient object with values 999, Josh etc. But in the next line , it is reassigned a new Patient() created with default constructor. So it will have default values. So when sop(p1) is issued, the default values for the Patient are printed.
Q15:
Exception is thrown when setID() is called with id is 0 or -ve and > 999. So any values out of valid range 0-999 will throw exception
To test the exception being thrown, In the demo class, we can write a code like below:
Patient p = new Patient ();
try
{
p.setId(1001);
//OR p.setId(-2);
}catch(IllegalArgumentException e)
{
System.out.println(e.getMessage());
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.