Date post: | 16-Jan-2016 |
Category: |
Documents |
Upload: | moses-manning |
View: | 221 times |
Download: | 0 times |
Week 9 1
Introduction to Programming
Ms. KnudtzonC Period
Quarter 2 – Lecture 20Monday, November 1st
Week 9 2
Debugging and Testing
• What is debugging?
• What is testing?
Week 9 3
Testing
• Is used to find out whether a piece of code (a method, class, or entire program) produces the intended behavior
• Testing well is not easy because there is a lot to think about when testing a program
• Unit testing refers to a test of individual parts of an application (can be done before an application is complete)
Week 9 4
Debugging
• Debugging generally comes after testing
• Is the attempt to pinpoint and fix the source of an error
Week 9 5
Debugger
• A debugger is a software tool that helps in examining how an application executes. It can be used to find bugs.– Lets you execute an application one step at a
time– Lets you start and stop a program at selected
points in the source code, and to examine the value of variables
Week 9 6
Why is an error called a BUG?
• ‘The first computer bug’ – was found inside the Mark II computer by Grace Murray Hopper in 1945– (Can be seen at the
National Museum of American History)
Week 9 7
Debuggers
• Can vary in complexity
• Those for professional developers are much more complex than the simple version we will use in BlueJ
• BlueJ’s debugger is good for novice programmers, and can give a lot of information that can be useful in understanding how the program works
Week 9 8
The this keyword• Used for name overloading – the same name is used for two
different entities– this can be used in methods or in constructors
• Example: (From the Car.java program)String make;public void setMake(String make){
this.make = make;}
• Note: the parameter (make) and the class variable or field (make) share the same name, but are two different containers
• Java specification says that the definition from the closest enclosing block {} will always be used
• The this keyword refers to the current object – so this.make refers to the class variable make, not the parameter make
• Used to make the code more readable
Week 9 9
In class Lab (Teams of 2)
• \\bonebox\users\Knudtzon\Java\mail-system– Exercise 3.31– Exercise 3.33– Exercise 3.34 – Then follow directions and exercises
on pages 73-76
– Time permitting: Add a subject line for an email to MailItems in the mail-system project. Make sure printing messages also prints the subject line. Modify the mail client accordingly. (Exercise 3.43)
Week 9 10
Introduction to Programming
Ms. KnudtzonC Period
Quarter 2 – Lecture 21Thursday, November 4th
Week 9 11
Schedule
• (Went over asterisks program from homework 9)
• Will come back to testing in more detail later this quarter, for now it suffices to test by calling individual methods through the BlueJ object bench
• Was going to begin covering Java API, but since there is no school tomorrow, will first cover arrays
Week 9 12
Grouping objects• Fixed-sized collections
– When you know in advance how many items will be stored in a collection (and that stays fixed for the life of the collection)
– A fixed-sized collection is an array• It’s kind of like a tray of cups – each cup can hold an object
or a primitive data type• Note: the array itself is an object
• Flexible-sized collections– When you don’t know in advance how many items
you will need to store– Will go into more details about this when we look at
the Java API
Week 9 13
Arrays
• An array is a group of variables (called elements or components) containing values that all have the same data type
• To refer to a particular element in an array, use the array’s name and the position number of the element in the array
54
32
2
9
4
3453
34
3
-423
int array called c
c[0]
c[3]
c[4]
c[5]
c[6]
c[7]
c[8]
c[1]
c[2]
Week 9 14
Anatomy of an array
• Array names are the same as any other variable name
• An array with 9 elements (or variables)
• First element has index zero (the zeroth element)
• ith element of array c is c[i -1]• Index must be a positive integer (or
an integer expression that can be promoted to an int)
54
32
2
9
4
3453
34
3
-423
int array called c
c[0]
c[3]
c[4]
c[5]
c[6]
c[7]
c[8]
c[1]
c[2]
Week 9 15
Advantages to using arrays
• It is easy to access individual items in an array (and it’s efficient)
• Arrays are able to store objects or primitive type values (int, double, float, etc)– (We’ll see later that the flexible-sized
collections can only store objects)
Week 9 16
Declaring and creating arrays
• Array objects occupy space in memory. All objects in Java must be created with a keyword new
• When you declare an array you must tell Java the type of the array elements and the number of the elements that will appear in the array
• Example declaration for an integer array called hourArray:
int hourArray[] = new int[24]; // declares and creates an array to hold 24 int
elements
Week 9 17
An array
hourArray
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
:int[ ]
Week 9 18
Other kinds of arrays
• How would you create an array to hold 20 student names?
int jesseArray[] = new int[20];
String nameArray[] = new String[20];
Week 9 19
How do we fill an array?
• All values are initially set to zero when we initialize an array
• One way to fill the array is to use an array initializer, a comma-separated list of expression enclosed in braces: int n[] = {10, 23, 34, 235, 234};
– The array length is automatically determined by the number of elements in the initializer list
Week 9 20
Class Exercise
• Write code to declare and initialize an array to hold the months of a year
String month[] = {“jan”, “feb”, “march”, “april”, “may”};
• Write code to declare and initialize an array to hold the number of days in each month of the year
int monthDays[] = {31,28,31,30};
Week 9 21
How else do we fill an array?
• Arrays and for-loops go together like peanut butter and jelly!
• We could use a for loop and access each array element to set a value to it:int myArray[] = new int[10];
for(int i =0; i < myArray.length; i++){
myArray[i] = 5 * i;
}
Week 9 22
Exercise• Suppose we wish to print out the names and lengths of each of the
months • To do this for each of the twelve months, we place this statement in
the body of a for loop that counts off all the monthsString monthNames[] =
{"January","February","March","April", "May","June","July","August", "September","October","November","December"};
int monthDays[] = {31,28,31,30,31,30,31,31,30,31,30,31};
for(int j = 0; j < 12; j++){System.out.println(“Month: “ + monthNames[j] +
“ has “ + monthDays[j] + “ days”); }
Week 9 23
Histogram Program
• ArrayEx
Week 9 24
Mistakes
• It is not uncommon to make a programming mistake that causes the bounds of a loop to be mis-defined and the loop index variable to stray beyond the range of the array.
• For example, what if in your for loop you wrote:for(int i = 0; i <= array.length; i++){
System.out.println(array[i]);
}
Week 9 25
Homework 10
• Will be posted today on the webpage
• Due date to be determined (Probably November 12)– Get started this weekend!