Computer Science 202
Introduction to Programming
Fall 2012, The College of Saint Rose
PerfectSquares BlueJ Project
Click here to download a BlueJ project for PerfectSquares.
Download file: PerfectSquares.vls
PerfectSquares Source Code
The Java source code for PerfectSquares is below. Click on a file name to download it.
/* * 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 * * $Id: PerfectSquares.java 1931 2012-10-02 15:00:07Z 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) { System.out.println(nextNumber + " squared is " + 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); } }