Lecture 18/19 Arrays COMP1681 / SE15 Introduction to Programming.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

1 Arrays An array is a special kind of object that is used to store a collection of data. The data stored in an array must all be of the same type, whether.
Arrays.
Arrays I Savitch Chapter 6.1: Introduction to Arrays.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 1 Overview 7.1 Introduction to Arrays 7.2 Arrays in Functions 7.3.
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
Introduction to arrays Data in economy size packages.
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.
Arrays Chapter 6 Chapter 6.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
CS102--Object Oriented Programming Lecture 5: – Arrays – Sorting: Selection Sort Copyright © 2008 Xiaoyan Li.
1 6/20/2015CS150 Introduction to Computer Science 1 Functions Chapter 6, 8.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays in Classes and Methods l Programming.
Lecture 15 Arrays: Part 1 COMP1681 / SE15 Introduction to Programming.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
Lecture 16 Arrays: Part 2 COMP1681 / SE15 Introduction to Programming.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
Sorting Arrays. Selection Sort  One of the easiest ways to sort the elements of an array is by using the selection sort algorithm.  Assume that the.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
Chapter 7 Arrays. Overview 7.1 Introduction to Arrays 7.2 Arrays in Functions 7.3 Programming with Arrays 7.4 Multidimensional Arrays.
Chapter 10Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 10 l Array Basics l Arrays in Classes and Methods l Programming.
1 Week 9 l Array Basics l Arrays in Classes and Methods l Programming with Arrays and Classes l Sorting Arrays l Multidimensional Arrays Arrays.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays in Classes and Methods l Programming.
1 Chapter 6 l Array Basics l Arrays in Classes and Methods l Programming with Arrays and Classes l Sorting Arrays l Multidimensional Arrays Arrays.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays in Classes and Methods l Programming.
Chapter 61 Arrays Chapter 6. 2 Objectives learn about arrays and how to use them in Java programs learn how to use array parameters and how to define.
Comp 248 Introduction to Programming Chapter 6 Arrays Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
Chapter 10. Arrays Array Basics Arrays in Classes and Methods Programming with Arrays and Classes Sorting Arrays Computer Programming with JAVA.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
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.
Catie Welsh March 28,  Lab 7 due Friday, April 1st, 1pm 2.
ARRAYS 1 TOPIC 8 l Array Basics l Arrays and Methods l Programming with Arrays Arrays.
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.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
Chapter 8: Arrays and Functions Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
CMSC 202 Arrays 2 nd Lecture. Aug 6, Array Parameters Both array indexed variables and entire arrays can be used as arguments to methods –An indexed.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
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];
COMP 110 Arrays Luv Kohli November 5, 2008 MWF 2-2:50 pm Sitterson 014.
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
Asserting Java © Rick Mercer Chapter 7 The Java Array Object.
© Rick Mercer Chapter 7 The Java Array Object.  Some variables store precisely one value: a double stores one floating-point number a double stores one.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Introduction to programming in java Lecture 21 Arrays – Part 1.
Data Structures & Algorithms CHAPTER 2 Arrays Ms. Manal Al-Asmari.
Arrays Chapter 7.
Michele Weigle - COMP 14 - Spr 04 Catie Welsh March 30, 2011
Arrays in Classes and Methods
Arrays 2/4 By Pius Nyaanga.
Chapter 7 Part 1 Edited by JJ Shepherd
Chapter 6 Arrays Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Object Oriented Programming in java
Arrays .
Announcements Lab 7 due Wednesday Assignment 4 due Friday.
Announcements Lab 6 was due today Lab 7 assigned this Friday
Dr. Sampath Jayarathna Cal Poly Pomona
Building Java Programs
Building Java Programs
Programming with Arrays 1
Presentation transcript:

Lecture 18/19 Arrays COMP1681 / SE15 Introduction to Programming

SE15: Arrays18–2 Today’s Learning Objectives To find out what an array is Learn how to use arrays in Java programs Learn how to write methods that use arrays as parameters or return arrays Learn how to use an array as an instance variable in a class

SE15: Arrays18–3 Lecture Outline Array Basics Creating and accessing Arrays Arrays in Classes and Methods Case Study: Sales Report Arrays as method arguments Methods that return Arrays Savitch sections 6.1 and 6.2

SE15: Arrays18–4 Why do we need arrays? Consider the following code: int count; double next, sum, average; Scanner keyboard = new Scanner(System.in); System.out.println(“Enter 7 temperatures”); sum = 0; for(count=0; count < 7; count++) { next = keyboard.nextDouble(); sum = sum + next; } average = sum/7; What will happen if we wish to find the temperatures that are above the average?

SE15: Arrays18–5 Creating Arrays You can think of array as a collection of variables of the same type To create a collection of seven variables of type double double[] temperature = new double[7]; This will similar to declaring seven variables named temperature[0],temperature[1], temperature[2],temperature[3], temperature[4], temperature[5], temperature[6] Note the numbering starts from zero!

SE15: Arrays18–6 Accessing Arrays You can use these in similar ways to other variables temperature[4] = -1; temperature[6] = temperature[1] + 10; System.out.println(temperature[2]); But you can also use the number in the square brackets to compute the name of one of the variables. for( index=0; index < 7; index++) System.out.println(temperature[index]);

SE15: Arrays18–7 Details An array is created like an object of a class type Base_Type[] Array_Name = new Base_Type[length]; e.g. int[] pressure = new int[100]; This array has length 100 which means it has indexed variables pressure[0] through pressure[99] Indices temperature[4]

SE15: Arrays18–8 More Details The base type can be any type Book myBook = new Book[10]; The integer in the square brackets can be an expression that evaluates to an integer System.out.println(“ How many temperatures will be entered? ”); int size = keyboard.nextInt(); double[] temperature = new double[size]; OR int day = 1; System.out.println(“temp 3 days later” + temperature[day+3]);

SE15: Arrays18–9 The length instance variable An array is a kind of object and therefore may have instance variables. An array only has one public instance variable, length length is equal to the length of the array For example: Book[] myBook = new Book[20]; myBook.length has a value 20

SE15: Arrays18–10 Initialising Arrays An array may be initialised at the time of declaration double[] temperature = {30.0, 25.5, 32.0}; The array length is set to the minimum that will hold the values. This is equivalent to the following: double[] temperature = new double[3]; temperature[0]= 30.0; temparature[1]= 25.5; temperature[2]= 32.0; If you do not initialise the elements of an array they may be automatically initialised to a default value for the base type.

SE15: Arrays18–11 Arrays in Classes and Methods Case Study: Sales Report To write a program to generate sales reports for a company’s team of salesmen You need to be able to show which salesmen have the highest sales figures and how they compare the average. Need to record the name and sales figure for each salesman Use an array to keep track of the data for all salesmen and record the average and highest sales figures

SE15: Arrays18–12 Indexed Variables as Method Arguments An indexed variable for an array a, such as a[i], can be used anywhere that you can use any other variable of the base type. So an indexed variable can be an argument to a method in exactly the same way as any other variable of the array’s base type can be used as an argument. printBar(temperature[index]);

SE15: Arrays18–13 Entire Arrays as Method Arguments The way you specify an array parameter in a method definition is similar to the way you declare an array. public static double calcAverage(double[] a) { double sum = 0; int i =0; for(i=0; i < a.length; i++) { sum = sum + a[i]; } return(sum/a.length); }

SE15: Arrays18–14 Methods that Return Arrays In Java a method may return an array. You specify the return type for a method in the same way that you specify a type for an array parameter. public static double[] calcDiffFromAverage(double[] a) { double average = calcAverage(a); double[] diff = new double [a.length]; for(int i=0; i < diff.length; i++) { diff[i] = Math.abs(a[i] - average); } return diff; }

SE15: Arrays18–15 Partially Filled arrays In some situations you need some but not all of the indexed variables in an array, such as a list that is not yet full. In these situations you need to keep track of how much of the array is used. This is normally done with an int variable such as the instance variable bookCount in the Library Class which tells the methods that the array consists of the array elements 0 – bookCount-1

SE15: Arrays18–16 Sorting Arrays Sort an array of ints to arrange the elements so that a[0] is the smallest, a[1] is the next smallest and so forth. for (index=0; index < a.length; index++) Place the index th smallest element in a[index] Savitch 6.4

SE15: Arrays18–17 Selection Sort a[0]a[1]a[2]a[3]a[4]a[5]a[6]Unsorted

SE15: Arrays18–18 Selection Sort for (index=0; index < a.length; index++) { // Place the correct value in a[index] indexOfNextSmallest = an index of the smallest value among a[index], a[index+1],…,a[a.length-1]; Interchange the values of a[index] and a[indexOfNextSmallest]; }

SE15: Arrays18–19 Summary Looked at : What arrays are How to create and initialise them How to use indexed variables How to pass and return them in methods How to sort arrays using selection sort

SE15: Arrays18–20 Follow-up Work Savitch chapter 6 Self Test Exercise 6 Self Test Exercise 9 Re-write the sort program to sort an Array of Strings into Alphabetical order.