Except where otherwise noted, the contents of this document are Copyright 2012 Stuart Reges and Marty Stepp.
lab document created by Marty Stepp, Stuart Reges and Whitaker Brand
Goals for today:
println
statements
Recall from lecture: A Java program must be compiled, or translated into binary instructions. Then it can be executed or run. When you run a program, it displays output messages to the user in a text window called a console.
For this exercise, let's compile and run a short program that we will provide to you. (See the following slides.) If you get stuck, ask a classmate or TA for help.
continued on next slide...
public class MyFirstProgram { public static void main(String[] args) { System.out.println("Hello, world!"); System.out.println(); System.out.println("This is my very first program."); System.out.println("Hooray!"); } }
continued on next slide...
.java
and must match the 'public
class' name from the Java program. Name this
file MyFirstProgram.java
.
continued on next slide...
continued on next slide...
Modify your MyFirstProgram
file to produce the following
console output. Note the blank lines; you should include those in your
output.
Hello, world! I am learning to program in Java. I hope it is a lot of fun! I hope I get a good grade! Maybe I'll change my major to computer science.
In CSE 142 you'll use a web turnin system for your homework. Let's
practice turning in a file by submitting
your MyFirstProgram.java
.
MyFirstProgram.java
file
and submit it.
Part of your homework grades come from producing correct output exactly.
Use our Output Comparison Tool web page to check if your output is correct.
Programs should be indented properly to make them easier to read:
{
brace → increase indent of following lines by one tab
}
brace → decrease indent of that line and following lines by one tab
Example:
public class Hello { public static void main(String[] args) { System.out.println("Hello, world!"); System.out.println("How are you?"); } }
Make sure that your Neighbors
program has good indentation.
continued on the next slide...
Our Indenter Tool web page can fix a program's indentation.
In this exercise, we will use the Indenter Tool to fix the following program that has poor indentation. Download it and open it in jGRASP:
continued on the next slide...
public class Icky { public static void main(String[] args) { System.out.println("Well-indented programs"); System.out.println("look much better."); System.out.println("Please fix me"); System.out.println("so I look nicer"); } } |
public class Tricky { public static void main(String[] args) { System.out.println("Testing, testing,"); System.out.println("one two three."); System.out.println(); System.out.println("How much output"); System.out.println(); System.out.println("will there be?"); } }
System.out.println();
statements do.
1 2 3 4 5 6 7 8 9 |
public class Tricky public static main(String args) { System.out.println(Hello world); system.out.Pritnln("Do you like this program"?); System.out.println() System.println("I wrote it myself."; { } |
answer on next slide...
{
after Tricky
void
before main
[]
after String
"
marks around Hello world
system
should be System
(uppercase S)
Pritnln
should be println
(lowercase P
and fixed spelling)
?
should be before "
()
)
after "
System.println
should
be System.out.println
{
should be }
public class Tricky { public static void main(String[] args) { System.out.println("Hello world"); System.out.println("Do you like this program?"); System.out.println(); System.out.println("I wrote it myself."); } }
Discover what error messages the compiler produces when you make each of the following mistakes. How many unique error messages are you able to cause the compiler to produce?
void
or class
"
(
or )
.
{
or }
Notice that the error messages don't always make it obvious what is wrong. But they usually tell you the right line number to fix.
An escape sequence inserts a special character into
a println
statement.
Sequence | Special character |
---|---|
\n | new-line (goes to the next line) |
\t | tab (indents output by roughly 8 spaces) |
\" | quotation mark |
\\ | backslash |
Example:
System.out.println("I said \"hello\" to Fred.");
System.out.println("Shaq is 7'1"); System.out.println("The string \"\" is an empty message."); System.out.println("\\'\"\\\\\"");
(Try to figure it out without running the code.
If you give up,
paste it into jGRASP and run it.)
Note: when you see a purple check mark as in the upper-right corner of this page, that means that this problem is also available in PracticeIt by clicking on the check mark.
Shaq is 7'1 The string "" is an empty message. \'"\\"
MuchBetter
Write a complete Java program named MuchBetter
that produces the following output (note the
blank line):
A "quoted" String is 'much' better if you learn the rules of "escape sequences." Also, "" represents an empty String. Don't forget: use \" instead of " ! '' is not the same as "
(You can check your output on the Output Comparison Tool web page.)
Spikey
Spikey
that produces the following output:
\/ \\// \\\/// ///\\\ //\\ /\
(Use only the material we have learned so far. You can check your output on the Output Comparison Tool web page.)
If you finish all the exercises, try out our Practice-It web tool. It lets you solve Java problems from our Building Java Programs textbook.
You can view an exercise, type a solution, and submit it to see if you have solved it correctly.
Choose some problems from the book and try to solve them!