Activity 2.7 - Using for loops with data stored in arrays

Topic

This activity uses arrays and for loops to create a simple bar graph. Please read the notes at the end of this file.

Materials

In addition to the discussion of the solution in the Solution document, the project for this activity contains two classes:

Tasks

The objective is to print out a bar graph illustrating some data about the number of classes in successive Java versions, like this, in which each hash mark indicates one hundred classes.

1.0   |##
|
1.1 |#####
|
1.2 |##################
|
1.3 |#####################
|
1.4 |##############################
|
|----|----|----|----|----|----|----|----|----|----|
Hundreds of classes in successive versions of Java

Instructions

Read the comments and complete the code for Ex2_7 by completing:

  1. the method printHashMarks(int i), which prints as many hash marks as the value stored in i, and then prints a newline character;
  2. the for loop condition in plotGraph, so that all versions of Java are included;
  3. the print statement in the for loop in plotGraph, to print labels on the axis.

Run the project to test your answer.

Notes

1. As of NetBeans 6.9, there has been a bug in the way NetBeans handles the tab character in the output window and so the output may not align correctly. If in doubt, please check the discussion of this activity to check your understanding.

2. The code uses more than one kind of print statement: print() prints its argument, while println() prints its argument and a newline character, so that the next output begins on the following line of the screen. A println() statement with no argument simply prints a newline character.