CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 8: Oct 10-14, 2011 Aditya Mathur Department of Computer Science Purdue.

Slides:



Advertisements
Similar presentations
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Arrays.
Advertisements

CS 180 Problem Solving and Object Oriented Programming Spring 2011 February 16, 2011 Aditya Mathur Department of Computer Science Purdue University West.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 5: September 19-23, 2011 Aditya Mathur Department of Computer Science Purdue.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 13: Nov 14-18, 2011 Aditya Mathur Department of Computer Science Purdue.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 15: Nov 28-Dec 2, 2011 Aditya Mathur Department of Computer Science Purdue.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 10: Oct 24-28, 2011 Aditya Mathur Department of Computer Science Purdue.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 4: September 12-16, 2011 Aditya Mathur/Tim Korb Department of Computer.
CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 13: Nov 15-19, 2010 Aditya Mathur Department of Computer Science Purdue.
CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 16: Dec 6-10, 2010 Aditya Mathur Department of Computer Science Purdue.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 9: Oct 17-21, 2011 Aditya Mathur Department of Computer Science Purdue.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for the Final Lecture Dec 7, 2011 Aditya Mathur Department of Computer Science Purdue.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 3: September 5-9, 2011 Aditya Mathur Department of Computer Science Purdue.
Multidimensional arrays Many problems require information be organized as a two- dimensional or multidimensional list Examples –Matrices –Graphical animation.
Java Programming, 3e Concepts and Techniques Chapter 5 Arrays, Loops, and Layout Managers Using External Classes.
Arrays Recitation – 10/(9,10)/2008 CS 180 Department of Computer Science, Purdue University.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 8 Multidimensional.
CS 106 Introduction to Computer Science I 02 / 22 / 2008 Instructor: Michael Eckmann.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
1 11/8/06CS150 Introduction to Computer Science 1 Arrays Chapter 8 page 477 November 13, 2006.
CS 106 Introduction to Computer Science I 10 / 09 / 2006 Instructor: Michael Eckmann.
CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 7: Oct 4-8, 2010 Aditya Mathur Department of Computer Science Purdue University.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 7: Oct 3-7, 2011 Aditya Mathur Department of Computer Science Purdue University.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 11: Oct 31-Nov 4, 2011 Aditya Mathur Department of Computer Science Purdue.
CPS120: Introduction to Computer Science Arrays. Arrays: A Definition A list of variables accessed using a single identifier May be of any data type Can.
CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 3: September 6-10, 2010 Aditya Mathur Department of Computer Science Purdue.
ARRAYS 1 Week 2. Data Structures  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently 
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.
Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
CHAPTER: 12. Array is a collection of variables of the same data type that are referenced by a common name. An Array of 10 Elements of type double.
Term 2, 2011 Week 1. CONTENTS Problem-solving methodology Programming and scripting languages – Programming languages Programming languages – Scripting.
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
Advanced Computer Science Lesson 4: Reviewing Loops and Arrays Reading User Input.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
1 Objectives ❏ To understand the basic concepts and uses of arrays ❏ To be able to define C arrays ❏ To be able to pass arrays and array elements to functions.
Data Structure CS 322. What is an array? Initializing arrays Accessing the values of an array Multidimensional arrays LAB#1 : Arrays.
CPS120: Introduction to Computer Science Lecture 15 Arrays.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 12: Nov 8-12, 2010 Aditya Mathur Department of Computer Science Purdue.
CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 11: Nov 1-5, 2010 Aditya Mathur Department of Computer Science Purdue University.
Arrays. The array data structure Array is a collection of elements, that have the same data type Integers (int) Floating point numbers (float, double)
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 14: Nov 21-25, 2011 Aditya Mathur Department of Computer Science Purdue.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
CS 178: Programming with Multimedia Objects Aditya P. Mathur Professor of Computer Sciences Purdue University, West Lafayette August 27, 2004 Last update:
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 9: Oct 18-22, 2010 Aditya Mathur Department of Computer Science Purdue.
CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 10: Oct 25-29, 2010 Aditya Mathur Department of Computer Science Purdue.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Multidimensional Arrays Vectors of Vectors When One Is Not Enough.
COMP 110: Spring Announcements Lab 7 was due today Binary Expression Assignment due Friday.
Arrays float Scores[9]; ? index: element // one dimensional array 1.
CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 6: September 27-Oct 1, 2010 Aditya Mathur Department of Computer Science.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
COSC 220 Computer Science II
Multidimensional Arrays
CS Problem Solving and Object Oriented Programming Spring 2019
CS Problem Solving and Object Oriented Programming Spring 2019
CS Problem Solving and Object Oriented Programming Spring 2019
CS Problem Solving and Object Oriented Programming Spring 2019
CS Problem Solving and Object Oriented Programming Spring 2019
CS Problem Solving and Object Oriented Programming Spring 2019
CS Problem Solving and Object Oriented Programming Spring 2019
CS Problem Solving and Object Oriented Programming Spring 2019
CS Problem Solving and Object Oriented Programming Spring 2019
Presentation transcript:

CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 8: Oct 10-14, 2011 Aditya Mathur Department of Computer Science Purdue University West Lafayette, IN, USA October break [No class] 10/11 This Week: 1.Arrays and loops [again] 2.Arrays: multidimensional 3.Methods 4.Creating GUIs 10/13

Readings and Exercises for Week 8 Readings: 1.Review Strings and various methodsStrings 2.Arrays: 6.6, GUI: 12.2, 12.3 Exercises: 6.9, 6.13 ©Aditya Mathur. CS 180. Fall Week 8

Project 3 Team project Start today! ©Aditya Mathur. CS 180. Fall Week 8

Special class Sunday October 17: 4-5:30pm LWSN 3102AB For those who have not been able to perform well on Exam 1 and/or Project 1/2. Send me if you wish to attend. Bring your laptops. ©Aditya Mathur. CS 180. Fall Week 8

Lunch meeting When: Thursday October 14, Noon-1:30pm Where: Ford Dining Hall Meet: Upstairs in the Ford dining hall Attendees:All who signed up but have not yet attended a lunch meeting Look forward to seeing you! ©Aditya Mathur. CS 180. Fall Week 8

Arrays: More of them ©Aditya Mathur. CS 180. Fall Week 8 [FreeFoto.com]

©Aditya Mathur. CS 180. Fall Week 8 Arrays: Visual representation

Single dimensional array: Example 1 int [] cloudyDays=new int [12]; // average cloudy days/month ©Aditya Mathur. CS 180. Fall Week 8 Anchorage, Alaska [ ] cloudyDays[0]cloudyDays[5]cloudyDays[11] Index Data

Single dimensional array: Example 2: Declaration Car [] carStock=new Car [5]; // Cars at a dealership ©Aditya Mathur. CS 180. Fall Week 8 public class Car{ String make; // e.g. Ford String model; // e.g. Fusion int year; // e.g long msrp; // e.g. US$23145 Picture carPic; }

Single dimensional array: Example 2: Visual Car [] carStock=new Car[5]; // Cars at a dealership ©Aditya Mathur. CS 180. Fall Week 8 CS180 Dealership in West Lafayette [ ] carStock[0] carStock[4] Index Object Reference {Porsche, Boxster Spyder, 2011, 72000, } } {Ford, fusion, 2011, } ….

©Aditya Mathur. CS 180. Fall Week 8 Arrays: Multidimensional

Example 1 ©Aditya Mathur. CS 180. Fall Week 8 FLAGSTAFF PHOENIX TUCSON WINSLOW YUMA MEAN MONTHLY CLOUDY DAYS IN ARIZONA J F M A M J J A S O N D int [][] cloudyDays=new int [5][12 ]; rowscolumns What is the value of cloudyDays[1,8]?

Declaration Cars [] inventory=new Car [3][]; 3 rows and undefined number of columns Each row represents the make of a car and column represents the models ©Aditya Mathur. CS 180. Fall Week 8 Chevy0 Honda1 Toyota2 AvalancheTraverse AccordFitCivic CamryCorollaRav4 021 Only two columns

Length of an array Book [][] b=new Book[30][]; Number of rows can be found using the length attribute. int rows=b.length; // number of rows Number of columns is found for each row. For example, int col=b[2].length; Gives us the number of columns in row with index 2. Recall: Number of columns can be different for each row. ©Aditya Mathur. CS 180. Fall Week 8

Problem-1 Write a method named initData() that performs the following tasks: 1.Inputs two integers n and m 2.Creates an array of integers containing n rows and m columns 3.Initializes all elements of the array to 1 ©Aditya Mathur. CS 180. Fall Week 8

Solution int [ ][ ] a; // Declare a as a two-dimensional array public void initData(){ Scanner in=new Scanner(System.in); int n=in.nextInt(); // Get number of rows int m=in.nextInt(); // Get number of columns a=new int[n][m]; for (int i=0; i<n; i++){ // Iterate across all rows for (int j=0; j<m; j++){// Iterate across all columns a[i][j]=1; // initialize an element } } ©Aditya Mathur. CS 180. Fall Week 8

Problem-2 Write a method named findMax() that takes a two dimensional array of integers as input and 1.Finds the maximum value in the array 2.Returns the maximum ©Aditya Mathur. CS 180. Fall Week 8

Solution public int findMax(int a[][]){ int max=Integer.MIN_VALUE; int n=a.length, m=a[0].length; for (int i=0; i<n; i++){ // Iterate across all rows for (int j=0; j<m; i++){// Iterate across all columns if(max<a[i][j]){ max=a[i][j]; } } } return (max); } ©Aditya Mathur. CS 180. Fall Week 8

Arrays: Typical runtime errors: Index Out of bounds int [] a=new int [10]; a[i]=x; // i is greater than 9 or less than 0 Element not initialized [Compile time error] String [] name; name[i]=“Bob”; // element not initialized ©Aditya Mathur. CS 180. Fall Week 8

Arrays: Typical compile time error: Element not initialized String [] name; name[i]=“Bob”; // element not initialized ©Aditya Mathur. CS 180. Fall Week 8 String [] name; name=new String[5]; name[i]=“Bob”; // Now OK if i is in range

©Aditya Mathur. CS 180. Fall Week 8 Graphical User Interfaces

©Aditya Mathur. CS 180. Fall Week 8 A simple GUI: Frame Title Color Border

©Aditya Mathur. CS 180. Fall Week 8 A simple GUI: Frame with a Panel and Four Buttons Button label Button Panel Background color

©Aditya Mathur. CS 180. Fall Week 8 Live demo

Week 8: October 11-15, 2010 Hope you enjoyed this week! Questions? Contact your recitation instructor. Make full use of our office hours. ©Aditya Mathur. CS 180. Fall Week 8