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.
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:
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.
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)
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.
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]."
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.
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.
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.