COMP 110 More arrays, 2D arrays, Program 4 Luv Kohli November 10, 2008 MWF 2-2:50 pm Sitterson 014.

Slides:



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

Copyright © 2002 Pearson Education, Inc. Slide 1.
Arrays.
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
Arrays part 3 Multidimensional arrays, odds & ends.
Arrays Chapter 6 Chapter 6.
ECE122 L14: Two Dimensional Arrays March 27, 2007 ECE 122 Engineering Problem Solving with Java Lecture 14 Two Dimensional Arrays.
Multi-Dimensional Arrays. A Table of Values Balances for Various Interest Rates Compounded Annually (Rounded to Whole Dollar Amounts) Year5.00%5.50%6.00%
1 More on Arrays Passing arrays to or from methods Arrays of objects Command line arguments Variable length parameter lists Two dimensional arrays Reading.
©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.
1 Arrays In many cases we need a group of nearly identical variables. Example: make one variable for the grade of each student in the class This results.
1 COMP 110 More Arrays Tabitha Peck M.S. April 2, 2008 MWF 3-3:50 pm Philips 367.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
Building Java Programs Chapter 7.5
1 Arrays & functions Each element of an array acts just like an ordinary variable: Like any ordinary variable, you can pass a single array element to a.
Multi-Dimensional Arrays in Java "If debugging is the process of removing software bugs, then programming must be the process of putting them in." -- Edsger.
11 Chapter 8 ARRAYS Continued. 22 MULTI-DIMENSIONAL ARRAYS A one-dimensional array is useful for storing/processing a list of values. For example: –The.
© 2011 Pearson Education, publishing as Addison-Wesley 1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 6 focuses.
Chapter 7 Arrays. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Arrays Arrays are objects that help us organize large amounts of information.
Arrays Part 9 dbg. Arrays An array is a fixed number of contiguous memory locations, all containing data of the same type, identified by one variable.
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 Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall
COMP 110 Constructors Luv Kohli October 13, 2008 MWF 2-2:50 pm Sitterson 014.
8-1 Chapter 8: Arrays Arrays are objects that help us organize large amounts of information Today we will focuses on: –array declaration and use –bounds.
© 2004 Pearson Addison-Wesley. All rights reserved October 13, D Arrays ComS 207: Programming I (in Java) Iowa State University, FALL 2006 Instructor:
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
Chapter overview This chapter focuses on Array declaration and use Bounds checking and capacity Arrays storing object references Variable length parameter.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
Data Structure CS 322. What is an array? Initializing arrays Accessing the values of an array Multidimensional arrays LAB#1 : Arrays.
COMP 110 Objects and references Luv Kohli October 8, 2008 MWF 2-2:50 pm Sitterson 014.
Arrays. Collections We would like to be able to keep lots of information at once Example: Keep all the students in the class Grade each one without writing.
Get Longest Run Index (FR) public int getLongestRunIndex(int []values) { int maxRunStart = -1, maxRunLength = 1; int runStart = 0, runLength = 1; for(int.
COMP 110 More loops Luv Kohli September 15, 2008 MWF 2-2:50 pm Sitterson
Week 9 - Wednesday.  What did we talk about last time?  2D arrays  Queen attacking pawn example  Started Game of Life.
COMP 110 Designing and overloading methods Luv Kohli November 3, 2008 MWF 2-2:50 pm Sitterson 014.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Arrays.
Chapter 8: Part 3 Collections and Two-dimensional arrays.
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.
Catie Welsh April 6,  Lab 8 due, Friday by 1pm 2.
COMP 110 Some notes on inheritance, review Luv Kohli December 1, 2008 MWF 2-2:50 pm Sitterson 014.
Multidimensional Arrays Vectors of Vectors When One Is Not Enough.
© 2006 Pearson Addison-Wesley. All rights reserved Arrays of Greater Dimension One-dimensional arrays are linear containers. Multi-dimensional Arrays.
Visual C# 2005 Using Arrays. Visual C# Objectives Declare an array and assign values to array elements Initialize an array Use subscripts to access.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
COMP 110: Spring Announcements Lab 7 was due today Binary Expression Assignment due Friday.
Array contiguous memory locations that have the same name and type. Note: an array may contain primitive data BUT an array is a data structure a collection.
COMP 110 Arrays Luv Kohli November 5, 2008 MWF 2-2:50 pm Sitterson 014.
© 2004 Pearson Addison-Wesley. All rights reserved7-1 Array review Array of primitives int [] count; count = new int[10]; Array of objects Grade [] cs239;
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 9 Introduction to Arrays Fundamentals of Java.
COMP 110 More about classes Luv Kohli October 3, 2008 MWF 2-2:50 pm Sitterson 014.
Beginning C for Engineers Fall 2005 Arrays, 2-D arrays, character strings Bettina Schimanski Lecture 5: Section 2 (9/28/05) Section 4 (9/29/05)
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
Chapter 8 Arrays and the ArrayList Class Multi-Dimensional Arrays.
Arrays 4/4 By Pius Nyaanga. Outline Multidimensional Arrays Two-Dimensional Array as an Array of Arrays Using the length Instance Variable Multidimensional.
COMP 110 More arrays, 2D arrays, Program 4
Two-Dimensional Arrays
Michele Weigle - COMP 14 - Spr 04 Catie Welsh March 30, 2011
COMP Final Exam Review Yi Hong June 15, 2015.
Two-Dimensional Arrays
Multidimensional Arrays
Announcements Lab 7 due Wednesday Assignment 4 due Friday.
Michele Weigle - COMP 14 - Spr 04
Presentation transcript:

COMP 110 More arrays, 2D arrays, Program 4 Luv Kohli November 10, 2008 MWF 2-2:50 pm Sitterson 014

Announcements Lab 7 due, Friday 2pm Final program, Program 4, assigned today, due Monday, December 1, 2pm Electronic Arts (EA) information session ◦ Tuesday, November 11 (tomorrow), 6pm ◦ Sitterson Hall 011 ◦ Free food! ◦ Free games! 2

Questions? 3

Today in COMP 110 More about arrays 2D arrays Program 4 4

Arrays as instance variables public class Weather { private double[] temperature; private double[] pressure; public void initializeTemperature(int len) { temperature = new double[len]; } } Example in jGRASP 5

Arrays of objects When you create an array of objects like this: Student[] students = new Student[35]; Each of the elements of students is not yet an object You have to instantiate each individual one students[0] = new Student(); students[1] = new Student(); …or do this in a loop 6

Smiley[] smilies = new Smiley[3]; for (int i = 0; i < smilies.length; i++) { smilies[i] = new Smiley(); } ??? Arrays of objects 7 true GREEN 3 false BLUE 1 false CYAN 4

Arrays of objects Example in jGRASP 8

Arrays as parameters public void changeArray(int[] arr) { int len = arr.length; arr[len – 1] = 25; }

Arrays as return types public double[] buildArray(int len) { double[] retArray = new double[len]; for (int i = 0; i < retArray.length; i++) { retArray[i] = i * 1.5; } return retArray; } 10

Indexed variables as method arguments No different from 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]); } 11

2D arrays Arrays having more than one index are often useful ◦ Tables ◦ Grids ◦ Bingo games 12 0: Open1: High2: Low3: Close 0: Apple Inc : Walt Disney Co : Google Inc : Microsoft Corp

Declaring and creating 2D arrays int[][] table = new int[4][3]; or int[][] table; table = new int[4][3]; 13

Declaring and creating 2D arrays int[][] table = new int[4][3]; gives you the ability to use table[0][0] table[0][1] table[0][2] table[1][0] table[1][1] table[1][2] table[2][0] table[2][1] table[2][2] table[3][0] table[3][1] table[3][2] 14

How do you use a 2D array? We used a loop to iterate over a 1D array int[] scores = { 13, 57, 93, 60, 102 }; for (int i = 0; i < scores.length; i++) { System.out.println(scores[i]); } 15

How do you use a 2D array? How about a 2D array? int[][] table = new int[4][3]; Use a nested loop for (int row = 0; row < 4; row++) { for (int column = 0; column < 3; column++) { table[row][column] = 37; } } 16

Multidimensional arrays You can have more than two dimensions int[][][] table = new int[4][3][5]; Use more nested loops to access all elements 17

Multidimensional arrays as parameters public void print2DArray(int[][] arr) { for (int row = 0; row < arr.length; row++) { for (int column = 0; column < arr[row].length; column++) { System.out.print(arr[row][column] + " "); } System.out.println(); } 18

Multidimensional arrays as return types public int[][] giveMeAnArray() { int[][] table = new int[4][3]; // put values in the table return table; } 19

length for a 2D array int[][] table = new int[4][3]; table.length is the number of rows, or the integer in the first pair of brackets (4) table[i].length is the number of columns, or the integer in the second pair of brackets (3) 20

Why? Arrays of arrays int[] scores = new int[5]; scores is a one-dimensional array ◦ base type is int int[][] table = new int[4][3]; table is also in fact a one-dimensional array ◦ base type is int[] We still refer to table as a two-dimensional array 21

Program 4: Battleship 22 This program is hard. But you can do it. START EARLY. If you start early, this will be you If you don’t start early, this will be you

Wednesday More array details Possibly an in-class exercise related to Program 4 23