Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 9 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.

Similar presentations


Presentation on theme: "Week 9 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham."— Presentation transcript:

1 Week 9 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham

2 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

3 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: [] [][] [][][] [][][][]

4 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);

5 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.

6 9.1.2 Processing Sentinel Values Cont. Example program run: Enter value, Q to quit: 1 Enter value, Q to quit: 2 Enter value, Q to quit: 3 Enter value, Q to quit: 4 Enter value, Q to quit: Q Average = 2.5

7 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);

8 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 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)

10 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

11 9.2.1 Arrays Cont. Representation of the array data from the previous slide: 0.010.020.030.040.050.060.070.080.090.0 0123456789 Value Subscript

12 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 accounts = new ArrayList (); accounts.add(new BankAccount(1001));

13 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());

14 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

15 Reference: Big Java 2 nd 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)


Download ppt "Week 9 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham."

Similar presentations


Ads by Google