Navigation
A. Preliminaries
Goals
The exercises here aim to introduce you to a few basic points of Java
syntax.
- Initializing variables, e.g.
int x = 5;
- Comparisons and logical operators
- Conditionals (if statements)
- Iteration (using if and while)
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,
- No starter code is provided. Use examples from class and the textbook to write your solutions. We don't care what you call your classes, since we don't intend to test your submissions.
- You can use whatever tool you'd like to write and run your code. This could be the javac/java on your own computer or the lab machines, etc., or IntelliJ, or an online execution tool.
- For each problem, use the main method to perform an ad-hoc test (by ad-hoc, I just mean a crude test that isn't generalizable like the fancier tests that you'll be developing in HW1).
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 there will be no skeleton file for your solution. 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.
We have provided a skeleton Tester.java
file for writing simple tests of your
solutions. However, you will have to figure out how to get it to actually
call your solutions before you can compile and run it.
You can use javac
to compile it, and java Tester
to run it.
To submit, follow the same instructions as for lab1:
- Commit (with
git commit -a
) any changes you've made. - Tag the submission (e.g.,
git tag hw0-0
). - Push your commits and tags (
git push
andgit push --tags
).
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.
C. 3SUM
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.
Examples:
threeSum({-6, 2, 4})
is true, can select -6, 2, and 4.threeSum({-6, 2, 5})
is false.threeSum({-6, 3, 10, 200})
is true, can select -6, 3, and 3.threeSum({8, 2, -1, 15})
is true, can select 2, -1, and -1.threeSum({8, 2, -1, -1, 15})
is true, can select 2, -1, -1.threeSum({5, 1, 0, 3, 6})
is true, can select 0, 0, and 0.
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?
D. 3SUM_DISTINCT
Repeat the exercise from Part C, but with the constraint that each number can be used only once.
Examples:
threeSumDistinct({-6, 2, 4})
is true, can select -6, 2, and 4.threeSumDistinct({-6, 2, 5})
is false.threeSumDistinct({-6, 3, 10, 200})
is false.threeSumDistinct({8, 2, -1, 15})
is false.threeSumDistinct({8, 2, -1, -1, 15})
is true, can select 2, -1, and -1.threeSumDistinct({5, 1, 0, 3, 6})
is false.