ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Teaching Java the Extreme Way

by Daniel H. Steinberg

This is the second in a series of articles that looks at how programming in Java is taught to new programmers. In the first article, I looked at the traditional first program, HelloWorld. I argued that although it was a good "systems check," it was not a good first program for a course in Java or in object-oriented programming. Despite the C-like syntax, we should not teach Java as if it were C with objects. Objects really need to come first.

Your feedback was wonderful. Take a look back at last month's article and the accompanying talkback and feel free to join in any of the threads. Many people provided suggestions for tools and approaches that were thought-provoking. There were some who thought that we shouldn't be teaching objects first (you need to teach pointers and memory allocation) and some who suggested that Java was a toy language that is fine for teaching but not for serious development. Students and developers should learn about memory and they should learn more than one language and more than one approach. OOP in Java provides a solid introduction to programming. This series will continue to explore taking an extreme programming approach to this introductory course.

A group of responses felt that OOP is just a layer on top of procedural programming, and that method calls are, at their core, the same as procedure calls. In a future article, we'll focus more on the concept of "an object is at least a certain type." A polymorphic method call means that you are making a procedure call -- you're just making it without knowing which object or even which type of object will respond. At compile time, the object that you end up calling at runtime may not exist.

In This Series

Cooking with Java XP, Part 3
Here are more sample recipes from Java Extreme Programming Cookbook. From Chapter 8 ("JUnitPerf"), learn how to create a load test; and from Chapter 9 ("XDoclet"), find out how to execute a custom template.

Cooking with Java XP, Part 2
In these two recipes, excerpted from Chapter 6 of Java Extreme Programming Cookbook, learn how to create a mock implementation of an event listener interface, and how to avoid duplicated validation logic in your tests. And check back here next week for recipes on creating a load test and executing a custom template.

Cooking with Java XP
In this recipe from Chapter 5 of Java Extreme Programming Cookbook, learn how to configure your development environment to support test-first development with HttpUnit, JUnit, Tomcat, and Ant.

Top 12 Reasons to Write Unit Tests
Most programmers do not write tests. This is unfortunate, because testing improves software quality and design, reduces bugs, and provides accurate documentation. But if those reasons don't sway you, here are 12 more on the importance of writing tests, brought to you by Eric M. Burke and Brian M. Coyner, coauthors of Java Extreme Programming Cookbook.

Rethinking the Java Curriculum: Goodbye, HelloWorld!
How do we train students in the ways of object-oriented programming? Teaching them C first and then porting to Java is obviously the wrong thing to do. And HelloWorld gets very few OO points. In this series, Daniel Steinberg applies Extreme Programming concepts to the problem of teaching Java.

In this article, we'll start looking at a test-first approach to programming and to learning. In extreme programming you have a task you want to accomplish. Within that task, you choose a small measurable result. You think about how you would verfiy that you've accomplished that result. You write the test and then you write the code that will pass the test.

In this article, I'll use an ad hoc framework for this process. Next, I'll use JUnit, but I want to separate what's being done from the tools used to do it. Please join the discussion at the end of each article and feel free to suggest future topics in the forum or by emailing me at DSteinberg@core.com.

HelloWorld: Your First Java Program

Fundamentally, object-oriented programming is all about objects communicating with each other through their visible interfaces. You will learn to be stingy about what you expose to the outside world. Ideally, you will expose very little about the object's structure and only reveal those behaviors you are allowing others to invoke.

In a first programming assignment, I would like to see the newbie accomplish the following tasks:

  • Learn to use an editor and a compiler to create Java classes.
  • Create a new Java class that can be instantiated in a specified way.
  • Create a public method in this class that conforms to a specified interface.
  • Ensure that this method returns your first name in a format that I can use.

As an example, consider the following two lines of code:

Friend friend = new Friend(); 
String yourName = friend.getName(); 
// somehow I will display yourName once I have it

Your first programming task is to write the code that makes those two lines work. Sure, this seems trivial to you now. You quickly whip up a file, Friend.java, that looks like this:

public class Friend { 
   public String getName(){

       return "Daniel";

You compile it and somehow run the program and you expect to be greeted by name. Again, it's hard to remember back to when you didn't know anything. There's a lot here. I taught freshman calculus for a dozen years or so; on the last day of each semester I would write all that we had learned that semester on the board and step back. There, on a pair of blackboards at the front of the room, was everything we had covered over the past fifteen weeks. Looking back, it didn't look like much -- but it had taken us an entire semester and countless blackboards to get to this point. I'm asking you to go back to when you didn't know how to program and think about this assignment.

Pages: 1, 2, 3

Next Pagearrow