Discussion 2 handout

Requirements reminder

Problem 1: 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 2: 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[0][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[2][2] = 9;

Problem 3: Program arguments

(Your TA may treat this as a demo instead of an exercise if time is short; in that case, a response is not required on your worksheet.)

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) {






    }
}

Problem 4: Code style

Download IntHeap.java from the course website and skim it. Discuss: what about this code makes it harder to understand than necessary?

You do not need to understand what the class is for or how it works. But you should understand the effects of individual statements. If any syntax (aside from throws, which we’ll get to in lecture 8) is unfamiliar, ask about it.

Install the class formatting preferences in IDEA. Have one group member reformat IntHeap.java while another retains the original. Compare the formatted copy to the original.

Are there aspects of the original format that you prefer?

Are there deficiences that reformatting did not fix?

Submission

  1. Open the assignment page for “Discussion activity 2” in CMSX
  2. [Recorder] Find the “Group Management” section and invite each group member
  3. [Others] Refresh the page and accept your invitation
  4. [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 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