Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 91 Introduction to Programming Ms. Knudtzon C Period Quarter 2 – Lecture 20 Monday, November 1 st.

Similar presentations


Presentation on theme: "Week 91 Introduction to Programming Ms. Knudtzon C Period Quarter 2 – Lecture 20 Monday, November 1 st."— Presentation transcript:

1 Week 91 Introduction to Programming Ms. Knudtzon C Period Quarter 2 – Lecture 20 Monday, November 1 st

2 Week 92 Debugging and Testing What is debugging? What is testing?

3 Week 93 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)

4 Week 94 Debugging Debugging generally comes after testing Is the attempt to pinpoint and fix the source of an error

5 Week 95 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

6 Week 96 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)

7 Week 97 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

8 Week 98 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

9 Week 99 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 MailItem s in the mail-system project. Make sure printing messages also prints the subject line. Modify the mail client accordingly. (Exercise 3.43)

10 Week 910 Introduction to Programming Ms. Knudtzon C Period Quarter 2 – Lecture 21 Thursday, November 4 th

11 Week 911 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

12 Week 912 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

13 Week 913 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]

14 Week 914 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]

15 Week 915 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)

16 Week 916 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

17 Week 917 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[ ]

18 Week 918 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];

19 Week 919 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

20 Week 920 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};

21 Week 921 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; }

22 Week 922 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 months String 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”); }

23 Week 923 Histogram Program ArrayEx

24 Week 924 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]); }

25 Week 925 Homework 10 Will be posted today on the webpage Due date to be determined (Probably November 12) –Get started this weekend!


Download ppt "Week 91 Introduction to Programming Ms. Knudtzon C Period Quarter 2 – Lecture 20 Monday, November 1 st."

Similar presentations


Ads by Google