|
|
|
This lab is intended to get everyone started using the Java tools. This
lab must be completed in the lab time, and demonstrated to the TA. For this
lab only, the TA should see the following:
- Your code, in an appropriately named text file.
- Your code being compiled successfully on the command line.
- Your compiled code being run, with the correct output.
The lab 7050 has Crimson Editor installed along with the JDK. You
should compile and run your code using the Windows Command Prompt (found
in the Start menu under All Programs > Accessories).
The lab tasks are as follows:
- Using Crimson Editor, create a java program called
HelloWorld.java that
prints the words "Hello World" on the screen. Compile your code with the
command javac HelloWorld.java and run your program using the command
java HelloWorld. Note that the actual code required is available
on the examples page
- Create a program called Parity.java
that initializes
an integer variable to some value, and then uses an if/else statement to
print "the number is even" or "the number is odd" as appropriate. So if the
initial number is 4, the program should output:
4 is even
Remember that
x % y gives the remainder when x is divided by y, so you can
check if x is even by looking at the value of x % 2.
|
- Make a new program called InputParity.java that uses
the Scanner class to get the user to enter a number, then outputs "the number is even" or
"the number is odd" as appropriate.
Note: To use Scanner, you need to include the line import
java.util.Scanner as the first line of your source file. See the scanner example.
|
- Make a new program called LoopParity.java that uses a Loop
statement to print the numbers 1 up 5, indicating whether each is even or odd. In other
words, the program should output:
1 is odd
2 is even
3 is odd
4 is even
5 is odd
|
|