1 / 5

AP Java

AP Java. Warm-up Boolean Array. class Exercise5 { public static void main ( String[] args ) { int [] val = {0, 1, 2, 3}; int temp; System.out.println ( "Original Array: " + val [0] + " " + val [1] + " " + val [2] + " " + val [3] );

donnelld
Download Presentation

AP Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. AP Java Warm-up Boolean Array

  2. class Exercise5 { public static void main ( String[] args ) { int[] val = {0, 1, 2, 3}; int temp; System.out.println( "Original Array: " + val[0] + " " + val[1] + " " + val[2] + " " + val[3] ); // write the code to reverse the order of the numbers in the array System.out.println( "Reversed Array: " + val[0] + " " + val[1] + " " + val[2] + " " + val[3] ); } }

  3. Warm up Array Reading • Common Array Algorithms Link • Complete the fill in blanks on the web pages • Take notes as needed.

  4. Boolean Array Program (11/2/2015) • Show all prime numbers <=1000 • Use the sieve of Eratosthenes • Write a program to use this process in finding all primes less than or equal to 1000. • How many primes will Java let you find using this method? • Cross out 1, because it is not prime. • Circle 2, because it is the smallest positive even prime. Now cross out every multiple of 2; in other words, cross out every second number. • Circle 3, the next prime. Then cross out all of the multiples of 3; in other words, every third number. Some, like 6, may have already been crossed out because they are multiples of 2. • Circle the next open number, 5. Now cross out all of the multiples of 5, or every 5th number. • Continue doing this until all the numbers through 100 have either been circled or crossed out. You have just circled all the prime numbers from 1 to 100!

  5. Getting Started • Creating an array of Booleans • boolean[] seive = new boolean[1000]; • Initializing them to true (potentially prime). • for(inti = 0; i < seive.length; i++ ) • seive[i] = true ;

More Related