int [] scores = new int [10];

Slides:



Advertisements
Similar presentations
Chapter 10 Introduction to Arrays
Advertisements

Lecture 05 - Arrays. Introduction useful and powerful aggregate data structure Arrays allow us to store arbitrary sized sequences of primitive values.
Arrays Liang, Chpt 5. arrays Fintan Array of chars For example, a String variable contains an array of characters: An array is a data structure.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
A First Book of ANSI C Fourth Edition
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
Catie Welsh March 28,  Lab 7 due Friday, April 1st, 1pm 2.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
1 Arrays An array is a collection of data values, all of which have the same type. The size of the array is fixed at creation. To refer to specific values.
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
1 Building Java Programs Chapter 7: Arrays These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or.
Truth and while Today 15 Minutes online time to finish the random/Swing programs. Truth tables: Ways to organize results of Boolean expressions. Note Taking:
How do you do the following? Find the number of scores within 3 points of the average of 10 scores? What kind of a tool do you need? Today’s notes: Include.
COMPUTER PROGRAMMING 2 ArrayLists. Objective/Essential Standard Essential Standard 3.00Apply Advanced Properties of Arrays Essential Indicator 3.02 Apply.
Chapter 5: ARRAYS ARRAYS. Why Do We Need Arrays? Java Programming: From Problem Analysis to Program Design, 4e 2  We want to write a Java program that.
int [] scores = new int [10];
Week 6 - Friday.  What did we talk about last time?  Loop examples.
COMP 110 Arrays Luv Kohli November 5, 2008 MWF 2-2:50 pm Sitterson 014.
For Friday Read No quiz Program 6 due. Program 6 Any questions?
Chapter 9 Introduction to Arrays Fundamentals of Java.
Introduction to programming in java Lecture 21 Arrays – Part 1.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
Building Java Programs Chapter 7 Arrays Copyright (c) Pearson All rights reserved.
Lecture 5 array declaration and instantiation array reference
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Chapter VII: Arrays.
CSC 211 Java I for loops and arrays.
Sections 10.1 – 10.4 Introduction to Arrays
Topic 2 Elementary Programming
CSC111 Quick Revision.
© 2016 Pearson Education, Ltd. All rights reserved.
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Chapter 7 Part 1 Edited by JJ Shepherd
Java Review: Reference Types
Arrays An Array is an ordered collection of variables
Building Java Programs Chapter 7
Chapter 6 Arrays Solution Opening Problem
Building Java Programs
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
© A+ Computer Science - Arrays and Lists © A+ Computer Science -
Java Programming Loops
Truth tables: Ways to organize results of Boolean expressions.
Can store many of the same kind of data together
Object Oriented Programming in java
Truth tables: Ways to organize results of Boolean expressions.
python.reset() Also moving to a more reasonable room (CSE 403)
Announcements Lab 7 due Wednesday Assignment 4 due Friday.
int [] scores = new int [10];
Announcements Lab 6 was due today Lab 7 assigned this Friday
Building Java Programs
Chapter 6 Arrays.
Computer Science 2 Tally Arrays 2/4/2016.
Truth tables: Ways to organize results of Boolean expressions.
Dr. Sampath Jayarathna Cal Poly Pomona
Arrays in Java.
Java Programming Loops
Suggested self-checks: Section 7.11 #1-11
ArrayList Day 3 Be able to read AP Mult Choice questions tied to ArrayLists Be able to write a program that uses ArrayLists that analyzes numbers.
Java: Variables, Input and Arrays
Dry Run Fix it Write a program
Random Numbers while loop
int [] scores = new int [10];
Dry Run Fix it Write a program
How do you do the following?
Collections.
Java Coding 6 David Davenport Computer Eng. Dept.,
Arrays.
Week 7 - Monday CS 121.
Presentation transcript:

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

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

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

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

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)

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.

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

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

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

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;

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.

Standard Deviation Example. Find the standard deviation of 4, 9, 11, 12, 17, 5, 8, 12, 14 First work out the mean (Average, xbar,…): (4 + 9 + 11 + 12 + 17 + 5 + 8 + 12 + 14)/9 = 10.222 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   3.16   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: 139.55 / 8 = 17.44 And finally, square root this: 4.18

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

“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…”

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

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.

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.