Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "BUILDING JAVA PROGRAMS CHAPTER 7 Arrays. Exam #2: Chapters 1-6 Thursday Dec. 4th."— Presentation transcript:

1 BUILDING JAVA PROGRAMS CHAPTER 7 Arrays

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

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

4 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?

5 // 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

6 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

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

8 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!

9 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

10 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. index0123456789 value1249-226517-684723 element 0element 4element 9

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

12 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]); }

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

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

15 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."); }

16 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?

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

18 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


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

Similar presentations


Ads by Google