Barb Ericson Georgia Institute of Technology Dec 2009

Slides:



Advertisements
Similar presentations
Introduction to Eclipse. Start Eclipse Click and then click Eclipse from the menu: Or open a shell and type eclipse after the prompt.
Advertisements

COMPUTER PROGRAMMING I Essential Standard 5.02 Understand Breakpoint, Watch Window, and Try And Catch to Find Errors.
ManipulatingPictures-Mod6-part21 Manipulating Pictures, Arrays, and Loops part 2 Barb Ericson Georgia Institute of Technology.
Georgia Institute of Technology DrJava Appendix A Barb Ericson Georgia Institute of Technology May 2006.
The IDE (Integrated Development Environment) provides a DEBUGGER for locating and correcting errors in program logic (logic errors not syntax errors) The.
CPSC1301 Computer Science 1 Chapter 11 Creating Classes part 1.
Debugging Projects Using C++.NET Click with the mouse button to control the flow of the presentation.
Author: Loh Jianxiong Christopher Editors: Chua Jie Sheng, Li Mengran, Peh Shao Hong, Oo Theong Siang.
CreatingClasses-part11 Creating Classes part 1 Barb Ericson Georgia Institute of Technology Dec 2009.
CPSC1301 Computer Science 1 Overview of Dr. Java.
Georgia Institute of Technology Creating Classes part 1 Barb Ericson Georgia Institute of Technology Oct 2005.
Debugging. 2 © 2003, Espirity Inc. Module Road Map 1.Eclipse Debugging  Debug Perspective  Debug Session  Breakpoint  Debug Views  Breakpoint Types.
Debugging in Java. Common Bugs Compilation or syntactical errors are the first that you will encounter and the easiest to debug They are usually the result.
CSC1401 Classes - 1. Learning Goals Computing concepts Identifying objects and classes Declaring a class Declaring fields Default field values.
Georgia Institute of Technology Manipulating Pictures, Arrays, and Loops Barb Ericson Georgia Institute of Technology August 2005.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
CreatingSubclasses1 Barb Ericson Georgia Institute of Technology Dec 2009.
Georgia Institute of Technology Creating Classes part 4 Barb Ericson Georgia Institute of Technology May 2006.
CreatingClasses-SlideShow-part41 Creating Classes part 4 Barb Ericson Georgia Institute of Technology Dec 2009.
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
Georgia Institute of Technology Creating Classes part 2 Barb Ericson Georgia Institute of Technology June 2006.
Georgia Institute of Technology More on Creating Classes part 1 Barb Ericson Georgia Institute of Technology Oct 2005.
Debugging What coders (programmers) do to find the errors (“bugs”) in their own programs “Bugs” – Admiral Grace Hopper, developer of the world’s first.
04-ManipulatingPictures-part21 Manipulating Pictures, Arrays, and Loops part 2 Barb Ericson Georgia Institute of Technology June 2008.
Georgia Institute of Technology More on Creating Classes Barb Ericson Georgia Institute of Technology June 2006.
Debuggers. Errors in Computer Code Errors in computer programs are commonly known as bugs. Three types of errors in computer programs –Syntax errors –Runtime.
Debugging with Eclipse
Appendix A Barb Ericson Georgia Institute of Technology May 2006
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
Manipulating Pictures, Arrays, and Loops part 2
Manipulating Pictures, Arrays, and Loops part 2
Appendix A Barb Ericson Georgia Institute of Technology May 2006
Debugging Dwight Deugo
Manipulating Pictures, Arrays, and Loops part 3
Testing and Debugging.
Computer Programming I
Debugging CMSC 202.
LESSON 20.
Creating and Modifying Text part 2
Important terms Black-box testing White-box testing Regression testing
Important terms Black-box testing White-box testing Regression testing
Manipulating Pictures, Arrays, and Loops part 2
Manipulating Pictures, Arrays, and Loops part 2
Manipulating Pictures, Arrays, and Loops
Debugging with Eclipse
Barb Ericson Georgia Institute of Technology August 2005
Introduction to Processing Digital Sounds part 2
Manipulating Pictures, Arrays, and Loops part 5
Introduction to Java part 2
Tonga Institute of Higher Education
Debugging Taken from notes by Dr. Neil Moore
Teaching Java using Turtles part 3
Introduction to Processing Digital Sounds part 3
Barb Ericson Georgia Institute of Technology Oct 2005
Workshop for Programming And Systems Management Teachers
Manipulating Pictures, Arrays, and Loops
Debugging “Why you were up till 2AM”
Debugging Taken from notes by Dr. Neil Moore
Debugging Visual Basic Programs
Barb Ericson Georgia Institute of Technology June 2005
Barb Ericson Georgia Institute of Technology May 2006
Debugging Dwight Deugo
Introduction to Java part 2
More on Creating Classes
Manipulating Pictures, Arrays, and Loops
Barb Ericson Georgia Institute of Technology Oct 2005
More on Creating Classes part 3
Corresponds with Chapter 5
Debugging with Eclipse
Workshop for Programming And Systems Management Teachers
Presentation transcript:

Barb Ericson Georgia Institute of Technology Dec 2009 Creating Classes part 2 Barb Ericson Georgia Institute of Technology Dec 2009 CreatingClasses-SlideShow-part2

CreatingClasses-SlideShow-part2 Learning Goals Computing concepts Declaring a constructor Overloading constructors Adding a no-argument constructor Using a debugger to step through execution How to start the debugger How to set a breakpoint How to check the value of a variable or field CreatingClasses-SlideShow-part2

CreatingClasses-SlideShow-part2 Constructors Are used to initialize the fields of an object To other than the default values or assigned values You can have more than one constructor As long as the parameter lists are different This is called overloading constructors Syntax visibility ClassName(paramList) {} Example public SlideShow(Picture[] pictArray) { this.pictureArray = pictArray; } Constructors differ from methods in that they must have the same name as the class and they can not have a return type. CreatingClasses-SlideShow-part2

CreatingClasses-SlideShow-part2 Creating 1D Arrays You can declare an array using Type[] arrayName; You can create an array using new Type[size]; You can declare an array and create it at the same time Type[] arrayName = new Type[size]; Array indices start at 0 and end at length – 1 You can get the length of an array using arrayName.length You can add an element to an array using name[index] = Object; Length is a public field of the array. CreatingClasses-SlideShow-part2

CreatingClasses-SlideShow-part2 Add a Constructor Add the following after the field declarations to SlideShow.java: public SlideShow(Picture[] pictArray) { this.pictureArray = pictArray; } Compile and test SlideShow showObj = new SlideShow(); You can leave off the ‘this.’ on ‘this.name’ and the compiler will add it for you. CreatingClasses-SlideShow-part2

CreatingClasses-SlideShow-part2 Why did you get an Error? We hadn’t declared any constructors before we added this one But a constructor is called each time a new object is created We didn’t provide one so the compiler added a no-argument constructor One that takes no parameters and leaves the fields with their default or assigned values But once you add a constructor The compiler will not add any for you So now you get an error when you try to use a no-argument constructor CreatingClasses-SlideShow-part2

Adding a No-Argument Constructor Add the following constructor to the SlideShow class public SlideShow() {} Now test it again with: SlideShow showObj = new SlideShow(); System.out.println(showObj); Also try: Picture[] pictArray = new Picture[5]; pictArray[0] = new Picture(FileChooser.getMediaPath("beach.jpg")); pictArray[1] = new Picture(FileChooser.getMediaPath("blueShrub.jpg")); pictArray[2] = new Picture(FileChooser.getMediaPath("church.jpg")); pictArray[3] = new Picture(FileChooser.getMediaPath("eiffel.jpg")); pictArray[4] = new Picture(FileChooser.getMediaPath("greece.jpg")); SlideShow vacShow = new SlideShow(pictArray); System.out.println(vacShow); When we create student1 the no-argument constructor will be called since we haven’t passed any parameters. When we create student2 the constructor will be called that takes a string. CreatingClasses-SlideShow-part2

CreatingClasses-SlideShow-part2 Tracing Execution One way to trace what is happening in your program is To add System.out.println() statements Add these to print out the value of the picture array both before and after it is set System.out.println(this.pictureArray); this.pictureArray = pictArray; CreatingClasses-SlideShow-part2

CreatingClasses-SlideShow-part2 Debuggers You can use a debugger to find the cause of bugs (errors in your program) A moth caused one bug http://www.jamesshuggins.com/h/tek1/first_computer_bug.htm And to trace execution to see what is happening Which constructor is executed or what method is executed What values are in the fields CreatingClasses-SlideShow-part2

CreatingClasses-SlideShow-part2 DrJava’s Debugger Click on Debugger in the menu Then check the Debug Mode checkbox Stack and Threads Area Watches and Breakpoints Area Check values here CreatingClasses-SlideShow-part2

CreatingClasses-SlideShow-part2 Setting a Breakpoint When you use a debugger you often want to set places to stop execution Each place to stop at is a breakpoint Once execution has stopped there You can check the value of parameters and fields To set a breakpoint Right click on a line of code Pick “Toggle Breakpoint” It will be highlighted in red All debuggers let you set breakpoints. CreatingClasses-SlideShow-part2

CreatingClasses-SlideShow-part2 Showing a Breakpoint Lines with breakpoints are highlighted in red in DrJava Set a breakpoint at the line that sets the picture array CreatingClasses-SlideShow-part2

CreatingClasses-SlideShow-part2 Testing a Breakpoint Try the constructor again that takes an array of pictures Execution should stop at the breakpoint And the color will change to blue CreatingClasses-SlideShow-part2

CreatingClasses-SlideShow-part2 Checking Values Execution stops before the breakpoint line is executed So the array hasn't been set yet Check this by printing out the value of it in the interactions pane this.pictureArray Then click on the Step Over button To let the current line of code be executed And check the values again Remember that in DrJava if you type something without a semicolon at the end it will print the value of the expression. CreatingClasses-SlideShow-part2

CreatingClasses-SlideShow-part2 Debugging Options Step Over Execute the current line of code and then stop again before you execute the next line of code Step Into If the line of code that we are stopped at has a method call in it stop at the first line in the called method Resume Continue execution at the current point Until the next breakpoint Or the program ends Step Out Execute the rest of the current method and stop at the first line after the call to this method You can quit debugging by clicking on the X CreatingClasses-SlideShow-part2

Adding a Constructor Exercise Create another constructor in the SlideShow class That takes both the array of pictures and the time to wait between pictures public SlideShow(Picture[] pictArray, int time) Use the debugger to check what happens during execution of this constructor CreatingClasses-SlideShow-part2

CreatingClasses-SlideShow-part2 Summary Constructors initialize the fields in an object To declare a constructor public ClassName(paramList) {} No return type Same name as the class You can overload constructors The parameter lists must be different Use a debugger To watch what happens during execution CreatingClasses-SlideShow-part2