Except where otherwise noted, the contents of this document are Copyright 2010 Stuart Reges and Marty Stepp.
lab document created by Whitaker Brand and Marty Stepp
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 our first 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("in CSE 142. 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 taking CSE 142. I hope it is a lot of fun! The teachers are named Benson and Marty. I hope they give me a grade of 4.0!
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
The following program has poor indentation. Paste it into jGRASP and fix it.
public class Icky { public static void main(String[] args) { System.out.println("Properly indented programs"); System.out.println("look ever so much better!"); System.out.println("please fix me"); System.out.println("and make me beautiful again"); } }
In CSE 142 you can use a web page called the Indenter Tool to automatically correct the indentation of a program.
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.)
Shaq is 7'1 The string "" is an empty message. \'"\\"
Write a complete Java program 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 Comparison Tool web page.)
\/ \\// \\\/// ///\\\ //\\ /\
(Use only the material we have learned so far. You can check your output on the Comparison Tool web page.)
M M IIIII SSSSS PPPPPP MM MM I S S P P M M M M I S P P M M M I SSSSS PPPPPP M M I S P M M I S S P M M IIIII SSSSS P
(Use only the material from Chapter 1. You can check your output on the 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 Chapter 1 and try to solve them!