To get started, create a directory called
introjava. Feel free to go back and peek at the UNIX
tutorial if you need to jog your memory on how to do so. Use the
cd command to navigate to the introjava directory
that you have just created.
Start up jedit, which is the text editor that we will be using to create and modify Java programs. To do so, type jedit & (don't forget the ampersand) in your terminal window. Play around with it a little bit: try to type some text, save it, close jedit, and see if you can open it again.
Look at the program Hello.java. In jedit, type in this
program exactly. You might find it easiest to put the two windows
side-by-side on your computer screen, or perhaps you might wish to
print out Hello.java first and work from the printout. Don't copy
and paste the code. You'll learn more if you go through the experience
of typing it in and fixing any errors. Save your file under the name
Hello.java.
Before we can run your program, we need to compile it (translate it to a language the computer can understand). To compile this program, type:
javac Hello.java
in the terminal. Hopefully, the program should compile without returning any messages. (Any messages you see are errors.) If you do get errors, get help from from the prof, the prefect, or a lab assistant.
Once you've compiled the program successfully, do an
ls. You should see a new file in the directory, named
Hello.class. This is the "bytecode" file that you will
run. To run the program, type:
java Hello
Notice that you do not include the .class extension when
you run a Java program.
EXERCISE 1: Modify the program so that it prints out "Welcome to Intro CS, [your name]."
This time, look at Input.java. Copy and paste the code
into jedit (or type it in if you like), and save the file to your
introjava directory. Compile the program and run it a
couple of times. (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. Type
your answer into a file called introjava.txt that you
also store in your introlab directory.
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? Put your answers in your introjava.txt file.
EXERCISE 4 (not required, but go for it if you can): 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.
Submit all of your work using the homework submission
program, which is referred to as hsp. (Note: you
can submit entire directories using hsp, so it may be
easiest to submit your entire introjava directory.)
Instructions for using hsp are here.
Note that hsp will not work unless you are officially
registered for the course in the registrar's systems. If you are
working from home, transfer your work to your department Linux account
so that you can test it there before submitting.
Authored by Dave Musicant and Amy Csizmar Dalal.