BUILDING JAVA PROGRAMS CHAPTER 7 Arrays. Exam #2: Chapters 1-6 Thursday Dec. 4th.

Slides:



Advertisements
Similar presentations
Chapter 8: Arrays.
Advertisements

Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading: , 3.3 self-checks: Ch. 7 #5, 8,
1 Various Methods of Populating Arrays Randomly generated integers.
Arrays I Savitch Chapter 6.1: Introduction to Arrays.
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. Topics Tables of Data Arrays – Single Dimensional Parsing a String into Multiple Tokens Arrays - Multi-dimensional.
Introduction to Application Programming IST 256 Application Programming for Information Systems Xiaozhong Liu
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-1: Arrays reading: 7.1 self-checks: #1-9 videos: Ch. 7 #4.
Copyright 2008 by Pearson Education Building Java Programs Chapter 7 Lecture 7-1: Arrays reading: 7.1 self-checks: #1-9 videos: Ch. 7 #4.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
Lecture 15 Arrays: Part 1 COMP1681 / SE15 Introduction to Programming.
BUILDING JAVA PROGRAMS CHAPTER 7 Array Algorithms.
CS 101 Problem Solving and Structured Programming in C Sami Rollins Spring 2003.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Building java programs chapter 6
CSci 111 – computer Science I Fall 2014 Cynthia Zickos WRITING A SIMPLE PROGRAM IN JAVA.
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.
1 Array basics. Data Structures Sometimes, we have data that have some natural structure to them  A few examples: Texts are sequences of characters Images.
Building java programs, chapter 5 Program logic and indefinite loops.
Array - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 12/19/20151.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Building Java Programs Chapter 7 Arrays Copyright (c) Pearson All rights reserved.
Building java programs, chapter 3 Parameters, Methods and Objects.
Introduction to array: why use arrays ?. Motivational example Problem: Write a program that reads in and stores away 5 double numbers After reading in.
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.
Variables and memory addresses
Simple algorithms on an array - compute sum and min.
int [] scores = new int [10];
BUILDING JAVA PROGRAMS CHAPTER 7 Arrays days until the AP Computer Science test.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
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.
Building Java Programs Chapter 7 Arrays Copyright (c) Pearson All rights reserved.
Topic 21 arrays - part 1 Copyright Pearson Education, 2010 Based on slides by Marty Stepp and Stuart Reges from "Should.
CSc 110, Autumn 2016 Lecture 15: lists Adapted from slides by Marty Stepp and Stuart Reges.
CSC111 Quick Revision.
1-1 Logic and Syntax A computer program is a solution to a problem.
Dr. Kyung Eun Park Summer 2017
Repetition-Counter control Loop
Building Java Programs
Building Java Programs
Arrays Part 1 Topic 19 - Stan Kelly-Bootle
CSC 142 Computer Science II
CSC141 Computer Science I Zhen Jiang Dept. of Computer Science
Building Java Programs Chapter 7
Building Java Programs
Building Java Programs
Building Java Programs
Lecture 15: lists Adapted from slides by Marty Stepp and Stuart Reges
Building Java Programs
Building Java Programs
Building Java Programs
Can we solve this problem?
python.reset() Also moving to a more reasonable room (CSE 403)
Lecture 19: lists Adapted from slides by Marty Stepp and Stuart Reges
Lecture 9: Arrays Building Java Programs: A Back to Basics Approach
Building Java Programs
Building Java Programs
Suggested self-checks: Section 7.11 #1-11
Can we solve this problem?
Building Java Programs
Building Java Programs
File output; Arrays reading: 6.4 – 6.5, 7.1
Building Java Programs
Can we solve this problem?
Building Java Programs
Presentation transcript:

BUILDING JAVA PROGRAMS CHAPTER 7 Arrays

Exam #2: Chapters 1-6 Thursday Dec. 4th

At the end of this class, you will be able to Understand what an array is and why the are used. Create an array. Determine the size of an array variable. Modify the contents of an array. Access values in an array.

Write code In Eclipse, write a program that to read temperature values from a file and calculate the average. File: … Example Output: Average temp = 44.6 Start with Pseudo-Code. Who will share their pseudo-code for a ticket?

// open a file with a scanner // read the count of temperatures in the file // initialize a sum to zero // for each temperature in the file //add the temp to the sum // average = sum / count // output average

Can we solve this slightly different problem? How many days' temperatures were above average? Of course we can, it’s just software! What do you need to know? The average temperature Each days’ temperature after you know the average Two ways to solve this: 1) Read the file twice. But what if the input is user input?? 2) Save each day’s temperature as you read them

Why the problem is hard We need each input value twice: to compute the average (a cumulative sum) to count how many were above average We could read each value into a variable... but we: don't know how many days are needed until the program runs don't know how many variables to declare We need a way to declare many variables in one step.

Working with many values So far, you have been working with variables that store a single value. Example: How would you store 3 temperatures as doubles? double temperature1; double temperature2; double temperature3; Question: How would you store 100 temperatures as doubles? Answer: Use an array!

Array declaration Syntax type[] name = new type[length]; Type of an array’s element must be declared with the array (just like a variable. All elements of an array must be of the same type. Examples: int[] numbers = new int[10]; double[] prices = new double[20]; String[] names = new String[3]; int[] fraction = new int[2]; Syntax Yoda

Arrays array - object that stores many values of the same type. element - One value in an array. Index - A 0-based integer to access an element from an array. index value element 0element 4element 9

Array Construction double[] temperatures = new double[3]; The variable temperatures is not itself the array. temperatures stores a reference to the array. 3 elements in the array: temperatures[0], temperatures[1], and temperatures[2]. temperatures 0.0 [0][1][2]

Simple Array Practice In eclipse, write a method that declares an array of size 8 of doubles. Print out the values in the array, one value on each line public static void arrayPractice() { double[] a = new double[8]; for (int i = 0; i < 8; i++) { System.out.println(a[i]); }

Arrays The size of an array can be found using the length field: int[] arr = new int[52]; System.out.println(arr.length); Similar to length() on a String, but without ()’s. String greeting = “Hello, world!”; System.out.println(greeting.length());

Arrays The size of an array can be any integer expression. int[] data = new int[x % ]; int size = console.nextInt(); int[] data2 = new int[size]; The size of an array can not change once it has been initialized. data2.lenth = 7; // ILLEGAL However, you can assign a new array of a different size to the same variable. int[] arr = new int[6]; … arr = new int[9];

Accessing elements Elements of an array are accessed using an indexer: name[index]// access name[index] = value;// modify Indexing begins at zero (just like charAt()). Example: numbers[0] = 27; numbers[3] = -6; System.out.println(numbers[0]); if (numbers[3] < 0) { System.out.println("Element 3 is negative."); }

Weather problem Using Eclipse, solve our “hard problem”. Read the file and output the average and the number of days with temperatures above average. Start with pseudo-code. Who will share their pseudo-code for a ticket?

public static void daysAboveAverage() { // read a file's temperatures into a array // calculate the array average // output the elements above average } public static double[] readFile(String name) { // open the file with a scanner // read the count of temperatures in the file // create an array to hold all the temps // for each temp we read in the file //add temp to the array // return the array } public static double findAvg(double[] temps) { // initialize a sum to zero // for each temp in the array //add the temp to the sum // return average = sum / count } public static int getAboveAvg(double[] temps, double avg) { // initialize a count = 0 // for each temp in the array //if temp > avg count++ // return count }

Homework for Chapter 7 HomeworkAssigned onDue on Read BJP 7.1 and write notes11/24/201411/26/2014 Practice It: SC 7.1 -> 7.511/24/201411/26/2014 Practice It: Ex 7.1, 7.211/24/201411/26/2014