Thursday, 24th June 2010
The new frontier for learning Java

NumberSquare.java

From WikiJava

Jump to: navigation, search


This application shows the use of arrays.

Contents

the article

The application generates a multiplication table. It has the numbers 1 to 10 across and down. every cell contains three characters (if the number has less than three characters, it is prefixed with spaces to make up the numbers).

Hyphens (-) are used to separate the rows and pipe symbols are used to separate the columns.

Arrays

//create the package.
package org.wikijava.file.split;
 
/**
 * @author Alex Mutuku Mbolonzi
 * @version 08/06/08
 */
 
// create the class.
public class NumberSquare {
 
    /**
     * First is to create a Two dimensional array of size 11 since arrays are
     * zero based.
     */
    static int myArray[][] = new int[11][11];
 
    //the long string is split to fit in the screen
    private static final String verticalSeparator = "____________________"
	    + "_________________________";
 
    private static final String horizontalSeparator = "|";
 
    private static final String horizontalSpace = "%3d";
 
    private static final String emptyCell = horizontalSeparator + "   ";
 
    /**
     * @param args
     */
    public static void main(String[] args) {
 
	// Inserts a line at the top of the table
	System.out.println(verticalSeparator);
 
	// Inserts a white space at the top left corner of the table.
	System.out.print(emptyCell);
 
	/*
	 * This for loop prints the first row of the array it has been
	 * initialized to 1, and it increments by one after each iteration, it
	 * prints all the numbers from one upto the max depending on the size of
	 * the array for now it will print 1 to 10
	 * 
	 * The pipe symbol is used to separate each row.
	 */
	for (int m = 1; m < myArray.length; m++) {
 
	    /*
	     * The 'System.out.printf()' method is used to format the numbers
	     * being printed out by printing three characters in each cell, if
	     * the number has less than three characters it pad's it with white
	     * spaces.
	     */
	    // Prints the first row of the table.
	    System.out.printf(horizontalSeparator + horizontalSpace, +m);
	}
 
	System.out.print(horizontalSeparator);
 
	// prints a blank line to make the output more legible
	System.out.println();
 
	/*
	 * This for loop is used to fill the array by calling the method
	 * 'fillArray'
	 */
	for (int j = 1; j < myArray.length; j++)
	    // fill and print the array
	    fillArray(j, j);
 
	// Inserts a line at the bottom of the table.
	System.out.print(verticalSeparator);
 
	// prints a blank line
	System.out.println();
 
    } // end main()
 
    /**
     * prints the multiplication table.
     * 
     * @param row
     *            number of rows
     * @param col
     *            number of columns
     */
    private static void fillArray(int row, int col) {
 
	// puts a line before each row of characters.
	System.out.println(verticalSeparator);
 
	// prints the numbers in each row of the array
	for (int k = 0; k < myArray.length; k++) {
	    myArray[row][col] = k;
	}
 
	System.out.printf(horizontalSeparator + horizontalSpace, +row);
 
	// computes the values of the elements in the Array
	for (int i = 1; i < myArray.length; i++) {
	    System.out
		    .printf(horizontalSeparator + horizontalSpace, +(row * i));
	}
 
	// Inserts a pipe symbol at the end of each row.
	System.out.print(horizontalSeparator);
	System.out.println();
 
    }
}

Comments from the users

To be notified via mail on the updates of this discussion you can login and click on watch at the top of the page

fixed

Hi, I fixed the article a bit, in order not to repeat literals (especially that evil pipe sign), and fixed readibility overall..

G

--DonGiulio 03:03, 17 October 2008 (PDT)


Title (required):

Website:

Comment: