/** * Contains example methods for lecture, about loops. */ class Looper { static void doIt() { int i = 0; while (i < 1000000) { System.out.println("I am at " + i); i = i + 1; } } static double sumSqrtFrom0ToN(int n) { long startTime = System.currentTimeMillis(); int i = 0; double sumSoFar = 0; while (i < n) { sumSoFar = sumSoFar + Math.sqrt(i); i = i + 1; } long elapsedTime = System.currentTimeMillis()-startTime; System.out.println( "For fun: elapsed time was " + elapsedTime/1000.0 + "seconds" ); return sumSoFar; } /** Return the 'fingerprint' of a string -- * a number which is a summary of it's info * (but not necessariliy different than every * other String's fingerprint). * This is also called a "hash value". * @param wrd The String to fingerprint. * @return `wrd`s fingerprint. */ static int fingerPrint( String wrd ) { int i = 0; // Or, "nextIndexToLookAt" int fingerPrintSoFar = 0; while ( i < wrd.length() ) { fingerPrintSoFar = fingerPrintSoFar + (int)(wrd.charAt(i)); i = i + 1; } return fingerPrintSoFar; } }