Homework 0: A Few Java Exercises

A. Preliminaries


The exercises here aim to introduce you to a few basic points of Java syntax.

For this assignment, we'll give you credit for just submitting something, but we suggest that you do give them a serious try in order to give yourself a chance to get comfortable with Java and dust off your programming skills from last semester.

For each of the problems,

As with lab1, start this homework by using the following commands in your local repo directory (assuming that you are using our normal setup--working on the default master branch--and that you have committed any files you you already have there from other assignments.)

git fetch shared
git merge shared/hw0 -m "Get HW0 skeleton"
git push

This time, however, there will be nothing useful in the folder, since the idea is for you create and add the necessary files. As you create new files, be sure to use git add to add bring them under Git's control. To submit, follow the same instructions as for lab1:

Do the Reading

Make sure you've done the first reading assignment: AJR 1.1 through 1.9 and Chapter 1 of Head First Java. If you don't have Head First Java, that's fine, AJR provides all the information you need (but might be a harder read).

While you're reading, you may find it helpful to be able to run Java code so that you can experiment. You can compile and execute as you have done in lab, of course. There is also a convenient online Java compilation tool. To run code, simply add the code you'd like to execute to the main method and then press the Compile and Execute buttons.

Head First Java provides you with exercises that you can do as you read. Feel free to skip doing these if you'd like.

B. Max

Write a function max(int[] a) that returns the maximum value of the an int array. Try writing this function using both a while loop and a for loop. See the sidebar for convenient syntax for creating arrays.

You can specify an array using curly braces. A strange choice of symbols, for a Python programmer, but we're in the C family of languages here. For example:

int[] a = {1, 2, 3, 4}

would create an array containing 1, 2, 3, and 4 and then give it the name a.

In this context, the curly braces are interpreted in a completely different way from when we use them to begin and end a block of code.


Suppose we have an array of integers int[] a. The 3SUM problem asks if there are three integers in a[] whose sum is zero.

For this problem, write a function threeSum(int[] a) that returns true if there exist three integers in a that sum to zero and false otherwise. Integers may be used more than once. As in part 1, use your main function to perform an ad-hoc test that threeSum works.

For loops will look a lot more compact than while loops for this problem.


This might seem daunting at first, but it's relatively straightforward. As a hint, consider an alternative way of stating the problem: Do there exist three indices (not necessarily distinct) f, g, and h such that a[f] + a[g] + a[h] == 0?


Repeat the exercise from Part C, but with the constraint that each number can be used only once.