Computer Science 523
Advanced Programming

Summer 2014, The College of Saint Rose

PerfectSquares BlueJ Project

Click here to download a BlueJ project for PerfectSquares.


PerfectSquares Source Code

The Java source code for PerfectSquares is below. Click on a file name to download it.


PerfectSquares.java

/*
 * Example PerfectSquares: a program to demonstrate basic while loops
 * by printing all perfect squares up to a given limit.
 *
 * Jim Teresco, The College of Saint Rose, CSC 202, Fall 2012
 * Updated for CSC 523, Summer 2014
 *
 * $Id: PerfectSquares.java 2366 2014-05-20 02:33:22Z terescoj $
 */

import java.util.Scanner;

public class PerfectSquares {

    public static void main(String[] args) {

        // keyboard input here
        Scanner kbd = new Scanner(System.in);

        // let's get our upper limit
        System.out.print("What is the upper limit on perfect squares? ");
        int limit = kbd.nextInt();

        // compute the first
        int nextNumber = 1;
        int square = nextNumber * nextNumber;

        // now print them out as long as the square does not exceed the limit
        while (square <= limit) {

	    // hey look, a printf!  %d means find and print an int
            System.out.printf("%d squared is %d\n", nextNumber, square);

            // now we move on to the next number to square -- this is a 
            // shorthand notation for
            // nextNumber = nextNumber + 1;
            nextNumber++;

            // compute the new square
            square = nextNumber * nextNumber;

            // and at the end of the body of the while loop, Java will
            // return back to the top and re-test the condition to see
            // if we need to go around again.
        }

        // if we got here, the loop is over.  We'll print a message so we can
        // see this.
        System.out.println("No further perfect squares that do not exceed " + limit);
    }
}