Getting started with Java

Note that in the evenings in CMC 306, we have lab assistants on duty who are happy to help. If you're having trouble getting started, feel free to pop into CMC 306 to get help. There should be a schedule on the wall in or near the lab indicating what hours lab assistants are working. Of course, you're also welcome to see me in office hours, as well as attend prefect sessions.

Choose an editor

To get started, start up a text editor of your choice. Many students who have taken CS 111 at Carleton like TextWrangler. You're welcome to use that if you'd like. If you're willing to try something new, I'd encourage you to try one of these two options:

  1. Emacs is an editor that has been around for decades, yet is still amazingly popular amongst techies. It's a little tricky to learn, but it has two major advantages over TextWrangler: it's really powerful, and you can run Emacs under any operating system (Windows, OS X, Linux, etc.) You can find Emacs on our department lab computers within Finder, by going to Applications/CarletonApps/Emacs23. (You can drag that icon to your dock if you like.) If working on your own computer, you can visit the links above to download directly. When you first start it up, there's a tutorial link in the screen that you'll see that you can follow, if you wish.
  2. Aquamacs is a version of Emacs that is customized for OS X. It looks more like a Mac application, its keyboard shortcuts are more natural, and it is somewhat easier to use than standard Emacs. However, it only runs on OS X, so by learning Aquamacs as opposed to standard Emacs, you would lose some of the benefit of getting experienced with a cross-platform editor. Download Aquamacs to your own Mac if you like, or if you can find it on our department lab computers within Finder, by going to Applications/CarletonApps.

You are also welcome to use whatever other text editor you like. Feel free to ask questions: in person, or Piazza, are both great places to ask questions about editors.

Configure your environment

Regardless of which editor you're using, there's one configuration item I'd highly recommend taking setting: set your editor to use "soft tabs" instead of "hard tabs." Setting your editor for hard tabs means that whenever you hit the tab key, an actual invisible tab character is inserted, but your editor shows it to you invisibly as a series of spaces. Soft tabs means that when you hit the tab key, no invisible tab character is placed in your document; rather, a series of spaces are simply inserted for you. The main disadvantage of hard tabs is that if your program ends up with a mix of spaces and tabs, you may not be able to see that on your screen, but if you view your program in someone else's editor that is configured differently, your code may not properly line up.

If you're using TextWrangler in the department, this has likely been already set up for you. To double check: start up TextWrangler, then go to Preferences, then Editor Defaults. Check "Auto-expand tabs."

If you're trying Emacs or Aquamacs, the settings for everything go in a file called .emacs that you keep in your home directory. Open up the file ~/.emacs (it should be a new file if you haven't used Emacs before), and paste the following text in.

(setq-default indent-tabs-mode nil)

How this lab works

Below, you'll find a series of tasks. I've indicated with boldfaces "EXERCISE" the specific tasks that you are supposed to turn in. Specifically, submit a zip file via Moodle that contains all the files you need to turn in. For the programming exercises, include all the Java files with the code that you have updated. For the "thinking" exercises, include a file called answers.txt that contains your answers to those questions.

A first Java program

Look at the program Hello.java. Save this code into a directory within your account, then open the file up in your text editor. Look at each line, and talk to your partner about what each word and symbol might mean.

Before we can run your program, we need to compile it (translate it to a language the computer can understand). Hopefully, the program should compile without returning any error messages. If you do get errors, check to see if you got the code correctly from the website, or ask for help.

In a terminal window, navigate to the directory where you have saved Hello.java, and type

javac Hello.java

In addition to your Hello.java file, you should now see a file called Hello.class; this is your bytecode. Java can only run bytecode files. To run your program, type the following at the command prompt:

java Hello
(note that you do not include the ".class" extension when running your program)

EXERCISE 1: Modify the program so that it prints out "Welcome to Data Structures, [your names]."

Java input

This time, look at Input.java. Again, save it locally, open it up in your editor, read it through, try to understand it, compile it, and run it. (Again, get help if you have any error messages.)

EXERCISE 2: You may notice that we sometimes use System.out.print() and sometimes System.out.println(). What's the difference between the two? Play around with both of these in your program (try modifiying it in various ways and rerunning) to see if you can figure it out.

EXERCISE 3: What happens if you enter a number when prompted for your name and a name when prompted for a number? If you get a message, what did the message say? What do you think it means?

EXERCISE 4: Modify the program so that it asks for the current year, and the year you were born. Your program should then print out the difference between the two, which is (approximately) your age. Make sure to put parentheses around your subtraction, like in the example shown.

Arrays

Now grab the program Numbers.java, and again save it, bring it into your editor, try to understand it, compile it, and run it.

EXERCISE 5: Add code to the program so that it determines the total of all of the numbers in the array, and prints them out.

Creating objects from classes

Grab two files: NumberList.java, NumberListTester.java. Try to get them on the screen at the same time, if you can, by opening each in its own window. Try to understand it, compile it, and run it.

EXERCISE 6: Add a method to the class NumberList to sort the numbers. You can use any sorting algorithm you like (bubble, selection, insertion, whatever), so long as you actually code it up yourselves. Feel free to look up old Python code of yours and port it over ("port" is programming-speak for "translate"). Add code to the main method so that the numbers for each list are sorted before they are printed.

EXERCISE 7: Change the main method so that instead of three different variables (list1, list2, list 3), an array of NumberLists is created and used. Take out the code duplication and instead put it inside a loop.

EXERCISE 8: Add a static variable to the class NumberList which keeps track of how many NumberList objects have been instantiated. Provide a static method to print out the value of this variable.