|
Choices in Teaching Java: Advice
|
Should you teach objects early? Should you teach applets or
applications? What's the difference between an applet
and an application anyway?
Every teacher is forced to choose a "Java teaching style". Loosely
speaking, this choice is about going traditional (similar
to teaching C or Pascal) or going non-traditional (making use of, say,
graphics). Some people favor continuing the traditional approach
with Java and postponing discussion of both objects and graphics
until after students have gotten their feet wet with simple
programming. Others are horrified by the traditional approach
and prefer to go the "objects-first, graphics-first" approach.
And there's a whole spectrum of opinion in between.
Even after deciding when to introduce graphical input/output,
there is a choice to be made between "applets" (Java programs
that are embedded in webpages) and "applications" (Java programs
that run by themselves on a PC).
Suggested steps:
So Many Choices - What are the pros and cons?
Let's go through the options one by one, starting with
commandline vs. GUI:
- Arguments in favor of the command-line approach:
- It's simpler to understand.
- Programs are smaller, easier to display in class.
- It's a familiar paradigm.
- Overly cute GUI stuff can distract from fundamentals.
- You don't have to deal with the many GUI options.
- APCS doesn't require GUI's, so why add this component?
- There are plenty of books taking this approach.
- Arguments in favor of the GUI-approach:
- It may be harder to understand everything about
GUI code, but who says it needs to be all understood? You can do
plenty of interesting GUI programming without a detailed understanding.
(More about this later).
- GUI's are interesting! How else do you keep students engaged?
- Java was written with GUI's in mind, so why waste time with
"old-world" programming. Students are going to use GUI's anyway,
as does the real world.
- The many GUI options are all usable, so there's no need to
fret over choice - any one option will do.
- APCS rightly focuses on fundamental principles; it would be
a mistake to have exam questions on GUI's. However, if GUI's
are a good way to get students interested early, what's wrong with
using this approach to achieve the end objective of learning
the fundamentals?
- There are plenty of books taking this approach.
There you have it - the two sides in a battle that has not been
settled yet. Universities use both approaches, sometimes even within
the same university.
Now let's consider "Objects-first" vs. "Objects-later":
- Arguments in favor of "Objects-later":
- We don't teach loops before conditionals because we believe
loops are more complex. Similarly, objects should be taught
later because they are more complex.
- Objects combine ideas (data, methods) that should precede
the teaching of objects.
- There's plenty to challenge the average student well before
getting to objects.
- There are too many new concepts related to objects
(inheritance, polymorphism, constructors) that are best digested
after mastering simpler statements (like "if", "while" etc).
- Arguments in favor of "Objects-first":
- Java was written for OOP; all of the library uses OOP. You
can't avoid using objects (see command-line input, for example),
so why not jump right in on day one?
- Don't underestimate students' ability to get comfortable with
objects - they seem to have no problem with them.
- It's not necessary to understand every detail regarding
objects. The understanding will come eventually.
- GUI's are all object-based, so better to take the bull by the horns.
- If you provide some software of your own (which many schools
do), it's easy to get students to work on "realistic" and
interesting projects with objects. The non-object approach
typically relies on exercises (e.g., compute the interest payment
on a mortgage) the students find uninteresting.
Next, the GUI options:
- Arguments in favor of applets:
- Applets are the "cool factor" that got Java the world's attention.
- Applets allow students to "show off" their work to "mom" (friends,
parents, anyone who can use a browser).
- Applets can be worked into HTML in creative ways, allowing
students to intersperse text with applets to create a larger "theme"
or "story".
- Students don't need to learn any more HTML than in the
helloworld examples we've seen.
- Applets can be graded from home, office, anywhere. There's no
need to compile and run students' code.
- Arguments in favor of applications:
- Applications do not need any HTML at all.
- Applications can hold their own in "coolness".
- Some browsers do not run applets, others have the option turned
off. Who wants the headache of worrying about browser compatibility?
- Even fewer browsers have automatic support for Swing applets,
so applets tend to be written in AWT, which has fewer
features.
- Most real-world Java code is written as application, so
students ought to know how to do this.
Next, let's consider the various file options in
writing GUI's:
- All in one file:
- Advantages: easy to download, can email a single file to students.
- Disadvantages: non-standard (Java recommends separate files
as a stylistic principle), harder to mix and match code.
- Each class in a separate file:
- Advantages: standard Java style, easier to mix and match code
(see the common application-applet approach in our
helloworld examples).
- Disadvantages: need to explain this to students, students
need to have all files in order to compile.
- Using constructors:
- Advantages: standard Java approach (better get used it),
initializations need not be repeated if multiple instances are
used, not hard to learn after seeing a few examples.
- Disadvantages: might need more explanation, yet another thing
to worry about.
Finally, let's review the event-handling options:
- The
frame-does-it approach. This is the one in which the
Frame implements the listener interface.
- The
separate-listener-class approach. This is the one
in which an entirely separate class (outside the frame) is written
to implement the listener interface. An instance of the class
is created and passed to the input component.
- The
anonymous-class approach. Here, an instance is created
right in the method call to pass the listener.
Advice
OK, there are a lot of options. That's nice, but now what? What do
I choose?
Rather than take sides in these raging debates, we offer the
following advice:
- If you opt for command-line, then be aware of the power
of graphics in motiving students. You might consider introducing
graphics no later than the second month (4th or 5th class).
- If you opt for GUI-first, remember that you don't
need to explain everything to students. For example, consider
this example:
import java.awt.*;
import java.applet.*;
public class DrawApplet extends Applet {
public void init ()
{
this.setBackground (Color.cyan);
this.setSize (500, 300);
this.setVisible (true);
}
public void paint (Graphics g)
{
//////////////////////////////////////////////////////////////
// DO NOT CHANGE CODE ABOVE THIS LINE
// Write your code between this line and the "---" line below
// The command to draw a line: e.g., g.drawLine (10, 10, 50, 50)
//-----------------------------------------------------------
}
}
You can give an exercise to students in which they must draw a square.
All they need to know is that they have to use g.drawLine
commands inside the paint() method. Thus, they will produce:
import java.awt.*;
import java.applet.*;
public class DrawApplet extends Applet {
public void init ()
{
this.setBackground (Color.cyan);
this.setSize (500, 300);
this.setVisible (true);
}
public void paint (Graphics g)
{
//////////////////////////////////////////////////////////////
// DO NOT CHANGE CODE ABOVE THIS LINE
// Write your code between this line and the "---" line below
// The command to draw a line: e.g., g.drawLine (10, 10, 50, 50)
// A square:
g.drawLine (10,10, 10,50);
g.drawLine (10,50, 50,50);
g.drawLine (50,50, 50,10);
g.drawLine (50,10, 10,10);
//-----------------------------------------------------------
}
}
- Objects-first or later?
- Use either approach, but an objects-later approach seems more
compatible with command-line, whereas an objects-first approach
seems more compatible with GUI-first.
- You can definitely postpone discussion of object details
while using them (in GUI examples, for instance).
Students seem to have no trouble with this approach.
- If using a GUI-first approach, it might be better to engage
the students with some problem-solving, rather than heavy
programming of multiple components.
- If using a GUI-first approach, try to include some exercises
that let students be creative. Drawing, coloring, telling a story,
animation are all good examples.
- Applets or Applications?
- If you opt for applets, we advise against using
JApplet's. This is because of the additional difficulty
in getting browsers to support them.
Applet's, on the other hand, are supported by most browsers
(even if they are not as slick).
- With applets, allow students some creativity in adding
HTML to make a "story". Perhaps organize a "best applet"
competition with student-panel judges.
- If you opt for applications, it's probably best to use Swing
directly since this is what the students will end up using, and
this is what the gung-ho students will want.
- Separate files or all-in-one file?
- Initially, it might be best to go with all-in-one-file,
especially if you plan on email submission of homeworks.
- On the other hand, if you are already using an environment
or special package (like Karel robots), there may be enough
separate classes in there to make an all-in-one approach
seem the exception to the rule.
- Which listener approach?
- We recommend using either the frame-does-it or the
anonymous-class approach.