Announcements Lab 7 due Wednesday Assignment 4 due Friday.

Slides:



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

Arrays I Savitch Chapter 6.1: Introduction to Arrays.
CS102--Object Oriented Programming Discussion 2: (programming strategy in java) – Two types of tasks – The use of arrays Copyright © 2008 Xiaoyan Li.
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
Lecture 17 Instructor: Craig Duckett Passing & Returning Arrays.
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.
Arrays CS Feb Announcements Exam 1 Grades on Blackboard Project 2 scores: end of Class Project 4, due date:20 th Feb –Snakes & Ladders Game.
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.
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.
Chapter 10Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 10 l Array Basics l Arrays in Classes and Methods l Programming.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays in Classes and Methods l Programming.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
French Territory of St. Pierre CSE 114 – Computer Science I Arrays.
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.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
Chapter 10. Arrays Array Basics Arrays in Classes and Methods Programming with Arrays and Classes Sorting Arrays Computer Programming with JAVA.
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.
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 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.
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.
CS 139-Programming Fundamentals Lecture 11B - Arrays Adapted from a presentation by Dr. Rahman Fall 2014.
EE 422C Day 2 Java, Eclipse. Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Introduction to array: why use arrays ?. Motivational example Problem: Write a program that reads in and stores away 5 double numbers After reading in.
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.
CS 180 Recitation 7 Arrays. Used to store similar values or objects. An array is an indexed collection of data values of the same type. Arrays are the.
COMP More About Arrays Yi Hong June 05, 2015.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
COMP 110 More arrays, 2D arrays, Program 4 Luv Kohli November 10, 2008 MWF 2-2:50 pm Sitterson 014.
COMP 110 Arrays Luv Kohli November 5, 2008 MWF 2-2:50 pm Sitterson 014.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
Building Java Programs Chapter 7 Arrays Copyright (c) Pearson All rights reserved.
Arrays Chapter 7.
4. Java language basics: Function
COMP 110 More arrays, 2D arrays, Program 4
Michele Weigle - COMP 14 - Spr 04 Catie Welsh March 30, 2011
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.
EKT472: Object Oriented Programming
Building Java Programs
BIT115: Introduction to Programming
An Introduction to Java – Part I, language basics
Building Java Programs
CS-161 Computer Programming Lecture 14: Arrays I
int [] scores = new int [10];
Building Java Programs
Building Java Programs
int [] scores = new int [10];
Announcements Lab 6 was due today Lab 7 assigned this Friday
Single-Dimensional Arrays chapter6
Dr. Sampath Jayarathna Cal Poly Pomona
Building Java Programs
Arrays.
Building Java Programs
How do you do the following?
Announcements Lab 5 due Wednesday at noon.
Presentation transcript:

Announcements Lab 7 due Wednesday Assignment 4 due Friday

Today in COMP 110 Review from last time Arrays in Classes & Methods Intro to Sorting Programming Demo

Arrays Review from last time Questions?

Arrays A special kind of object in Java used to store a collection of data What if you wanted to store 80 basketball scores? Instead of declaring 80 integer variables, declare a single array!

Array Details Syntax for creating an array: Example: Alternatively: Base_Type[] Array_Name = new Base_Type[Length]; Example: int[] pressure = new int[100]; //create 100 variables of type int that can //be referred to collectively Alternatively: int[] pressure; //declare an integer array called pressure pressure = new int[100]; //allocate memory for the array to hold 100 ints

Arrays The array itself is referred to by a name “scores” or “vector” (in our examples) Indices 1 2 3 4 68 73 57 102 94 the array scores scores[3]

Array Length 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]; int sizeOfArray = pets.length; //sizeOfArray will have the value 20 You cannot change the value of length (it is final)

For Loops and Arrays For loops are often perfectly suited to processing arrays Why? Because we know the number of iterations (array.length) int[] pressure = new int[100]; for(int index = 0; index < pressure.length; index++) scores[index] = 0;

Be Careful with Indices Indices MUST be in bounds double[] entries = new double[5]; entries[5] = 3.7; Your code WILL compile with an out-of-bounds index But it will result in a run-time error (crash) //RUN-TIME ERROR! Index out of bounds

Arrays in Classes & Methods

Arrays as Instance Variables Arrays can be used as instance variables in classes public class Course { private Student[] enrolledStudents; } public class Pressure { private double[] pressure;

Arrays as Instance Variables public class Pressure { private double[] pressure; //ask user for the number of pressure values and read them in public void getData() { System.out.println("Hi. How many pressure values would you like to enter?"); Scanner keyboard = new Scanner(System.in); int num = keyboard.nextInt(); pressure = new double[num]; System.out.println("Ok. Please enter " + num + " integers"); for(int i = 0; i < pressure.length; i++) { pressure[i] = keyboard.nextDouble(); } } } An array instance variable Create storage for the array Write values into the array

Arrays of Objects Creating an array of objects does not initialize the individual objects Student[] students = new Student[35]; students[0].getGPA(); //run-time error, students[0] holds no object Each object in the array must be instantiated students[0] = new Student(); students[1] = new Student(); … students[34] = new Student(); Do this in a loop

Arrays of Objects 1045 2584 2836 major class GPA major class GPA major Student[] students = new Student[3]; for(int i = 0; i < students .length; i++) { students[i] = new Student(); } 1045 2584 2836 students ? ? ? major class GPA major class GPA major class GPA

Indexed Variables as Arguments The same as using a regular variable public void printNum(int num) { System.out.println(num); } public void doStuff() { int[] scores = { 15, 37, 95 }; for(int index = 0; index < scores.length; index++) { printNum(scores[index]);

Array Assignment & Equality Arrays are objects The value of an array is a memory address Using == to compare arrays two arrays a & b returns whether they point to the same memory address int[] a = {4, 5, 6}; int[] b = {4, 5, 6}; //a == b is false!

Comparing Arrays To determine whether two arrays hold the same data, compare the two arrays element by element Lab 7 equals method

What is the Output? int a = 7; int b = a; b = 8; System.out.println("A: " + a); System.out.println("B: " + b); //a is not changed by this Output A = 7 B = 8

This is like giving the array two names (a & b) What is the Output? int[] a = {4, 5, 6}; int[] b = a; b[0] = 3; System.out.println("a: {" + a[0] + ", " + a[1] + ", " + a[2] + "}"); System.out.println("b: {" + b[0] + ", " + b[1] + ", " + b[2] + "}"); This is like giving the array two names (a & b) //b holds same memory address as a //we’re changing both b & a! Output a = {3, 5, 6} b = {3, 5, 6}

Array Assignment 4 5 6 int[] a = {4, 5, 6}; int[] b = a; b[0] = 3; a 3

Copying Arrays int[] a = {4, 5, 6}; int[] b = new int[a.length]; //create a new array b //copy a’s entries into b for(int i = 0; i < b.length; b++) { b[i] = a[i]; }

What is the Output? public void changeNumber(int num) { num = 7; } public static void main(String[] args) { int a = 9; changeNumber(a); System.out.println("a = " + a); int num = 9; changeNumber(num); System.out.println("num = " + num); Output a = 9 num = 9

What is the Output? public void changeNumber(int num) { num = 7; } public static void main(String[] args) { int[] a = {4, 5, 6}; changeNumber(a[0]); System.out.println("a: {" + a[0] + ", " + a[1] + ", " + a[2] + "}"); Output a = {4, 5, 6}

What is the Output? public void changeArray(int[] array) { a[0] = 7; } public static void main(String[] args) { int[] a = {4, 5, 6}; changeArray(a); System.out.println("a: {" + a[0] + ", " + a[1] + ", " + a[2] + "}"); Output a = {7, 5, 6}

What is the Output? public void changeArray(int[] a) { a = new int[3]; } public static void main(String[] args) { int[] a = {4, 5, 6}; changeArray(a); System.out.println("a: {" + a[0] + ", " + a[1] + ", " + a[2] + "}"); Output a = {4, 5, 6}

Programming Demo Sorting (Selection Sort)

Introduction to Sorting Given an array of numbers, sort the numbers into ascending order Input array: Sorted array: 4 7 3 9 6 2 8 2 3 4 6 7 8 9

Selection Sort 4 7 3 9 6 2 8 2 7 3 9 6 4 8 2 3 7 9 6 4 8 …

Pseudocode for i = 0 to array.length - 1 2 3 7 9 6 4 8 Find the index s of the smallest element starting at index i Swap elements i & s in the array 2 3 7 9 6 4 8 i s

Programming Demo Programming

Wednesday Multi-Dimensional Arrays