|
Spring Semester 2005
|
Last time we developed the smallest Java program. The empty program:You noticed that we changed its name. That's your degree of freedom.class One { public static void main(String[] args) { } }Let's make it print something:
We will focus on the inside, the part that is clearly visible.class One { public static void main(String[] args) { System.out.println("This is my song for the asking..."); } }What does it look like?
The inside is a sequence of instructions (commands) separated by semicolons.class One { public static void main(String[] args) { System.out.println("This is my song for the asking..."); System.out.println(" and I'll"); System.out.println("Ask me play"); System.out.println(" it"); System.out.println(" so sweetly, I'll"); System.out.println(" make you smile..."); } }What instructions do we know (and could use in our programs so far)?
Only one (print line) and we'll add one in a second: print.
print and println What does the following program do?Let's change the program above to:class One { public static void main(String[] args) { System.out.println("One"); System.out.println("Two"); System.out.println("Three"); System.out.println("Four"); System.out.println("Five"); System.out.println("Six"); } }Soclass One { public static void main(String[] args) { System.out.print("One"); System.out.print("Two"); System.out.print("Three"); System.out.print("Four"); System.out.print("Five"); System.out.print("Six"); } }System.out.print(...)does not move the cursor to the next line.
", \ and newline Consider the following program:Can you achieve the exact same result without usingclass One { public static void main(String[] args) { System.out.println(". . . . . ."); System.out.println(". . . . . ."); System.out.println(". . . . . ."); System.out.println(". . . . . ."); System.out.println(". . . . . ."); System.out.println(". . . . . ."); } }println?Here's some help: what does the following program do?
No, it doesn't print nothing. It prints four characters. (Really? Four?)class One { public static void main(String[] args) { System.out.print("\n\n\n\n"); } }What does the following program do?
What does this program do? Explain.class One { public static void main(String[] args) { System.out.println("\""); System.out.println("\"\""); System.out.println("\"\"\""); System.out.println("\"\"\"\""); } }What's to be done?class One { public static void main(String[] args) { System.out.print("\"\"); } }
Now that we understand printing, what can we print?We can print numbers and expressions with numbers.
Try to predict what the next program will print and then run it:
OK, that's a lot.class One { public static void main(String[] args) { System.out.println(1); System.out.println(1 + 1); System.out.println(2 * 3); System.out.println(1 + 2 * 3); System.out.println(2 * 3 + 1); System.out.println(2 * (3 + 1)); System.out.println(2 / 3); System.out.println(3.14); System.out.println(2.0); System.out.println(2.0 / 3.0); System.out.println(4 * 2 / 3); System.out.println(4.0 * 2.0 / 3.0); System.out.println(2 / 3 * 4); System.out.println(2.0 / 3.0 * 4.0); System.out.println(1 - 2 + 3); System.out.println(1 - (2 + 3)); System.out.println(1 + 2 - 3); System.out.println(1 + (2 - 3)); System.out.println(7 % 3); System.out.println(4 % 5); System.out.println(1 < 2); System.out.println(1 > 2); System.out.println(1 == 2); System.out.println(2 == 2); System.out.println(2 <= 2); System.out.println(2 >= 2); System.out.println("Larry" + "Bird"); System.out.println("a" + "1"); System.out.println("a" + 1); System.out.println("1" + "1"); System.out.println("1" + 1); System.out.println(1 + 1); System.out.println("1" + (1 + 1)); } }Plenty of operators.
Take your time, have patience. Be persistent.
Ask for help and clarification.
Here's how you can remember calculated values for later:Variables are names for values of a particular type.class One { public static void main(String[] args) { int n; n = 1 + 1; System.out.println(n); } }Assignment statements associate a value with a particular name.
The association can change, so that's why the name used as such is the name of a variable.
You could think of it as the name of a location of variable content.
Two more examples will end our first set of experiments:
Variables need to be declared, and initialized.class One { public static void main(String[] args) { int n = 1; n = n + 1; System.out.println(n); } }The value in a variable can be updated.
In an assignment statement the right hand side is calculated first.
When the value of the expression is determined, it replaces the previous value of the variable.
So
nreceives a value of1 + 1 = 2.Note the difference between mathematical notation and the one used in programming.
One last thing:
Why doesn't this work?class One{ public static void main(String[] args) { int n; n = 3.14; System.out.println(n); } }What can we do about it? (Answer: make
nadouble).
Tue Jan 11 09:34:52 EST 2005