[ Team LiB ] Page 646

[ Team LiB ] 5 Control Flow, Exception Handling, and Assertions 5.1 Finding primes using for-loops. // Filename: ForPrimes.java public class ForPrimes { final static int MAX = 100; public static void main(String[] args) { numbers: for (int num = 1; num < MAX; num++) { int divLim = (int) Math.sqrt(num); for (int div = 2; div <= divLim; div++) if ((num % div) == 0) continue numbers; System.out.println(num); } } } Finding primes using while-loops. // Filename: WhilePrimes.java public class WhilePrimes { final static int MAX = 100; public static void main(String[] args) { int num = 1; numbers: while (num < MAX) { int number = num++; int divLim = (int) Math.sqrt(number); int div = 2; while (div <= divLim) if ((number % div++) == 0) continue numbers; System.out.println(number); } } } 5.2 /** A PowerPlant with a reactor core. */ public class PowerPlant { /** Each power plant has a reactor core. This has packageaccessibility so that the Control class which is definedin the same package can access it. */ Reactor core; /** Create and initialize the PowerPlant with a reactor core. Page 647
Hint: If you are looking for good and high quality web space to host and run your java application check Vision java web hosting services

Comments are closed.