Except where otherwise noted, the contents of this document are Copyright 2012 Stuart Reges and Marty Stepp.
lab document created by Marty Stepp and Stuart Reges
Goals for today:
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.
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.
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.
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. \'"\\"
Recall the syntax for writing a static method. Methods are useful for representing a program's structure and capturing common code to avoid redundancy:
public static void name() { statements; }
Example:
public static void song() { System.out.println("This is the song that never ends,"); System.out.println("Yes, it goes on and on, my friends."); }
FightSong
Go, team, go! You can do it. Go, team, go! You can do it. You're the best, In the West. Go, team, go! You can do it. Go, team, go! You can do it. You're the best, in the West. Go, team, go! You can do it. Go, team, go! You can do it.
The following program produces the output at left, but it has poor structure and redundancy. Download it and open it in jGRASP, then add at least two static methods.
continued on the next slide...
FightSong
, cont'dGo, team, go! You can do it.
Go, team, go! You can do it. You're the best, In the West. Go, team, go! You can do it.
Go, team, go! You can do it. You're the best, In the West. Go, team, go! You can do it.
Go, team, go! You can do it.
Did you choose your methods well? Avoid the following pitfalls:
println
statement occur more than once in your code?
println
statements in the main
method?
main
method a good summary of the overall program? (If main
is only 1-2 lines long, it is not a good summary.)
Please ask your TA or neighbor to check out your methods if you're not sure whether you made good choices.
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.)
Lanterns
Lanterns
that produces the
following output. Use static methods to capture structure and remove
redundancy. (Check your output on the Comparison Tool.)
***** ********* ************* ***** ********* ************* * | | | | | * ************* ***** ********* ************* ***** ********* ************* ***** * | | | | | * * | | | | | * ***** *****
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
The giant letters should flow vertically. Use methods to avoid redundancy when the same giant letter appears multiple times in your name. (Use only the material from Chapter 1.)
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!