| CSC 207 | Algorithms and Object Oriented Design | Spring 2010 |
Summary: In this lab, you will run and modify your first Java programs.
$ mkdir -p somewhere/CSC207/labs(The
-p switch makes all the parent
directories along the way for you.) Presumably, you'll want
to use this directory when we start creating other
directories for future labs.
$ mkdir somewhere/CSC207/labs/intro
$ cd somewhere/CSC207/labs/intro
$ cp ~weinman/public_html/courses/CSC207/2010S/labs/code/introjava/First.java .
$ javac First.java
.class file
called First.class. Run this program using the Java virtual machine:
$ java First(Note that the extension is omitted.)
First.java in emacs:
$ emacs First.java &
Hello in First.java to a word
or phrase of your choice.
First.java
called Second.java. Do not make any changes.
$ cp First.java Second.java
Second.java
$ javac Second.java
Second.java so that you can run it. Remember
that the name of a class must match the name of the file.
Second.java so that it prints two lines:
Hello Goodbye
Second.java.
println
in Second.java. Replace the
first println with print.
println in Second.java to
print.
pen.flush() after the two calls to
pen.print(...) and see what difference this makes.
pen.flush() with
pen.close(). Rerun the program to see what difference
that change makes.
flush.
Read the documentation for close.
Praise the authors for their clarity.
flush and
close.
import packagename.ClassName;
You may then use just ClassName in the rest of the program.
java.io.PrintWriter in
Second.java with PrintWriter.
Do not add the import statement above. What do you expect to
happen when you try to compile and run Second?
Odds.java.
main method that
is invoked when your program is run from the command line.:
public static void main(String[] args)
{
} int Integer.parseInt(String s)int for you.
$ java Odds 10should produce
1 3 5 7 9
abcdxyz?
10.2 or 11.1?
System.out.print(i + " "); System.out.print(i + ' ');Days.
switch statement depending on the character,
your program should print the full name of day of the
corresponding day of the week (e.g., Monday-Sunday). (See
Section 1.5.8 in the text if you do not remember how to
use switch.)
Note: you can determine the length of the first argument with the expression
args[0].length()int value. You may also
extract the first character from a String variable
(which args[0] is), with the expression
args[0].charAt(0)char value.
Your program should print an error message if it is invoked with
an invalid input.