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

Here\'s the question: Consider the following code segment. Location L1 = new Loc

ID: 3618522 • Letter: H

Question

Here's the question:
Consider the following code segment.
   Location L1 = new Location("Baltimore",39.18, 76.38);    Location L2 = L1;    Location L3 = L1;    L1.setName("NewYork");    L2.setName("Chicago");    System.out.println(L1.getName() + " " +L2.getName() + " " + L3.getName());
What is printed when the code executes? A. NewYork NewYork NewYork B. Chicago Chicago Chicago C. NewYork Chicago Baltimore D. Chicago Chicago Baltimore E. NewYork Chicago Baltimore
Well the answer in this book says that it is "B" and I picked"D" so I'm probably overlooking something. Can someone explain whyit the answer is "B" and not "D"? The visual I used was that L1'slocation was set to a specific new location and then L2 and L3pointed to that location. So after L1 was set to NewYork, I thoughtthe others would change to NewYork Baltimore Baltimore. Then afterL2 is set to NewYork it would be NewYork ChicagoBaltimore.


Consider the following code segment.
   Location L1 = new Location("Baltimore",39.18, 76.38);    Location L2 = L1;    Location L3 = L1;    L1.setName("NewYork");    L2.setName("Chicago");    System.out.println(L1.getName() + " " +L2.getName() + " " + L3.getName());
What is printed when the code executes? A. NewYork NewYork NewYork B. Chicago Chicago Chicago C. NewYork Chicago Baltimore D. Chicago Chicago Baltimore E. NewYork Chicago Baltimore
Well the answer in this book says that it is "B" and I picked"D" so I'm probably overlooking something. Can someone explain whyit the answer is "B" and not "D"? The visual I used was that L1'slocation was set to a specific new location and then L2 and L3pointed to that location. So after L1 was set to NewYork, I thoughtthe others would change to NewYork Baltimore Baltimore. Then afterL2 is set to NewYork it would be NewYork ChicagoBaltimore.

   Location L1 = new Location("Baltimore",39.18, 76.38);    Location L2 = L1;    Location L3 = L1;    L1.setName("NewYork");    L2.setName("Chicago");    System.out.println(L1.getName() + " " +L2.getName() + " " + L3.getName());
What is printed when the code executes? A. NewYork NewYork NewYork B. Chicago Chicago Chicago C. NewYork Chicago Baltimore D. Chicago Chicago Baltimore E. NewYork Chicago Baltimore
Well the answer in this book says that it is "B" and I picked"D" so I'm probably overlooking something. Can someone explain whyit the answer is "B" and not "D"? The visual I used was that L1'slocation was set to a specific new location and then L2 and L3pointed to that location. So after L1 was set to NewYork, I thoughtthe others would change to NewYork Baltimore Baltimore. Then afterL2 is set to NewYork it would be NewYork ChicagoBaltimore.

Explanation / Answer

Location L1 = new Location("Baltimore", 39.18,76.38);                  
   Location L2 =L1;                                                    
        
   L1.setName("NewYork");                                                            
   L2.setName("Chicago");                                                             
   System.out.println(L1.getName() + " " +L2.getName() + " " + L3.getName());

What is printed when the code executes? A. NewYork NewYork NewYork B. Chicago Chicago Chicago C. NewYork Chicago Baltimore D. Chicago Chicago Baltimore E. NewYork Chicago Baltimore

Well the answer in this book says that it is "B" and I picked"D" so I'm probably overlooking something. Can someone explain whyit the answer is "B" and not "D"? The visual I used was that L1'slocation was set to a specific new location and then L2 and L3pointed to that location. So after L1 was set to NewYork, I thoughtthe others would change to NewYork Baltimore Baltimore. Then afterL2 is set to NewYork it would be NewYork ChicagoBaltimore.




when you set the classes equal to each other, youset their locations in memory as being the same
therefore when you change 1 you change them all
hope this helps