18
BUILDING JAVA PROGRAMS CHAPTER 7 Arrays

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

Embed Size (px)

Citation preview

BUILDING JAVA PROGRAMSCHAPTER 7Arrays

Exam #2: Chapters 1-6Thursday 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 codeIn Eclipse, write a program that to read temperaturevalues from a file and calculate the average. File: <count> <temp> <temp> … <temp>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 0 1 2 3 4 5 6 7 8 9

value 12 49 -2 26 5 17 -6 84 72 3

element 0 element 4 element 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.00.00.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 % 2 + 35];

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] // accessname[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 andthe 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 7Homework Assigned on Due on

Read BJP 7.1 and write notes 11/24/2014 11/26/2014

Practice It: SC 7.1 -> 7.5 11/24/2014 11/26/2014

Practice It: Ex 7.1, 7.2 11/24/2014 11/26/2014