120 likes | 131 Views
This text introduces the concepts of random numbers and simulations, using a debugger to analyze program behavior, working with arrays, and utilizing array lists for dynamic collections.
E N D
Week 9 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham
Week 9 Topics 9.1.1 Random Numbers and Simulations 9.1.2 Using a Debugger 9.2.1 Arrays 9.2.2 Array Lists
9.1.1 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.1 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.1.2 Using a Debugger • A debugger is a program that you can use to execute another program and analyze its run-time behavior • You can make effective use of a debugger by mastering just three concepts: breakpoints, single-stepping, and inspecting variables
9.1.2 Using a Debugger Cont. • When a debugger executes a program, the execution is suspended whenever a breakpoint is reached • The single-step command executes the program one step at a time • You should step into a method to check if carries out its job correctly, you should step over it if you know it works correctly
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
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 4th Edition by Cay Horstmann 9.1.1 Random Numbers and Simulations (section 6.5 in Big Java) 9.1.2 Using a Debugger (section 6.6 in Big Java) 9.2.1 Arrays (section 7.1 in Big Java) 9.2.2 Array Lists (section 7.2 in Big Java)