System
? This is a conundrum for Java programmers. After all, the following code has ten System.out.print
lines. Shouldn’t your computer remember what out.print
means?importjava.util.Scanner;
<strong>import static java.lang.System.in;</strong>
<strong>import static java.lang.System.out;</strong>
classTwoTeams {
public static void main(String args[]) {
Scanner keyboard = new Scanner(<strong>in</strong>);
inthankees, socks;
out.print("Hankees and Socks scores? ");
hankees = keyboard.nextInt();
socks = keyboard.nextInt();
out.println();
if (hankees> socks) {
<strong>out.print("Hankees: ");</strong>
<strong>out.println(hankees);</strong>
<strong>out.print("Socks: ");</strong>
<strong>out.println(socks);</strong>
} else {
<strong>out.print("Socks: ");</strong>
<strong>out.println(socks);</strong>
<strong>out.print("Hankees: ");</strong>
<strong>out.println(hankees);</strong>
}
keyboard.close();
}
}
Of course, computers don’t work that way. If you want a computer to “know” what out.print
means, you have to code that knowledge somewhere inside the Java compiler.
Fortunately, the ability to abbreviate things like System.out.print
is available from Java 5.0 onward. This ability to abbreviate things is called static import. It’s illustrated in the second and third lines of the code above.
Whenever you start a program with the line
import static java.lang.System.out;
You can replace System.out
with plain out
in the remainder of the program. The same holds true of System.in
. With an import declaration near the top, you can replace new Scanner(System.in)
with the simpler new Scanner(in)
.
You may be wondering what all the fuss is about. If you can abbreviate java.util
.Scanner by writing Scanner
, what’s so special about abbreviating System.out
? And why do I have to write out.print
? Can I trim System.out.print
to the single word print
? Look again at the first few lines. When do you need the word static
? And what’s the difference between java.util
and java.lang
?
Before you can fix these issues, you need to understand classes, packages, and static members.
Until then, just paste three import declarations to the top of your Java programs and trust that everything will work well.
You can abbreviate System.out
with the single word out
. And you can abbreviate System.in
with the single word in
. Just be sure to copy the import
declarations exactly as you see them above. With any deviation, you may get a compiler error.
OOPS!
What's wrong with the following code? How can the code be fixed?System.out.println("How many donuts are in a dozen?");
int number = keyboard.nextInt();
if (number = 12) {
System.out.println("That's correct.");
} else {
System.out.println("Sorry. That's incorrect");
}
Don’t write code this way
When the following code was written, it wasn't indented properly. What's the output of this bad code? Why?int n = 100;
if (n > 100)
System.out.println("n is big");
System.out.println("Will Java display this line of text?");
if (n <= 100)
System.out.println("n is small");
System.out.println("How about this line of text?");
The world smiles with you
Write a program that asks the users whether they want to see a smiley face. If the user repliesY
(meaning “yes”), the code displays this::-)
Otherwise, the code displays this:
:-(
Successive IF statements
Modify the previous program (the smiley face program) to take three possibilities into account:- If the user replies Y (meaning “yes”), the code displays :-).
- If the user replies N (meaning “no”), the code displays :-(.
- If the user replies ? (meaning “I don't know”), the code displays :-|.
if
statements, one after another.
Guessing game
Write a program that randomly generates a number from 1 to 10. The program then reads a number that the user enters on the keyboard. If the user's number is the same as the randomly generated number, the program displaysYou win!
. Otherwise, the program displays You lose
.
Converting lengths
Write a program that reads a number of meters from the keyboard. The program also reads a letter from the keyboard. If the letter isc
, the program converts the number of meters into centimeters and displays the result. If the letter is m
, the program converts the number of meters into millimeters and displays the result. For any other number, the program doesn't display any result.
Putting several statements inside an IF statement
Find a short poem (maybe four or five lines long). Write a program that asks the users whether they want to read the poem. If the user repliesY
(meaning “yes”), display the poem in Eclipse's Console view. If the user's reply is anything other than Y
, display the following:Sorry!
I thought you were a poetry buff.
Maybe you'll want to see the poem the next time you run this program.