HW 0: A Java Crash Course

Introduction #

In this assignment, we will go through basic Java syntax concepts. To help ease the introduction, equivalent concepts are also shown in Python. This homework does NOT require that you know Python. Even if you don’t know Python, you’ll see that Python code is almost like pseudocode and should therefore be readable.

This tutorial assumes that you have significant (one semester) experience with at least some programming language, and is intended only to highlight the Java way of doing some previously familiar things. If you don’t have prior experience, please feel free to contact the staff if you need extra assistance.

This homework is due on Friday, August 25! Early lectures, labs, and projects will rely on being able to read and understand the basic structure of Java code.

Crash Course #

For ease of navigation, this crash course has been split up into several parts. Feel free to skim and read at whatever pace you feel comfortable with. You can start the exercises even before you finish reading part A - we recommend doing the reading and the exercises concurrently.

Part A: Java Syntax

Part B: Data Structures

Part C: Instance vs Static

Exercises #

UW has a large collection of introductory Java exercises that we will be borrowing. For HW 0, create an account on Practice-It, and complete the following:

Make sure to follow the directions on the exercise page, especially for printing versus returning!

If you run into trouble with the exercises, one strategy could be solving in Python first, then translating that to Java. If you’re having trouble with solving in Python, that’s fine, and not the point of this exercise. If you’d like to reference Python solutions, see the dropdowns below.

starTriangle

for i in range(5):
    line = ""
    for j in range(i + 1):
        line += "*"
    print(line)

printIndexed

def printIndexed(s):
    output = ""
    for i in range(len(s)):
        output += s[i]
        output += str(len(s) - 1 - i)
    print(output)

stutter

def stutter(s):
    output = ""
    for i in range(len(s)):
        output += s[i]
        output += s[i]
    return output

quadrant

def quadrant(x, y):
    if x == 0 or y == 0:
        return 0
    elif y > 0 and x > 0:
        return 1
    elif y > 0 and x < 0:
        return 2
    elif y < 0 and x < 0:
        return 3
    else:
        return 4

Once you’ve completed the 6 exercises, go to your list of solved problems, take a screenshot of the table, and submit the screenshot to HW 0 on Gradescope.

NOTE: If you are having trouble getting your screenshot into the PDF format required by Gradescope, try using this converter.

Congratulations! You’re prepared for the next few lectures, and have completed HW 0.

A programming language is not too different from a spoken language – in particular, you will get better the more code you write. The PracticeIt site has many problems available, and you should feel free to attempt more. (Their progression doesn’t exactly match ours, though – if you see a Scanner or need to generate a random number, you can skip that problem.)

We also recommend https://codingbat.com/java/AP-1, which has more advanced Java problems.

Last built: 2023-12-02 09:53 UTC