To get started, create a directory called lab2
. Feel
free to go back and peek at the lab from last time if you need to jog
your memory on how to do so.
Click on the file Simple.java
and save it in your "lab2" directory. Open up the file in NEdit. The
parts of the program are labeled. Take a couple of minutes to look at
the program; see if you can figure out what it does.
Before we can run it, we need to convert this text into
bytecode (a format that Java can read and interpret). We do
this by compiling the program (running a special program that
converts text into bytecode). Java's compiler program is named
javac
. To compile this program, type:
javac Simple.java
The program should compile without returning any messages. (Any messages you see are errors). If you do get errors, get help from from the instructor or the prefector.
Once you've compiled the program successfully, do an
ls
. You should see a new file in the directory, named
Simple.class
. This is the "executable" (bytecode) file
that you will run. To run the program, type:
java Simple
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 CS117, [your name and your partner's name]!".
In general, to compile a Java program type javac [filename]
, and to run a Java program type java [classname]
, where [classname] is the name of the class file, minus the .class
extension.
Most Java programs look pretty similar. They all have the following format:
Simple.java
does not contain any data, but it does
contain one method. That method is called main
.
main
is a special method in Java; it is always the one
that the computer looks for to see how to run your program.
EXERCISE 2: What action does the main
method in Simple.java
perform?
Now download the file Simple2.java
into your
lab2
directory. Simple2.java
is very
similar to Simple1.java
; the difference is that
Simple2.java
uses data. The line that starts with
String
is our data line. This line is an example of a
variable declaration, which is a fancy way of saying "here's
some data that we will use; it's a String (of characters), its name is
'message', and its value is 'Are we having fun yet?."
EXERCISE 3: Modify the message that the program writes out to the screen in Simple2.java
by changing the value of the variable.
The programs we've seen so far contain "output" statements, because they each write something to the screen. There are several ways to do output in Java; we're just going to look at one today.
The output of these programs is what we call "console output",
because data is printed out to the console (another name for the
terminal or xterm). In order to do console output, we use one of
Java's many built-in classes. (One of the nice things about Java is
that, not only can we write our own classes, but we can use other
classes in our programs, either ones we write or ones that Java
provides.) This class is the System
class.
System
contains attributes and methods for doing various
things, such as writing out data and errors to the console, exiting a
program, etc. The System
class contains an object named
out
, which knows how to write to the console. Finally,
println()
is a method (action) associated with the
out
object. So, System.out.println("...")
is
how we indicate that we want to write out something to the console.
Just as there are several ways to output data in Java, there are also several ways to input data to Java. We'll look at one today and one later on in the term.
Save Simple3.java
to your lab2
directory. Compile and run the program. What do you see this time?
Now look at the program in NEdit. You should notice several
differences between this program and the previous program. First, we
are "importing" all of the classes in the java.util
package. (A "package" in Java is a set of related classes that are
pre-written for us.) The java.util
package contains
"utility" classes that are useful to many types of programs, such as a
random number generator and classes that deal with groupings of
items. The class that we're using here is the Scanner
class, which handles getting input from the user in various ways
(console, file, etc.). Second, we have two more variables that we've
declared. These variables will store whatever the user types in to the
console window (in this case, your name and your partner's name).
Third, we have a statement that contains the word new
.
Statements that contain the word new
are used to create
objects. Here, we're creating an object from the Scanner
class. In most cases, we need to create an object before we can use
it; we create a Scanner
object, name it "input", and use
this object to read in data from the console (that the user has typed
in). (We'll talk about this more in class, so hang in there.)
Now make the following changes to the code:
name2
to age
, and change its type from String
to int
.System.out.println("Enter your age: ");
age = input.nextInt();
"Your name is " + name1 + " and you are " + age + " years old. Welcome!"
Compile and run the program.
EXERCISE 4: What happens if you enter a number when prompted for your name and a name when prompted for your age? If you get a message, what did the message say? In words, explain what you might do as a programmer to prevent a user from entering incorrect input. (I'm not looking for an exact answer, just a general idea as to the approach you'd take.)
Submit your answers to these exercises, and the ones above, via
hsp
. (Note: you can submit entire directories using
hsp
, so it may be easiest to submit your entire "lab2"
directory.) Answers to the questions should be in a text file named
"lab2.txt". You should work on and submit the answers together with
your partner. Only one of you should submit your lab with
hsp
, but make sure that you indicate in the
lab2.txt
both of your names.
Simple3.java
again so that it asks for the
names of you and your partner and both of your ages, and displays the
information on separate lines. Save this program as Simple4.java
.Simple.java
, and replace System.out.println(...)
with System.out.print(...)
. What is the difference between the println()
and print()
methods?Simple4.java
by doing the following:
input.next()
to get input from the user, and sometimes we use input.nextInt()
. Based on what you observed, what is the difference between input.next()
and input.nextInt()
?Instructions for using hsp
are here.
Authored originally by Amy Csizmar Dalal. Modified by Dave Musicant.