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:

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":

Next, the GUI options:

Next, let's consider the various file options in writing GUI's:

Finally, let's review the event-handling options:


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.