Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Week 9 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham Week 9 Topics 9.1.1 Nested Loops 9.1.2 Processing Sentinel Values 9.1.3 Random Numbers and Simulations 9.2.1 Arrays 9.2.2 Array Lists 9.1.1 Nested Loops • Sometimes the body of a loop is again a loop. We say that the inner loop is nested inside an outer loop. • As an example, assume that we want to generate the following output: [] [][] [][][] [][][][] 9.1.1 Nested Loops Cont. String r = ""; // Triangle is 4 rows high for (int i = 1; i <= 4; ++i) { // Number of blocks per row for (int j = 1; j <= i; j++) r = r + "[]"; r = r + "\n“; } System.out.println(r); 9.1.2 Processing Sentinel Values • Sometimes, the termination of a loop can only be evaluated in the middle of a loop. • You can introduce a Boolean variable to control such a loop. • The value that you evaluate to determine whether to terminate such a loop construct is called a sentinel value. 9.1.2 Processing Sentinel Values Cont. Example program run: Enter value, Q to quit: Enter value, Q to quit: Enter value, Q to quit: Enter value, Q to quit: Enter value, Q to quit: Average = 2.5 1 2 3 4 Q Scanner in = new Scanner(System.in); double total = 0; double i = 0; boolean done = false; while (!done) { System.out.print(“Enter value, Q to quit:”); String input = in.next(); if (input.equalsIgnoreCase(“Q”)) done = true; else { double x = Double.parseDouble(input); total = total + x; i++; } } if (i > 0) System.out.println("Average = " + total/i); 9.1.3 Random Numbers and Simulations • In a simulation, you repeatedly generate random numbers and use them to simulate an activity. • The basic approach of a simulation is to have a loop, generate a large number of sample values, and record these values. When completed, averages or other statistics are calculated. • See the Buffon needle experiment for an example. 9.1.3 Random Numbers and Simulations Cont. • • • • Here is how to simulate the cast of a die: Random generator = new Random(); int d = 1 + generator.nextInt(6); The call generator.nextInt(6) gives you a random number between 0 and 5 (inclusive). Add 1 to obtain a number between 1 and 6. • import java.util.Random; • nextInt(n) – a random integer between 0 (inclusive) and n (exclusive) • nextDouble() – a random floating-point number between 0 (inclusive) and 1 (exclusive) 9.2.1 Arrays • An array is a sequence of values of the same type. Arrays can be very useful, but suffer from the limitation that their length is fixed. • double[] data = new double[10]; • for (i = 0; i < data.length; ++i) data[i] = i * 10; data[0] value is 0, data[1] is 10, data[2] is 20, data[3] is 30 … data[9] is 90 9.2.1 Arrays Cont. • Representation of the array data from the previous slide: Value Subscript 0.0 0 10.0 20.0 30.0 40.0 50.0 60.0 70.0 80.0 90.0 1 2 3 4 5 6 7 8 9 9.2.2 Array Lists • ArrayList is a collection class. • Array lists can grow and shrink as needed. • The ArrayList class provides methods for many common tasks. • ArrayList<BankAccount> accounts = new ArrayList<BankAccounts>(); accounts.add(new BankAccount(1001)); 9.2.2 Array Lists Cont. • BankAccount anAccount = accounts.get(2); • BankAccount anAccount = new BankAccount(1729); • accounts.set(2, anAccount); • accounts.remove(0); • System.out.println(accounts.size()); 9.2.2 Array Lists Cont. • Note, don’t try to access an element of an array or an array list that does not exist, or you will get an out-of-bounds exception and the program will be terminated! • For example, this will generate an out-of-bounds exception: • BankAccount anAccount = accounts.get(accounts.size()); • Error since accounts.size() – 1 is the last valid element of the array list Reference: Big Java 2nd Edition by Cay Horstmann 9.1.1 Nested Loops (section 7.3 in Big Java) 9.1.2 Processing Sentinel Values (section 7.4 in Big Java) 9.1.3 Random Numbers and Simulations (section 7.5 in Big Java) 9.2.1 Arrays (section 8.1 in Big Java) 9.2.2 Array Lists (section 8.2 in Big Java)