Discussion 2 handout
Group members (names & NetIDs)
Objectives
- Draw object diagrams to illustrate runtime state and reason about reference semantics
- Write Java code to create and process arrays
- Process program arguments in
main()
Problem 1: Object diagrams
Draw labeled boxes for variables “x”, “y”, “a”, and “b”. Then execute the following assignment statements by hand, adding and erasing values and arrows in your diagram accordingly.
x = 3;
y = 2 * x;
x = 7;
z = y;
a = "world";
b = a;
a = "hello";
Problem 2: Array initialization
Initialize an integer array containing two “1”s in two different ways.
One way
Another way
Initialize an integer array containing one hundred “1”s.
Problem 3: Multidimensional arrays
Draw an object diagram to illustrate execution of the following statements:
int[][] a = new int[3][];
a[0] = new int[]{3, 1, 4};
a[1] = new int[]{1, 5};
a[2] = a[0];
a[2][1] = 2;
Recall the semantics of the assignment statement from last week’s discussion. Based on that, what should happen when the following statement is executed?
a[1][2] = 9;
Problem 4: Program arguments
Implement an “echo” program that prints each argument passed to it, separated by spaces.
Example:
$ java Echo My name is Hal. My name is Hal.
public class Echo {
public static void main(String[] args) {
}
}
Submission
- Open the assignment page for “Discussion activity 2” in CMSX
- [Recorder] Find the “Group Management” section and invite each group member
- [Others] Refresh the page and accept your invitation
- [Recorder] Take a picture of your work and save as either a JPEG or a PDF file named “discussion_responses”. After all invitations have been accepted, upload your picture along with your code as your group’s submission.
- Recommended scanning apps: Microsoft Office Lens, Adobe Scan, Genius Scan, Evernote Scannable
Ensure that your group is formed and your work submitted before the Friday evening deadline.
Tips and reminders
- Discussion is not a race. Focus on the current activity being facilitated by your TA and engage your whole group to propose and explain ideas.
- Elect a recorder to maintain the “master” copy of your work (others are still encouraged to jot down ideas on scratch paper). Rotate this position each week.
- It is a violation of academic integrity to credit someone for work if they were not present when the work was done, and the whole group is accountable. Your CMS group must only include classmates who attended section with you on that day. Remember that our participation policies accommodate occasional absences without penalty.
- It is your individual responsibility to ensure that you are grouped on CMS and that your group’s work is submitted before the deadline. Log into CMS the day after section to ensure that everything is in order, and contact your groupmates if not. It would be prudent for multiple members to photograph the group’s work.
- Only one group member (typically the recorder) needs to upload a submission. But their submission must not be uploaded until after all group members have confirmed their membership in CMS (contact your TA if you have trouble grouping in CMS).