| CSC 207 | Algorithms and Object Oriented Design | Spring 2009 |
Summary: In this lab, you will run and modify your first Java programs.
mkdir somewhere/CSC207/labsPresumably, 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/2009S/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)
will convert a string to an int for you.
java Odds 10should produce
1 3 5 7 9
abcdxyz?
10.2 or 11.1?
System.out.print(i + " ");
(If you are using two different print statements to accomplish
this, be sure you understand how the above works before going on.)
Change your program so that it instead has (something similar to) the line:
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).
Note: you can determine the length of the first argument with the expression
args[0].length()
which will evaluate to an int value. You may also
extract the first character from a String variable
(which args[0] is), with the expression
args[0].charAt(0)
which will evaluate to a char value.
Your program should print an error message if it is invoked with
an invalid input.