NotesWeek7.Oct 4-8

advertisement
CS 180 Problem Solving and Object Oriented
Programming
Fall 2010
http://www.cs.purdue.edu/homes/apm/courses/CS180Fall2010/
This Week:
Notes for Week 7:
Oct 4-8, 2010
Aditya Mathur
Department of Computer Science
Purdue University
West Lafayette, IN, USA
1.
2.
3.
4.
5.
10/6 6.
7.
8.
10/4
Exam 1: Performance
Project 1: Performance
Feedback for Week 6
Arrays
Demo
Incremental program development
Multi-dimensional arrays
Visual depiction of arrays
Readings and Exercises for Week 7
Readings:
Chapter 6: 6.1, 6.2, 6.3, 6.4
Exercises:
6.3, 6.4, 6.5, 6.7
10/6/2010
©Aditya Mathur. CS 180. Fall 2010. Week 7
2
Special help session
Thursday Oct 7, 5-7pm: Special help session in the B158
Sunday October 10: No help session.
Enjoy your break!
10/6/2010
©Aditya Mathur. CS 180. Fall 2010. Week 7
3
Special class
Sunday October 17: 4-5:30pm
For those who have not been able to perform
well on Exam 1 and/or Project 1.
Send me email if you wish to attend. Bring
your laptops.
10/6/2010
©Aditya Mathur. CS 180. Fall 2010. Week 7
4
Lunch meeting
When: Thursday October 7, 2010. Noon-1:30pm
Where: Ford Dining Hall
Meet: Upstairs in the Ford dining hall
Attendees: All who signed up for September 30
and October 7
Look forward to seeing you!
10/6/2010
©Aditya Mathur. CS 180. Fall 2010. Week 7
5
Performance: Exam 1
Overall:
• Count: 231
• Average: 393.3/480
• Max Score: 480 obtained by 11 students
• Median: 417.6
• Standard deviation: 78.2
Part B:
•
•
•
•
•
10/6/2010
Count: 231
Average: 59.7
Max Score: 70/70 scored by 40 students
Median: 63.0
Standard deviation: 11.95
©Aditya Mathur. CS 180. Fall 2010. Week 7
6
Performance: Exam 1: Histogram
10/6/2010
©Aditya Mathur. CS 180. Fall 2010. Week 7
7
Exam 1: Common mistakes: Q1
10/6/2010
•
Incorrect types for coordinates (int instead of float or
double)
3 to -5 points
•
Incorrect or forgotten formatting
-3 points
•
Code in main() method instead of in constructor
-2 points
•
Incorrect coding of the distance formula
-10 to –20 points
©Aditya Mathur. CS 180. Fall 2010. Week 7
8
Exam 1: Common mistakes: Q2
•
•
•
•
Incorrect random number generation (-2 to -15 points)
Minor syntax errors (-1 to -2 points).
Code in main() instead of constructor (-3 points).
Incorrect datatype for min, max (-2 points)
•
Incorrect class structure, like constructor inside main() (-2 to
-5 points)
Not *reading* min and max from user (-5 points)
•
•
10/6/2010
Incorrect if conditions (-2 to -5 points)
©Aditya Mathur. CS 180. Fall 2010. Week 7
9
Performance: Project 1
•
•
•
•
•
•
10/6/2010
Total graded: 222
Average: 60.7
Max score: 64 by 81 students
59—63: 105 students
Median: 62
Standard deviation: 7.23
©Aditya Mathur. CS 180. Fall 2010. Week 7
10
Robots in labs
•
Please take a survey in this week’s lab
•
Please do not toast us!
•
The robots were intended to give you some
“thrill” and help in understanding the challenges
of programming embedded systems.
•
Unfortunately, the Ridge Soft robots turned out
to be unreliable devices!
10/6/2010
©Aditya Mathur. CS 180. Fall 2010. Week 7
11
Feedback for Week 6
10/6/2010
©Aditya Mathur. CS 180. Fall 2010. Week 7
12
Q1.
The lab exercises were useful
(Use scale of 1-10: 10 most useful, 1 not
useful at all)
(a) 8-10
(b) 4-7
(c) 1-3
(d) Missed Week 6 lab
10/6/2010
©Aditya Mathur. CS 180. Fall 2010. Week 7
13
Q2.
The recitation exercises were useful
(Use scale of 1-10: 10 most useful, 1 not
useful at all)
(a) 8-10
(b) 4-7
(c) 1-3
(d) Missed 6 recitation
10/6/2010
©Aditya Mathur. CS 180. Fall 2010. Week 7
14
Q3.
The recitation instructor was helpful.
(Use scale of 1-10: 10 most helpful, 1 not
helpful at all)
(a) 8-10
(b) 4-7
(c) 1-3
(d) Missed week 6 recitation
10/6/2010
©Aditya Mathur. CS 180. Fall 2010. Week 7
15
Q4
Exam 1 was
(a)
(b)
(c)
(d)
(e)
10/6/2010
Very easy
Somewhat easy
Somewhat tough
Tough
Very tough
©Aditya Mathur. CS 180. Fall 2010. Week 7
16
Q5.
I have completed Project 2
(a) Yes
(b) Nearly complete
(c) Not started
10/6/2010
©Aditya Mathur. CS 180. Fall 2010. Week 7
17
Q6.
So far I am liking the course
(a) A lot
(b) Somewhat
(c) Not sure
10/6/2010
©Aditya Mathur. CS 180. Fall 2010. Week 7
18
Arrays
10/6/2010
©Aditya Mathur. CS 180. Fall 2010. Week 7
19
Arrays: What are these?
A homogeneous collection of variables or objects. All
elements of an array are of the same type.
Examples:
Array of integers where each integer represents the age
of a patient.
Array of cars where each element of the array denotes
a specific car.
Array of flowers where each element of the array
denotes a flower.
10/6/2010
©Aditya Mathur. CS 180. Fall 2010. Week 7
20
Arrays: When to use?
When there is a need to retain data in memory for efficient
processing. Data is created once and used many times.
Examples:
Array of flights: Search for a specific flight
Array of cars: Search for a specific car, or find the
average price.
Array of laptops: find the cheapest laptop
10/6/2010
©Aditya Mathur. CS 180. Fall 2010. Week 7
21
Declaring an Array
Indicates age is an array variable
int [] age;
/* age refers to an array of integers; e.g.
age of people in a database*/
double [] weight;
/* weight refers to an array of doubles;
e.g. weights of items shipped*/
String [] name;
/* name refers to an array of elements
each of type String; e.g., names of
people in a database*/
10/6/2010
©Aditya Mathur. CS 180. Fall 2010. Week 7
22
Declaring an Array: of objects
Bird [] bird;
/* Bird refers to a class; bird is an array
where each element is an object of
type Bird. */
Aircraft [] fleet;
/* Aircraft refers to a class; fleet is an
array where each element is an
object of type Aircraft. */
10/6/2010
©Aditya Mathur. CS 180. Fall 2010. Week 7
23
Creating an Array
int [] age=new int[10];
/* age is an array of 10 elements each of type int*/
double [] weight=new double[100];
/* weight is an array of 100 elements each of type double*/
String [] name=new String[15];
/* name is an array of 15 elements each of type String*/
Aircraft [] fleet=new Aircraft[150];
/* fleet is an array of 150 elements each of type Aircraft*/
10/6/2010
©Aditya Mathur. CS 180. Fall 2010. Week 7
24
Accessing an array element: general syntax
Name of the array
Index, starts from 0, must be an int
name [expression]
Examples:
Note: First element of
an array is located at
index 0.
age[i]; // 0<=i<10
fleet[k]; // 0<=k<150
weight[i+j]; // 0<=(i+j)<100
10/6/2010
©Aditya Mathur. CS 180. Fall 2010. Week 7
25
Accessing an array element
int [] age=new int[10];
int p= age[5]; /* p gets element at index 5 of array age*/
double [] weight=new double[100];
double hisWeight=weight[i]; /* hisWeight gets the element of
weight at index i*/
Aircraft [] fleet=new Aircraft[150];
Aircraft rental=fleet[k]/* rental gets the element of fleet
located at index k */
10/6/2010
©Aditya Mathur. CS 180. Fall 2010. Week 7
26
Assigning a value to an array element
int [] age=new int[10];
age[i]=15; /* (i+1)th element of age becomes 15. */
int [] age={-15, 20, 30} // All elements initialized
double [] weight=new double[100];
weight[k]=weight[k]-10; /* (k+1)th element of the weight is
decremented by 10.*/
Aircraft [] fleet=new Aircraft[150];
fleet[i]=new Aircraft(); /* (i+1)th element of fleet gets a new
Aircraft */
10/6/2010
©Aditya Mathur. CS 180. Fall 2010. Week 7
27
Iterating through elements of an array
In many problems we need to iterate through all or a
few of the elements of an array.
Such an iteration is accomplished by using a for or a
while loop.
10/6/2010
©Aditya Mathur. CS 180. Fall 2010. Week 7
28
Problem: Find average
int ageSum=0; // Initialize sum of all ages
int [] age=new int[10]; // Create array of int to hold ages
for( int i=0; i<age.length; i++){
ageSum=ageSum+age[i]; // Accumulate ages
}
double average=ageSum/(double)age.length;
10/6/2010
©Aditya Mathur. CS 180. Fall 2010. Week 7
29
Problem: Search in an array
String [] wList={"Emilio", "Semion", "Osama"};
Scanner s=new Scanner (System.in);
String wanted=s.next(); // Get name to be searched
boolean found=false; // Not yet found
int next=0; // Points to the first element in wanted list
while(next<wList.length && !found) {
if(wanted.equals(wList[nextItem])) // Compare
found=true; // If equal then we have found the wanted!
else
next++; // else point to the next item on the list
}//End of loop
10/6/2010
©Aditya Mathur. CS 180. Fall 2010. Week 7
30
Problem: Search in an array
if(found)
System.out.println(wanted+ " exists in database");
else
System.out.println(wanted+ " not in database")
} // End of program
10/6/2010
©Aditya Mathur. CS 180. Fall 2010. Week 7
31
Problem: Statement
A plant biology lab maintains a collection of leaves.
The lab director wishes to create a simple database that
contains several entries. Each entry is a plant name,
type of its leaf, and a brief description of the leaf in the
collection. Example:
WhitePine Needle NA
Heather Compound NA
Write a program that reads data from a file as
mentioned above and allows a user to search for a tree
in the database.
10/6/2010
©Aditya Mathur. CS 180. Fall 2010. Week 7
32
Problem: Understanding
A plant biology lab maintains a collection of leaves.
From where can my program read the leaf data? If
it is in a file, then what is the name of that file?
The lab director wishes to create a simple database that
contains several entries.
How much data is available?
Write a program that reads data from a file as
mentioned above and allows a user to search for a tree
in the database.
In what form will the user input a request?
10/6/2010
©Aditya Mathur. CS 180. Fall 2010. Week 7
33
Java program for plant biology lab.
Incremental stepwise development!
10/6/2010
©Aditya Mathur. CS 180. Fall 2010. Week 7
34
Tasks
1. Read plant collection data from a file
2. Provide search service to the user
3. Exit with a bye bye message when done.
10/6/2010
©Aditya Mathur. CS 180. Fall 2010. Week 7
35
Let us assume….
1. LeafCollection class is available
2. It provides the following methods:
• createCollection()
• searchService()
3. Leaf class is available. It provides the following
methods:
• public void getTree();
•
•
public void getLeafType();
public void getDescriptionType();
Why did we make the above assumptions?
10/6/2010
©Aditya Mathur. CS 180. Fall 2010. Week 7
36
Overall design: Classes
LeafCollectionServer
Main server
Initiates actions
Uses
LeafCollection
public void createCollection();
public void
10/6/2010
Leaf
private String parentPlant;
private String leafType;
private String leafDescription;
public String getTree();
public String getLeafType();
public String
getDescriptionType();
();
©Aditya Mathur. CS 180. Fall 2010. Week 7
Uses
37
LeafCollectionServer
Back to the Java program. Version 1.0.
10/6/2010
©Aditya Mathur. CS 180. Fall 2010. Week 7
38
LeafCollection: Design
•
This is a class.
•
Provides two methods:
• createCollection()
• searchService()
But before we can write these methods we must
•
have a way to read from a file and save in memory
all the data about the collection!
Question: How should we store leaf collection data?
• Recall: For each leaf, we have its parent tree, its
type, and its description.
10/6/2010
©Aditya Mathur. CS 180. Fall 2010. Week 7
39
LeafCollection: Design Question
Question: How should we store leaf collection data?
•
Recall: For each leaf, we have its parent tree, its
type, and its description.
Answer: ??
10/6/2010
©Aditya Mathur. CS 180. Fall 2010. Week 7
40
LeafCollection
Back to the Java program. Version 2.0.
10/6/2010
©Aditya Mathur. CS 180. Fall 2010. Week 7
41
Leaf: Design: Attributes
Recall, for each leaf, we have the following available
data:
Name of parent tree
Name of the leaf
Description
10/6/2010
©Aditya Mathur. CS 180. Fall 2010. Week 7
42
Leaf: Design: Methods
Recall, for each leaf, we have the following available
data:
public String getLeafName();
public String getTreeName();
public String getDsecription();
10/6/2010
©Aditya Mathur. CS 180. Fall 2010. Week 7
43
Leaf class
Back to the Java program. Version 3.0.
10/6/2010
©Aditya Mathur. CS 180. Fall 2010. Week 7
44
Arrays: Visual representation
10/6/2010
©Aditya Mathur. CS 180. Fall 2010. Week 7
45
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 ]
cloudyDays[0] cloudyDays[5]
10/6/2010
Index
Data
cloudyDays[11]
©Aditya Mathur. CS 180. Fall 2010. Week 7
46
Single dimensional array: Example 2: Declaration
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/6/2010
©Aditya Mathur. CS 180. Fall 2010. Week 7
47
Single dimensional array: Example 2: Visual
Car [] carStock=new Car[5] // Cars at a dealership
CS180 Dealership in West Lafayette
0
1
2
3
[
{Ford, fusion,
2011, 23150
}
carStock[0]
10/6/2010
4
Index
]
….
….
….
Object
Reference
{Porsche, Boxster Spyder,
2011, 72000,
}
}
carStock[4]
©Aditya Mathur. CS 180. Fall 2010. Week 7
48
Arrays: Multidimensional
10/6/2010
©Aditya Mathur. CS 180. Fall 2010. Week 7
49
Example 1
MEAN MONTHLY CLOUDY DAYS IN ARIZONA
0
1
2
3
4
FLAGSTAFF
PHOENIX
TUCSON
WINSLOW
YUMA
0
J
12
10
10
12
9
1
F
11
9
9
10
6
2
M
12
8
9
9
6
3
A
9
6
6
7
4
4
M
7
3
4
5
2
5
J
4
2
3
4
1
6
J
9
4
9
8
3
7
A
8
4
7
6
3
8
S
5
3
4
4
2
rows
9 10 11
O N D
7 8 11
4 6 9
5 6 10
6 8 10
3 5 8
columns
int [][] cloudyDays=new int [5][12]
What is the value of cloudyDays[1,8]?
10/6/2010
©Aditya Mathur. CS 180. Fall 2010. Week 7
50
Declaration
Cars [] inventory=new Car [3][];
3 rows and undefined number of columns
Each row represents make of a car and
column represents models
10/6/2010
0
Chevy
0
1
Avalanche Traverse
1
Honda
Accord
Fit
Civic
2
Toyota
Camry
Corolla
Rav4
©Aditya Mathur. CS 180. Fall 2010. Week 7
2
51
Arrays: Typical errors
•
Index out of bounds [Run time error]
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
10/6/2010
©Aditya Mathur. CS 180. Fall 2010. Week 7
52
Week 7: October 4-8, 2010
Hope you enjoyed this week!
Questions?
Contact your recitation instructor. Make
full use of our office hours.
10/6/2010
©Aditya Mathur. CS 180. Fall 2010. Week 7
53
Download