HW1: Intro to Java

Java Basics

Learning your second programming language is different from learning your first. Assuming your first language isn't one of the many so-called esoteric programming languages or a more specialized language like SQL, you start out already knowing about many of the nearly-universal language constructs: assignments, loops, conditionals, functions, operators, (maybe) classes, etc. Learning the second language involves a lot of learning new syntax for old ideas, rather than learning both the ideas and the syntax at the same time.

As a result, learning your second language typically goes more quickly than learning the first. That doesn't mean the second one is easy to learn — “easy” starts to happen around the fourth or fifth language — but there are definitely opportunities to learn more efficiently the second time around.

For this class, I will assume that you are making a transition from Python (which we teach in CS 111) to Java (which we will use in CS 201). In fact, I imagine that a few of you met Java in high school, and some of you may even have skipped CS 111 and thus not know Python. If you're one of the latter and you plan to take courses beyond CS 201, you'll eventually want to start teaching yourself Python, for which you may find some of these exercises useful in a backwards sort of way.

During the first week of class, we're going to focus our attention on the differences between Python and Java to help get you started in Java. Some key differences include:

Language-Learning Strategies

This list is by Jeff Ondich; he's the “I” in this case. Thanks Jeff!

One strategy used by many people is to find a good tutorial and work through it from beginning to end. This is a great approach.

My personal language-learning approach is intended to keep my start-up time investment as low as possible, so I can move into serious projects right away. I won't know the whole language, not by a long shot, until I have written thousands of lines of code in various contexts. But my goal is to move from the "just studying" mode into the "doing serious work" mode as fast as I can.

To that end, I start by finding the official reference documentation for the language. Next I find a collection of simple sample programs (search for “name-of-language tutorial” or “name-of-language samples” or “name-of-language examples”). Then, with my browser handy, I study the examples just enough to enable me to write the following programs:

Hello World gets me to “I can create, compile, and run a program in this language.”

Line Reader teaches me to open and read from files, which is essential for writing interesting programs in most languages. It also teaches me at least one loop syntax.

Word Counter teaches me loops, conditionals, functions, classes (I intentionally use a simple class to store (word, count) pairs so I will learn the class syntax), and a little bit about formatted strings (to produce a pretty report). After that, I'm ready to get going on serious work.

These days, I can usually write these three programs in under an hour in a new language, with most of the time going into polishing and experimenting with the third program. Of course, I wasn't nearly so speedy when I learned my second language back at the dawn of time.

Your Assignment

For each of the following Python/Java program pairs, read the programs, run the programs (both Python and Java!), and write down questions you have about the Java code. When you're done, post your questions (and answers!) on Piazza. Look for my “How to post about HW1” post in the upper-left corner for instructions about asking and answering questions.

Take this exercise seriously. Figure out what you know, what you don't know, what's easy, what's confusing, etc. If you pay careful attention to these starter programs and I can get you quick turn-around on your main questions, you'll have a great start on writing your own Java programs.

Parting Notes

This assignment is worth 25 points. It is graded entirely on participation; the absolute baseline for an A is that you post three constructive things to Piazza (which may be either questions or answers). Please post lots more than that, but three will give you full credit.

And don't forget...

Start early, ask lots of questions, and have fun!

This assignment originally designed by Jeff Ondich. Thanks for sharing, Jeff!