CIS 163 |
References |
Summer 2020 |
Objectives:
- Understand how references work in Java
- Understand the differences between variables for primitive data and references.
- Be able to trace Java code that uses primitive variables and references.
- Be able to explain why Java code behaves the way it does.
Problems
You may work in teams of two.
If you haven't yet, clone the CIS 163 sample code repository: github.com/kurmasz-SampleCode/CIS163-SampleCode
(the SSH URL is git@github.com:kurmasz-SampleCode/CIS163-SampleCode.git
).
Load the ReferenceExamples
directory into your favorite IDE and make sure you can run the main
method in ReferenceReadingExamples.java
.
Answer the following questions. It is important to trace through the code before you run it. (I can't stop you from just running the code and writing down the answer; but, if you do, it will come back to haunt you on the exams.)
- Predict the output of running case 1 in
ReferenceReadingExamples.
. - Set
exampleToRun = 1
inmain
and run the code. Were you correct? If not, (A) talk to me and make sure you understand why the code behaves the way it does and (B) explain the misconceptions that led to your incorrect answer. - Predict the output of running case 2.
- Set
exampleToRun = 2
, run the code, and explain your misconceptions if your initial answer was incorrect. - Explain how the values of
x
andy
get flipped between the linesinit
andin
. - Why are the changes made by
method1
not visible frommain
? - Predict the output of running case 3.
- Set
exampleToRun = 3
, run the code, and explain your misconceptions if your initial answer was incorrect. - Predict the output of running case 4.
- Set
exampleToRun = 4
, run the code, and explain your misconceptions if your initial answer was incorrect. - Why are the changes made by
method4
in case 4 "lost" and not visible inmain
? You answer should contrast the behavior ofmethod3
in case 3 with the behavior ofmethod4
in case 4. - Predict the output of running case 5.
- Set
exampleToRun = 5
, run the code, and explain your misconceptions if your initial answer was incorrect. - Why are the changes made by
method5
in case 5 "lost" and not visible inmain
? - Predict the output of running case 6.
- Set
exampleToRun = 6
, run the code, and explain your misconceptions if your initial answer was incorrect. - This time, only the change to
ptx
's' x value is visible inmain
. The change to they
value is "lost". Why? - Predict the output of running case 7.
- Set
exampleToRun = 7
, run the code, and explain your misconceptions if your initial answer was incorrect. - Inside
method7
, why does modifyingx
also modifyy
? - After running
method7
, why is only the change topty
visible in the main method? (Why is the change toptx
"lost"?) - Optional: Predict the output of cases 8 - 11.
Submission
Simply email me a document with your answers. If you worked with a partner, be sure both names are on your submission.Updated Thursday, 7 May 2020, 8:14 PM