JavaCross
How does a crossword puzzle help you learn Java? Well, all of the words
are
Java related. In addition, the clues provide metaphors, puns, and the
like. These mental twists and turns burn alternate routes to Java knowledge,
right into your brain!
Mixed Messages
A short Java program is listed below. One block of the program is missing.
Your challenge is to
match the candidate block of code
(on the left),
with
the outpu
t that you’d see if the block were inserted. Not all the lines of
output will be used, and some of the lines of output might be used more
than once. Draw lines connecting the candidate blocks of code with their
matching command-line output.
Exercise Solutions
Be the JVM:
class Output {
public static void main(String [] args) {
Output o = new Output();
o.go();
}
void go() {
int y = 7;
for(int x = 1; x < 8; x++) {
y++;
if (x > 4) {
System.out.print(++y + " ");
}
if (y > 14) {
System.out.println(" x = " + x);
break;
}
}
}
}
Code Magnets:
class MultiFor {
public static void main(String [] args) {
for(int x = 0; x < 4; x++) {
for(int y = 4; y > 2; y--) {
System.out.println(x + “ “ + y);
}
if (x == 1) {
x++;
}
}
}
}
Puzzle Solutions
1.
1. Dive in A Quick Dip: Breaking the Surface
a.
The Way Java Works
b.
What you’ll do in Java
c.
A Very Brief History of Java
i.
Speed and Memory Usage
ii.
Sharpen your pencil Answers
d.
Code structure in Java
i.
What goes in a source file?
ii.
What goes in a class?
iii.
What goes in a method?
e.
Anatomy of a class
f.
Writing a class with a main
g.
What can you say in the main method?
i.
Looping and looping and...
ii.
Simple boolean tests
h.
There are no dumb Questions
i.
Example of a while loop
j.
Conditional branching
k.
Coding a Serious Business Application
i.
Monday Morning at Bob’s Java-Enabled House
l.
Phrase-O-Matic
m.
Code Magnets
i.
BE the compiler
n.
JavaCross 7.0
o.
Pool Puzzle
p.
Exercise Solutions
q.
puzzle answers
2.
2. Classes and Objects: A Trip to Objectville
a.
Chair Wars
i.
(or How Objects Can Change Your Life)
ii.
In Larry’s cube
iii.
At Brad’s laptop at the cafe
iv.
Larry thought he’d nailed it. He could almost feel
the rolled steel of the Aeron beneath his...
v.
Back in Larry’s cube
vi.
At Brad’s laptop at the beach
vii.
Larry snuck in just moments ahead of Brad.
viii.
Back in Larry’s cube
ix.
At Brad’s laptop on his lawn chair at the Telluride
Bluegrass Festival
x.
So, Brad the OO guy got the chair and desk,
right?
b.
What about the Amoeba rotate()?
c.
The suspense is killing me. Who got the chair and desk?
i.
Brain Power
d.
When you design a class, think about the objects that will
be created from that class t ype. Think about:
e.
What’s the difference between a class and an object?
i.
A class is not an object.
f.
Making your first object
g.
Making and testing Movie objects
h.
Quick! Get out of main!
i.
The Guessing Game
i.
Running the Guessing Game
j.
There are no Dumb Questions
i.
BE the compiler
k.
Code Magnets
i.
Pool Puzzle
l.
Exercise Solutions
m.
Puzzle Solutions
i.
Pool Puzzle
ii.
Who am I?
3.
3. Primitives And References: Know Your Variables
a.
Declaring a variable
i.
variables must have a type
ii.
variables must have a name
b.
“I’d like a double mocha, no, make it an int.”
i.
Primitive Types
c.
You really don’t want to spill that...
d.
Back away from that keyword!
e.
Controlling your Dog object
f.
An object reference is just another variable value.
i.
The 3 steps of object declaration, creation and
assignment
g.
There are no Dumb Questions
i.
Java Exposed
h.
Life on the garbage-collectible heap
i.
Life and death on the heap
ii.
An array is like a tray of cups
iii.
Arrays are objects too
iv.
Make an array of Dogs
v.
Control your Dog
vi.
What happens if the Dog is in a Dog array?
vii.
A Dog example
viii.
BE the compiler
ix.
Code Magnets
i.
Pool Puzzle
j.
A Heap o’ Trouble
i.
The case of the pilfered references
k.
Exercise Solutions
l.
Puzzle Solutions
i.
The case of the pilfered references
4.
4. methods use instance variables: How Objects Behave
a.
Remember: a class describes what an object knows and
what an object does
i.
Can every object of that type have different
method behavior?
ii.
The size affects the bark
iii.
You can send things to a method
b.
You can get things back from a method.
c.
You can send more than one thing to a method
i.
Calling a two-parameter method, and sending it
two arguments.
d.
There are no Dumb Questions
i.
Reminder: Java cares about type!
e.
Cool things you can do with parameters and return types
f.
Encapsulation
i.
Do it or risk humiliation and ridicule.
ii.
Hide the data
g.
Java Exposed
h.
Encapsulating the GoodDog class
i.
How do objects in an array behave?
i.
Declaring and initializing instance variables
j.
The difference between instance and local variables
k.
There are no Dumb Questions
l.
Comparing variables (primitives or references)
i.
BE the compiler
m.
Mixed Messages
n.
Pool Puzzle
i.
Fast Times in Stim-City
o.
Exercise Solutions
p.
Puzzle Solutions
5.
5. Writing a Program: Extra-Strength Methods
a.
Let’s build a Battleship-style game: “Sink a Startup”
b.
First, a high-level design
c.
The “Simple Startup Game” a gentler introduction
d.
Developing a Class
e.
Brain Power
f.
SimpleStartup class
g.
Writing the method implementations
i.
let’s write the real method code now, and get this
puppy working.
h.
Writing test code for the SimpleStartup class
i.
There are no Dumb Questions
j.
The checkYourself() method
k.
Just the new stuff
l.
There are no Dumb Questions
m.
Final code for SimpleStartup and SimpleStartupTester
n.
Prepcode for the SimpleStartupGame class
o.
The game’s main() method
p.
random() and getUserInput()
q.
One last class: GameHelper
i.
Let’s play
ii.
What’s this? A bug ?
r.
More about for loops
i.
Regular (non-enhanced) for loops
s.
Trips through a loop
i.
Difference between for and while
t.
The enhanced for loop
u.
Casting primitives
i.
BE the JVM
v.
Code Magnets
w.
JavaCross
i.
Mixed Messages
x.
Exercise Solutions
i.
Puzzle Solutions
Do'stlaringiz bilan baham: |