Catie Welsh March 28, 2011.  Lab 7 due Friday, April 1st, 1pm 2.

Slides:



Advertisements
Similar presentations
COMP 110: Introduction to Programming Tyler Johnson Mar 25, 2009 MWF 11:00AM-12:15PM Sitterson 014.
Advertisements

Chapter 8: Arrays.
Arrays I Savitch Chapter 6.1: Introduction to Arrays.
CS110 Programming Language I Lab 10: Arrays I Computer Science Department Spring 2014.
Arrays. What is an array An array is used to store a collection of data It is a collection of variables of the same type.
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
Introduction to arrays Data in economy size packages.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
Arrays. A problem with simple variables One variable holds one value –The value may change over time, but at any given time, a variable holds a single.
18-Jun-15 Arrays. 2 A problem with simple variables One variable holds one value The value may change over time, but at any given time, a variable holds.
1 COMP 110 Arrays Tabitha Peck M.S. March 31, 2008 MWF 3-3:50 pm Philips 367.
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
Loops – While, Do, For Repetition Statements Introduction to Arrays
1 Arrays In many cases we need a group of nearly identical variables. Example: make one variable for the grade of each student in the class This results.
Lecture 15 Arrays: Part 1 COMP1681 / SE15 Introduction to Programming.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
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.
26-Jun-15 Arrays. 2 A problem with simple variables One variable holds one value The value may change over time, but at any given time, a variable holds.
Arrays. A problem with simple variables One variable holds one value –The value may change over time, but at any given time, a variable holds a single.
Arrays.
Arrays. A problem with simple variables One variable holds one value –The value may change over time, but at any given time, a variable holds a single.
CS0007: Introduction to Computer Programming Introduction to Arrays.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
One Dimensional Array. Introduction to Arrays Primitive variables are designed to hold only one value at a time. Arrays allow us to create a collection.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
CMSC 202 Arrays. Aug 6, Introduction to Arrays An array is a data structure used to process a collection of data that is all of the same type –An.
JAVA Array 8-1 Outline  Extra material  Array of Objects  enhanced-for Loop  Class Array  Passing Arrays as Arguments to Methods  Returning Arrays.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
COMP102 Lab 081 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
ARRAYS 1 TOPIC 8 l Array Basics l Arrays and Methods l Programming with Arrays Arrays.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Introduction to Collections Arrays. Collections Collections allow us to treat a group of values as one collective entity. The array is a collection of.
ARRAYS Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
Lecture 18/19 Arrays COMP1681 / SE15 Introduction to Programming.
Catie Welsh April 20,  Program 4 due Wed, April 27 th by 11:59pm  Final exam, comprehensive ◦ Friday, May 6th, 12pm  No class Friday - Holiday.
Arrays. The array data structure Array is a collection of elements, that have the same data type Integers (int) Floating point numbers (float, double)
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
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.
Arrays Chapter 6. Objectives learn about arrays and how to use them in Java programs learn how to use array parameters and how to define methods that.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Two Dimensional Arrays Found in chapter 8, Section 8.9.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
int [] scores = new int [10];
1 Parameter passing Call by value The caller evaluates the actual parameters and passes copies of their values to the called function. Changes to the copies.
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
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 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Introduction to programming in java Lecture 21 Arrays – Part 1.
Windows Programming Lecture 03. Pointers and Arrays.
Arrays Chapter 7.
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Some Assignments  Write a program which prints the following information about at least 5 persons: NAME MAIL-ID EMPLOYEE-CODE PHONE Eg. Umesh
Michele Weigle - COMP 14 - Spr 04 Catie Welsh March 30, 2011
Software Development Java Classes and Methods
Chapter 7 Part 1 Edited by JJ Shepherd
Arrays, For loop While loop Do while loop
Yong Choi School of Business CSU, Bakersfield
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.
int [] scores = new int [10];
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
Dr. Sampath Jayarathna Cal Poly Pomona
Collections.
Presentation transcript:

Catie Welsh March 28, 2011

 Lab 7 due Friday, April 1st, 1pm 2

3

 Array basics 4

 You wrote a program to read in a list of basketball scores from the user and output a bunch of statistics 5

System.out.println("Enter the list of basketball scores " + "(enter a negative number to end your list): "); while ( (score = keyboard.nextInt()) >= 0) { totalGames++; scoreSum += score; if (score >= 90) totalGamesOver90++; if (score > highestScore) highestScore = score; if (score < lowestScore) lowestScore = score; } if (totalGames > 0) { // some stuff double average = (double) scoreSum / (double) totalGames; // some other stuff } 6

System.out.println("Enter the list of basketball scores " + "(enter a negative number to end your list): "); while ( (score = keyboard.nextInt()) >= 0) { totalGames++; scoreSum += score; } if (totalGames > 0) { double average = (double) scoreSum / (double) totalGames; System.out.println("Average score: " + average); } 7

 …we wanted to know which of the scores entered were ◦ above average? ◦ below average?  How would we do it?  Let’s simplify this a little first 8

System.out.println("Enter 5 basketball scores:"); for (int i = 0; i < 5; i++) { scoreSum += keyboard.nextInt(); } double average = (double) scoreSum / 5.0; System.out.println("Average score: " + average); 9

 …we wanted to know which of the scores entered were ◦ above average? ◦ below average?  How would we do it? 10

System.out.println("Enter 5 basketball scores:"); int score1 = keyboard.nextInt(); int score2 = keyboard.nextInt(); int score3 = keyboard.nextInt(); int score4 = keyboard.nextInt(); int score5 = keyboard.nextInt(); double average = (double) (score1 + score2 + score3 + score4 + score5) / 5.0; System.out.println("Average score: " + average); // repeat this for each of the 5 scores if (score1 > average) System.out.println(score1 + ": above average"); else if (score1 < average) System.out.println(score1 + ": below average"); else System.out.println(score1 + ": equal to the average"); 11

System.out.println("Enter 80 basketball scores:"); int score1 = keyboard.nextInt(); int score2 = keyboard.nextInt(); int score3 = keyboard.nextInt(); //...are we done yet? int score23 = keyboard.nextInt(); int score24 = keyboard.nextInt(); int score25 = keyboard.nextInt(); //...how about now? int score67 = keyboard.nextInt(); int score68 = keyboard.nextInt(); //...all typing and no play makes Homer...go crazy? int score80 = keyboard.nextInt(); //...whew! double average = (double) (score1 + score2 + score3 + score score23 + score24 + score ) / 80.0; System.out.println("Average score: " + average); // now do below/above average check for all 80 scores 12

 Arrays to the rescue!  An array is a collection of items of the same type  Like a list of variables, but with a nice, compact way to name them  A special kind of object in Java 13 Array Man

int[] scores = new int[5];  This is like declaring 5 strangely named variables of type int: ◦scores[0] ◦scores[1] ◦scores[2] ◦scores[3] ◦scores[4] 14

 Variables such as scores[0] and scores[1] that have an integer expression in square brackets are known as: ◦ indexed variables, subscripted variables, array elements, or simply elements  An index or subscript is an integer expression inside the square brackets that indicates an array element 15

 Where have we seen the word index before? ◦ String’s indexOf method  Index numbers start with 0. They do NOT start with 1 or any other number. 16

 The number inside square brackets can be any integer expression ◦ An integer:scores[3] ◦ Variable of type int:scores[index] ◦ Expression that evaluates to int: scores[index*3]  Can use these strangely named variables just like any other variables: ◦scores[3] = 68; ◦scores[4] = scores[4] + 3; // just made a 3-pointer! ◦System.out.println(scores[1]); 17

 The array itself is referred to by the name scores (in this particular case) Indices the array scores scores[3]

System.out.println("Enter 5 basketball scores:"); int[] scores = new int[5]; int scoreSum = 0; for (int i = 0; i < 5; i++) { scores[i] = keyboard.nextInt(); scoreSum += scores[i]; } double average = (double) scoreSum / 5; System.out.println("Average score: " + average); for (int i = 0; i < 5; i++) { if (scores[i] > average) System.out.println(scores[i] + ": above average"); else if (scores[i] < average) System.out.println(scores[i] + ": below average"); else System.out.println(scores[i] + ": equal to the average"); } 19

 You can also use another form of the for loop with collections (such as arrays) for (int s : scores) { if (s > average) System.out.println(s + ": above average"); else if (s < average) System.out.println(s + ": below average"); else System.out.println(s + ": equal to the average"); }  s takes on the value of each element of the array score, but you cannot change an element’s value this way 20

 Syntax for creating an array: Base_Type[] Array_Name = new Base_Type[Length]  Example: int[] pressure = new int[100];  Alternatively: int[] pressure; pressure = new int[100]; 21

 The base type can be any type double[] temperature = new double[7]; Student[] students = new Student[35];  The number of elements in an array is its length, size, or capacity ◦ temperature has 7 elements, temperature[0] through temperature[6] ◦ students has 35 elements, students[0] through students[34] 22

 Usually want to use a named constant when creating an array public static final int NUMBER_OF_READINGS = 100; int[] pressure = new int[NUMBER_OF_READINGS]; 23

System.out.println("How many scores?"); int numScores = keyboard.nextInt(); int[] scores = new int[numScores]; 24

 An array is a special kind of object ◦ It has one public instance variable: length ◦ length is equal to the length of the array Pet[] pets = new Pet[20]; pets.length has the value 20 ◦ You cannot change the value of length because it is final 25

System.out.println("Enter 5 basketball scores:"); int[] scores = new int[5]; int scoreSum = 0; for (int i = 0; i < scores.length; i++) { scores[i] = keyboard.nextInt(); scoreSum += scores[i]; } double average = (double) scoreSum / 5; System.out.println("Average score: " + average); for (int i = 0; i < scores.length; i++) { if (scores[i] > average) System.out.println(scores[i] + ": above average"); else if (scores[i] < average) System.out.println(scores[i] + ": below average"); else System.out.println(scores[i] + ": equal to the average"); } 26

 Indices MUST be in bounds double[] entries = new double[5]; entries[5] = 3.7; // ERROR! Index out of bounds  Your code will compile if you are using an index that is out of bounds, but it will give you an error when you run your program 27

 You can initialize arrays when you declare them int[] scores = { 68, 97, 102 };  Equivalent to int[] scores = new scores[3]; scores[0] = 68; scores[1] = 97; scores[2] = 102; 28

 More arrays 29