Motivation
Imagine that you're in charge of a sports application. You maintain the teams from one sport (such as baseball) in an ArrayList, and maintain teams from another sport (maybe football) in an ordinary Java array. With your access to both teams' data structures you to manage all data in the application. Listing 1, Listing 2, and Listing 3 show rudimentary code.
Listing 1 shows a base class, Sports, which handles the crux of sports-related activities (i.e., getters and setters) for team information such as team name, number of wins, losses, ties, and winning percentage (which is automatically calculated). The derived classes Baseball and Football, shown in Listing 2 and Listing 3, use super() for object creation.
Notice the difference between the Baseball and Football classes. The Baseball class defines its own toString() method. Sports fans know that there are no ties in baseball. (Do you remember the classic 24-inning games of the past?) So the Baseball class' toString() method doesn't call getTie(). Also, the Baseball class's second constructor has no tie parameter. The constructor sneaks the value 0 for tie in a super() call.
In Listing 4, the BaseballTeams class collects Baseball objects into a group of some kind. In Listing 5, the FootballTeams class does the same for Football objects.
In the real world, you'd store team information in a database. But this simple example parses small comma-separated value (CSV) filesthe files mlb2006.csv and nfl2006.csv named in Listing 4 and 5. Building the teams for each sport is relatively straightforward: You read the CSV files into a Java BufferedReader, parse the data using a StringTokenizer, and then store the data in two data structures (an ArrayList for baseball and a Java array for football).
Listing 6 shows a client application.
| Author's Note: You might be wondering why we aren't using the final 2007 standings. After all, this is 2008! Unfortunately, Mike is still smarting from the Mets September 2007 collapse and it would pain him to see the Mets listed in second place. |
Running the application produces the following output as shown in Figure 1.
### Major League Baseball 2006 ###
New York Yankees 97 65 0.599
Toronto Blue Jays 87 75 0.537
Boston Red Sox 86 76 0.531
Baltimore Orioles 70 92 0.432
Tampa Bay Devil Rays 61 101 0.377
Minnesota Twins 96 66 0.593
Detroit Tigers 95 67 0.586
Chicago White Sox 90 72 0.556
Cleveland Indians 78 84 0.481
Kansas City Royals 62 100 0.383
Oakland A's 93 69 0.574
Los Angeles Angels 89 73 0.549
Texas Rangers 80 82 0.494
Seattle Mariners 78 84 0.481
New York Mets 97 65 0.599
Philadelphia Phillies 85 77 0.525
Atlanta Braves 79 83 0.488
Florida Marlins 78 84 0.481
Washington Nationals 71 91 0.438
St. Louis Cardinals 83 78 0.516
Houston Astros 82 80 0.506
Cincinnati Reds 80 82 0.494
Milwaukee Brewers 75 87 0.463
Pittsburgh Pirates 67 95 0.414
Chicago Cubs 66 96 0.407
San Diego Padres 88 74 0.543
Los Angeles Dodgers 88 74 0.543
San Francisco Giants 76 85 0.472
Arizona Diamondbacks 76 86 0.469
Colorado Rockies 76 86 0.469
### National Football League 2006 ###
New England Patriots 12 4 0 0.750
New York Jets 10 6 0 0.625
Buffalo Bills 7 9 0 0.438
Miami Dolphins 6 10 0 0.375
Baltimore Ravens 13 3 0 0.812
Cincinnati Bengals 8 8 0 0.500
Pittsburgh Steelers 8 8 0 0.500
Cleveland Browns 4 12 0 0.250
Indianapolis Colts 12 4 0 0.750
Tennessee Titans 8 8 0 0.500
Jacksonville Jaguars 8 8 0 0.500
Houston Texans 6 10 0 0.375
San Diego Chargers 14 2 0 0.875
Kansas City Chiefs 9 7 0 0.562
Denver Broncos 9 7 0 0.562
Oakland Raiders 2 14 0 0.125
Philadelphia Eagles 10 6 0 0.625
Dallas Cowboys 9 7 0 0.562
New York Giants 8 8 0 0.500
Washington Redskins 5 11 0 0.312
Chicago Bears 13 3 0 0.812
Green Bay Packers 8 8 0 0.500
Minnesota Vikings 6 10 0 0.375
Detroit Lions 3 13 0 0.188
New Orleans Saints 10 6 0 0.625
Carolina Panthers 8 8 0 0.500
Atlanta Falcons 7 9 0 0.438
Tampa Bay Buccaneers 4 12 0 0.250
Seattle Seahawks 9 7 0 0.562
St. Louis Rams 8 8 0 0.500
San Francisco 49ers 7 9 0 0.438
Arizona Cardinals 5 11 0 0.312
Figure 1: The output of the Sports application
Adding an additional sport (basketball, hockey, pie-eating, or whatever) isn't difficult. But Listing 1 through 6 have a very serious deficiency. The client code knows way too much about the implementation (the underlying data structures) of the Sports application. In particular, the client code (Listing 6) uses an ArrayList for baseball:
List<Sports> baseballList = baseball.getTeamList();
And for football, the client code uses a Java array:
Sports[] footballList = football.getTeamList();
The difference between an ArrayList and an array forces the client code's for-loops to differ slightly:
for(int i = 0;i < baseballList.size();++i) { ...
for(int i = 0;i < footballList.length;++i) { ...
These code differences are clumsy and wasteful. As you may expect, the Iterator design pattern comes to the rescue.
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|