NotesWeek8.Oct 11-15 - CS 180 Problem Solving and Object...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS 180 Problem Solving and Object Oriented Programming Fall 2010 hLp://www.cs.purdue.edu/homes/apm/courses/CS180Fall2010/ This Week: Notes for Week8: Oct 1115, 2010 Aditya Mathur 10/11 October break [No class] 10/13 1. Department of Computer Science Purdue University West LafayeLe, IN, USA Arrays and loops [again] 2. Arrays: mulTdimensional 3. Methods 4. CreaTng GUIs Readings and Exercises for Week 8 Readings: 1. Review Strings and various methods 2. Arrays: 6.6, 6.7.1 3. GUI: 12.2, 12.3 Exercises: 6.9, 6.13 10/13/2010 Aditya Mathur. CS 180. Fall 2010. Week 8 2 Special class Sunday October 17: 45:30pm LWSN 3102AB For those who have not been able to perform well on Exam 1 and/or Project 1/2. Send me email if you wish to aLend. Bring your laptops. 10/13/2010 Aditya Mathur. CS 180. Fall 2010. Week 8 3 Lunch meeTng When: Thursday October 14, 2010. Noon1:30pm Where: Ford Dining Hall Meet: Upstairs in the Ford dining hall ALendees: All who signed up but have not yet aLended a lunch meeTng Look forward to seeing you! 10/13/2010 Aditya Mathur. CS 180. Fall 2010. Week 8 4 Arrays: More of them [FreeFoto.com 10/13/2010 Aditya Mathur. CS 180. Fall 2010. Week 8 5 Arrays: Visual representaTon 10/13/2010 Aditya Mathur. CS 180. Fall 2010. Week 8 6 Single dimensional array: Example 1 int cloudyDays=new int [12] // average cloudy days/month Anchorage, Alaska 0 1 2 3 4 5 6 7 8 9 10 11 [19 18 18 18 20 20 22 21 21 21 20 21 ] Index Data cloudyDays[0] cloudyDays[5] cloudyDays[11] 10/13/2010 Aditya Mathur. CS 180. Fall 2010. Week 8 7 Single dimensional array: Example 2: DeclaraTon Car carStock=new Car [5]; // Cars at a dealership public class Car{ String make; // e.g. Ford String model; // e.g. Fusion int year; // e.g. 2011 long msrp; // e.g. US$23145 Picture carPic; } 10/13/2010 Aditya Mathur. CS 180. Fall 2010. Week 8 8 Single dimensional array: Example 2: Visual Car carStock=new Car[5]; // Cars at a dealership CS180 Dealership in West LafayeLe 0 1 2 3 4 [ ] Index Object Reference {Ford, fusion, 2011, 23150 } .... .... .... {Porsche, Boxster Spyder, 2011, 72000, } } carStock[0] 10/13/2010 carStock[4] Aditya Mathur. CS 180. Fall 2010. Week 8 9 Arrays: MulTdimensional 10/13/2010 Aditya Mathur. CS 180. Fall 2010. Week 8 10 Example 1 MEAN MONTHLY CLOUDY DAYS IN ARIZONA 0 1 2 3 4 5 7 10 6 8 11 9 J F M A M J J A S O N D 0 FLAGSTAFF 12 11 12 9 7 4 9 8 5 7 8 11 1 PHOENIX 10 9 8 6 3 2 4 4 3 4 6 9 2 TUCSON 10 9 9 6 4 3 9 7 4 5 6 10 WINSLOW 12 10 9 7 5 4 8 6 4 6 8 10 3 YUMA 9 6 6 4 2 1 3 3 2 3 5 8 4 rows columns int cloudyDays=new int [5][12]; What is the value of cloudyDays[1,8]? 10/13/2010 Aditya Mathur. CS 180. Fall 2010. Week 8 11 DeclaraTon 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 0 1 Avalanche Traverse Accord Camry Fit Corolla 2 Only two columns 0 1 2 Chevy Honda Toyota Civic Rav4 12 10/13/2010 Aditya Mathur. CS 180. Fall 2010. Week 8 Length of an array Book b=new Book[30]; Number of rows can be found using the length aLribute. 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. 10/13/2010 Aditya Mathur. CS 180. Fall 2010. Week 8 13 Problem1 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. IniTalizes all elements of the array to 1 10/13/2010 Aditya Mathur. CS 180. Fall 2010. Week 8 14 SoluTon int [ ][ ] a; // Declare a as a twodimensional 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 for (int i=0; i<n; i++){ // Iterate across all rows } 10/13/2010 for (int j=0; j<m; i++){// Iterate across all columns } a[i][j]=1; // iniTalize an element } Aditya Mathur. CS 180. Fall 2010. Week 8 15 Problem2 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 10/13/2010 Aditya Mathur. CS 180. Fall 2010. Week 8 16 SoluTon 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); } 10/13/2010 Aditya Mathur. CS 180. Fall 2010. Week 8 17 Arrays: Typical runTme errors: Index Out of bounds int a=new int [10]; a[i]=x; // i is greater than 9 or less than 0 Element not iniTalized [Compile Tme error] String name; name[i]="Bob"; // element not iniTalized 10/13/2010 Aditya Mathur. CS 180. Fall 2010. Week 8 18 Arrays: Typical compile Tme error: Element not iniTalized String name; name[i]="Bob"; // element not iniTalized String name; name=new String[5]; name[i]="Bob"; // Now OK if j is in range 10/13/2010 Aditya Mathur. CS 180. Fall 2010. Week 8 19 Graphical User Interfaces 10/13/2010 Aditya Mathur. CS 180. Fall 2010. Week 8 20 A simple GUI: Frame Title Color Border 10/13/2010 Aditya Mathur. CS 180. Fall 2010. Week 8 21 A simple GUI: Frame with a Panel and Four BuLons BuLon label BuLon BuLon Panel Background color BuLon BuLon 10/13/2010 Aditya Mathur. CS 180. Fall 2010. Week 8 22 Live demo 10/13/2010 Aditya Mathur. CS 180. Fall 2010. Week 8 23 Week 8: October 1115, 2010 Hope you enjoyed this week! QuesTons? Contact your recitaTon instructor. Make full use of our office hours. 10/13/2010 Aditya Mathur. CS 180. Fall 2010. Week 8 24 ...
View Full Document

This note was uploaded on 02/05/2012 for the course CS 180 taught by Professor Staff during the Fall '08 term at Purdue University-West Lafayette.

Ask a homework question - tutors are online