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, Whitaker Brand and Hélène Martin
Goals for today:
In this exercise, you will meet your neighbors and write a program that displays facts about them.
Create a complete Java program in a class named Neighbors
.
Introduce yourself to two people around you (get up to do so if you have no immediate neighbors). Ask them for their full name, including their middle initial. Make sure you know how to spell their name correctly! Ask them for an interesting fact about themselves.
Your program should output the name and interesting fact of two of your neighbors. You may put all of your code in the main
method.
Your output should look like the following:
Talib K. Greene's mother is an English professor in New York. Jermaine L. Cole was born in Germany.
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"); } } |
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."); } }
Download a program from lecture and 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.
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.)
Compare your method choice with your neighbor's. Discuss any different decisions you made and ask a TA if you have questions about them.
Your homework must match expected output exactly. You can use our Output Comparison Tool web page to check your output.
In this exercise, we will check whether a program produces correct output. Download the program and open it in your jGRASP editor.
continued on the next slide...
WrongOutput.java
. Copy/paste the program's output from jGRASP's console into the "Actual Output" box of the Output Comparison web page.
Which output line number does not match? |
4 |
What should be the output for that line? |
"Four score and seven years ago, |
Which expected blank line number is missing? |
8 |
Recall that Java has expressions to represent math and other computations. Expressions may use operators, which are evaluated according to rules of precedence. Every expression produces a value of a given type.
Type | Description | Example | Result |
---|---|---|---|
int |
integers (up to 231 - 1) | 3 + 4 * 5 |
23 |
double |
real numbers (up to 10308) | 3.0 / 2.0 + 4.1 |
5.6 |
String |
text characters | "hi" + (1 + 1) + "u" |
"hi2u" |
Write the results of each of the following expressions. If you're stuck, ask a TA or neighbor.
12 / 5 + 8 / 4
|
4 |
|
3 * 4 + 15 / 2
|
19 |
|
-(1 + 2 * 3 + (1 + 2) * 3)
|
-16 |
|
42 % 5 + 16 % 3
|
3 |
|
2.5 * 2 + 17 / 4
|
9.0 |
|
4.5 / 3 / 2 + 1
|
1.75 |
Write the results of each of the following expressions.
5 * 6 / 4 % 3 - 23 / (14 % 6)
|
-10 |
|
30 % 9 + 5 % 8 - 11 % 4 % 2
|
7 |
|
1 + 9 / 2 * 2.0
|
9.0 |
|
46 / 3 / 2.0 / 3 * 4/5
|
2.0 |
|
50 / 9 / 2.0 + 200 / 10 / (5.0 / 2)
|
10.5 |
continued on the next slide...
In this exercise, you'll use the Interactions Pane to quickly discover the result of some expressions that would be difficult to evaluate by hand. Copy/paste each expression below into the Interactions Pane to evaluate it, then input the answer into this slide.
123 * 456 - 789
|
55299 |
|
3.14 + 1.59 * 2.65
|
7.3535 |
|
2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2
|
1024 |
|
2 + 2 + "xyz" + 3 + 3
|
"4xyz33" |
(For the last expression, the Interactions Pane doesn't put ""
quotes around Strings when displaying results, so you must add those yourself if needed. For example, if the Interactions Pane gives you a result of abc123
, it should be written here as "abc123"
.)
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. \'"\\"
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!