CS 201: Data Structures (Spring 2017)
HW01: Java basics
Due: Friday, 03/31 at 22:00
Prologue
Note that in the evenings in CMC 102 and 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 in 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.
1. Choose an editor
To get started, start up a text editor of your choice. If you took CS 111 at Carleton, you might have experience with TextWrangler, Brackets, or some other editor. 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 options:
- 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 some major advantages: 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/Emacs. (You can drag that icon to your dock if you like.) If working on your own computer, here is one page that summarizes nicely how to install it for different operating systems. 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.
- 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.
- Vim is another editor that has been around for a very long time, and
is also still exceedingly popular among heavy computer users. It has the
advantage that essentially any UNIX system (Mac or Linux) always has it
installed, and it is very lightweight and starts up quickly. It is extremely
powerful. You can start it in a terminal window in any Mac or Linux system by
typing
vim
at a terminal prompt. You can also download it for Windows if you wish to install it on your own computer.
You are also welcome to use whatever other text editor you like. Feel free to ask questions: in person or Moodle Discussion Forum are both great places to ask questions about editors. I am personally a (heavy) Vim user.
2. Configure your environment
Regardless of which editor you're using, there's one configuration item I'd highly recommend: 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.
The textbook uses three spaces per tab, which I will match. You can use other numbers: 2 and 4 are common.
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)
If you are using Vim, the settings go into .vimrc
in your home
directory. Open it up and paste in the following:
set tabstop=3 set expandtab set softtabstop=3 set shiftwidth=3 set autoindent
The Assignment
3. How this assignment works
Below, you'll find a series of tasks. I've indicated with boldfaced
"EXERCISE" the specific tasks that you are supposed to turn in. Submit via
Moodle all the files you need to turn in. For the programming exercises,
include all the Java files with the code that you have updated. Do not change
the file names. For the "thinking" exercises, include a file called
answers.txt
that contains your answers to those questions.
4. 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 think 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 name]."
5. 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 out a couple of differences.
EXERCISE 3: What happens if you enter a number when prompted for your name or 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.
6. 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 it out.
7. 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 (public) 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 of NumberListTester
so that the
numbers for each list are sorted before they are printed.
EXERCISE 7: Change the main
method of
NumberListTester
so that instead of three different variables
(list1
, list2
, list3
), an array of
NumberList
s is created and used. Take out the code duplication and
instead put it inside a loop.
EXERCISE 8: Add a (private) static variable to the class
NumberList
which keeps track of how many NumberList
objects have been
instantiated. Provide a (public) static method to print out the value of this
variable. Call that static method from main
.
8. Epilogue
Each exercise is worth 2 points, for a total of 16 points. Submit your files electronically on Moodle. Submit modified versions of the 5.java
files (do not change file names!)
and submit answers.txt
with your answers to the thinking exercises.
You may either submit these files individually or as a zip
or tar
archive.
Do not submit .class
files.