Presentation is loading. Please wait.

Presentation is loading. Please wait.

int [] scores = new int [10];

Similar presentations


Presentation on theme: "int [] scores = new int [10];"— Presentation transcript:

1 int [] scores = new int [10];
for (int i=0; i<scores.length; i++) { System.out.println(scores[i]); } scores.length AP Java for (int s : scores) { System.out.println(s); } int [ ] scores = { 10, 50, 20 }; More on Arrays int [] scores = new int [10];

2 Today Review Arrays Declaration Inputting Values Processing Outputting
Array program #1 For..each loop Array program #2

3 Some Vocabulary Array: A numbered sequence of items, which are all of the same type. Index: The position of the item. In Java they are numbered 0, 1, 2, … (n-1) where n is the number of elements in the array. Base type: the type of elements in the array. Can have arrays of primitives, classes, or interfaces

4 Java arrays are Objects
Created using new The array variable does not hold the array, it holds the reference to the array. (It is a pointer) It is a pointer that is defined in the array declaration. As a reference, it can have a value of null. (Like nil in Pascal.)

5 Declaration: Two steps.
Or one step int [] list = new list [5]; int [] list; Creates a variable of type int[] (something that can point to an array of ints.) The variable now has a value of null. list = new int [5]; Points list to an array with 5 spaces for ints. The length of the array is an instance variable. You can get the length by using the method. list.length. (Generically it is arrayname.length)

6 int [] list = new int [5]; (5) list.length list[0] list[1] list[2]
list.length list[0] list[1] list[2] list[3] list[4] Note: In Java, a newly created array is automatically filled with: 0 for numbers false for boolean Unicode number zero for char null for objects.

7 Declaring and initializing
int [] list = { 1, 4, 9, 16 }; The elements using to initialize the array can be constants, variables, or expressions as long as the type matches. This can only be done in the declaration. However you can do the following later in the program list = new int[ ] { 4, 3, 2 , 12};

8 Some code examples // do any necessary initialization
for (int i = 0; i < A.length; i++) { . . . // process A[i] } double sum; // The sum of the numbers in A. sum = 0; // Start with 0. sum += A[i]; // add A[i] to the sum, for // i = 0, 1, ..., A.length - 1

9 How can you… Create an integer array called scores that will hold 10 scores? int [] scores = new int[10]; What is the code to get the scores from the user? Scanner input = new Scanner(System.in); for (int count = 0;count < 10; count++) { System.out.println(“Enter a score.”); scores[count] = input.nextInt(); }

10 How can you … Find the average of the array declared previously?
int total = 0; for (int count =0; count<scores.length;count++) total+=scores[count]; double average = 1.0*total / scores.length;

11 Array Program 1 Input: 10 scores (integers) Output:
The high score The low score The average The number of scores within 3 of the average Push: Show the numbers in order low to high Input: 10 scores and calculate the standard deviation Accommodations: Provide notes accessible at school and at home for this lesson. Include scaffolding activities to check for understanding during the lesson Have Java language available for students to complete activities and explore applying the ideas at home. Provide extended time to complete the assignment. Modifications: Students can complete either of the assignments for full credit Add an option for simple input to/output from the array. Input 10 names and output the names in reverse order. Model rolling a pair of dice 100 times and save the rolls in an array. Then show the rolls from the array. Students dry run a program, drawing a picture of the array and demonstrating how the values change while the program is running.

12 Standard Deviation Example.
Find the standard deviation of 4, 9, 11, 12, 17, 5, 8, 12, 14 First work out the mean (Average, xbar,…): ( )/9 = Now, subtract the mean individually from each of the numbers in the question and square the result. This is equivalent to the (x - xbar)² step. x refers to the values in the question. x               4        9        11       12       17       5       8       12      14 (x - xbar)²     38.7   1.49    0.60    3.16    45.9   27.3   4.94     14.3 Now add up these results (this is the 'sigma' in the formula): 139.55 Divide by n-1. n is the number of values, so in this case n-1 is 8: / 8 = 17.44 And finally, square root this: 4.18

13 “For Each” Loop Introduced in Java 5
Works both with standard arrays and ArrayLists Convenient for traversing (going through and looking at the values) for (int s : scores) { System.out.println(s); } An iterator is an object that helps to traverse a collection. It has a method next that supplies the next element of the collection. A “For each” loop is a shortcut for an iterator.

14 “For Each” Loop: Example 1
So s will store the value of the current address of the array. The type of the elements int [ ] scores = { ... }; ... int sum = 0; for (int s : scores) { sum += s; } The name of the array. Basically the same as: for (int i = 0; i < scores.length; i++) { int s = scores[i]; sum += s; } Thus the “for each” loop has been added to Java 5 simply for convenience. Read “for each integer s in scores…”

15 “For Each” Loop (cont’d)
You cannot add or remove elements within a “for each” loop. You cannot change elements of primitive data types or references to objects within a “for each” loop. For an array or ArrayList of objects, a “for each” loop supplies a reference to an element. You can change the object it refers to (unless it is immutable), but you cannot change the reference itself.

16 Copying arrays list.length (5) list[0] 1 list[1] 4
9 16 25 What would the following do? list = new int[] { 1, 4, 9, 16, 25 }; int [] b; b = list; How can you copy all of the elements? b = new int[5]; for (int i = 0; i < list.length; i++) b[i] = list[i]; // Copy each item from list to B.

17 Array Program 2 Write a program that will roll a pair of six-sided dice 1000 times and count and display how often each roll occurs. Also show which roll occurs the most often and which occurs the least often. Push: Compare the results to what should happen statistically. Push: Display the results in a graph. Create a random compliment generator using an array to store the compliments. Use a loop (so the user can be complimented often) have the computer display one of at least 5 random compliments. Push: Look up a Chatbot to include interaction with the user. Ask questions about them so you can give better compliments.


Download ppt "int [] scores = new int [10];"

Similar presentations


Ads by Google