CS 201: Data Structures

Java basics

Goals

Background

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 (+, -, *, /, //, %), 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 for most people around the fourth or fifth language, in my experience), 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 Carleton courses beyond CS 201, you'll eventually want to start teaching yourself Python, for which you may find some of my 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

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:

Why are those the programs I start with?

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, Part 1: set up Slack

Make sure you have accepted my invitation to join our class Slack workspace. Set up your working environment to include the Slack desktop app or a bookmark to the Slack website or the Slack app on your phone, and arrange things so you will check our Slack messages at least once per day during the term.

Your assignment, Part 2: set up Unix and Java

If you plan to work in either Weitz 138 or CMC 304, 306, 102, then you're done. Login to the macOS side of one of the lab computers, open the Terminal app, and off you go. We'll be using Java 1.8, which installed in all of those labs.

If you have your own computer and you want to do your CS201 work on it, then you may have extra work to do. I have prepared a computer setup guide to get you started. Check it out.

For this class, we will be using Java Standard Edition version 8, which is the version that's currently installed on macOS in all the CMC labs as well as in Weitz 138. Java 8 is the "long term support" version of Java that was most recent until 2018, when Java 11 came out. I expect that Carleton's labs (along with much of the rest of the Java world) will switch over to 11 over the next year. But I will write my samples and grade your code using Java 8. (By the way, Java 8 is often referred to as Java 1.8. Version numbering is weird. Sorry.)

Your assignment, Part 3: compare Python and Java

For each of the following Python/Java program pairs, read the programs, run the programs, and write down questions you have about the Java code. When you're done, post your questions on the #questions channel on Slack.

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.

One more thing

Start early, ask questions, and have fun!