Presentation is loading. Please wait.

Presentation is loading. Please wait.

March 2005 1R. Smith - University of St Thomas - Minnesota QMCS 230: Today in Class Tomorrow’s ExamTomorrow’s Exam Today’s LabToday’s Lab SortingSorting.

Similar presentations


Presentation on theme: "March 2005 1R. Smith - University of St Thomas - Minnesota QMCS 230: Today in Class Tomorrow’s ExamTomorrow’s Exam Today’s LabToday’s Lab SortingSorting."— Presentation transcript:

1 March 2005 1R. Smith - University of St Thomas - Minnesota QMCS 230: Today in Class Tomorrow’s ExamTomorrow’s Exam Today’s LabToday’s Lab SortingSorting

2 March 2005 2R. Smith - University of St Thomas - Minnesota Exam Chapter 4.10-11 – I/OChapter 4.10-11 – I/O Chapter 5 – MethodsChapter 5 – Methods Chapter 6 – Classes and ObjectsChapter 6 – Classes and Objects Chapter 8 – ArraysChapter 8 – Arrays No long programs – a few statements at mostNo long programs – a few statements at most Reading programs – what happens?Reading programs – what happens? Open Book, Open Notes.Open Book, Open Notes.

3 March 2005 3R. Smith - University of St Thomas - Minnesota Chapter 4.10-11 – I/O Role of the different classes and objectsRole of the different classes and objects –File, Scanner –FileWriter, PrintWriter Testing for a file’s existenceTesting for a file’s existence Reading until end of fileReading until end of file Throws clauseThrows clause

4 March 2005 4R. Smith - University of St Thomas - Minnesota Chapter 5 – Methods Calling a methodCalling a method –With or without arguments –What kinds of arguments can we pass? Void vs. value-returning methodsVoid vs. value-returning methods –Calling with/without a value returned –What sorts of values can be returned? Arguments and “side effects”Arguments and “side effects” –What happens when we change an argument? –Primitive vs reference variables Strings vs arraysStrings vs arrays

5 March 2005 5R. Smith - University of St Thomas - Minnesota Declaring a method Mostly we’ve declared the main methodMostly we’ve declared the main method Two situationsTwo situations –Method that stands alone Public static Public static –Method that’s tied to an object Public Public Local variablesLocal variables –Similar to parameters – just exist in the method –Defined locally, results are all local –A “new” will be preserved –Results should be returned via RETURN statement Throwing exceptionsThrowing exceptions

6 March 2005 6R. Smith - University of St Thomas - Minnesota Chapter 6 – Classes and Objects Class – a ‘template’ for an objectClass – a ‘template’ for an object Object – an “instance” of a classObject – an “instance” of a class Instance fieldsInstance fields –Local variables tied to a particular variable –Retain their value over time Constructor – the object’s ‘creation’ methodConstructor – the object’s ‘creation’ method –Normally provided automatically –You can ‘tailor’ it to an object BankAccount: initialize balance to 0BankAccount: initialize balance to 0 Overloading – reusing the method nameOverloading – reusing the method name –Usually refers to changes in the parameter list

7 March 2005 7R. Smith - University of St Thomas - Minnesota Object Features An object – data plus methodsAn object – data plus methods –Some variables with methods tied to them Object’s internal variables = instance fieldsObject’s internal variables = instance fields –We use reference variables to handle objects BankAccount exampleBankAccount example –One object per distinct account The current balanceThe current balance Data about the last transactionData about the last transaction How is this like visiting an ATM?How is this like visiting an ATM? –Methods to modify the balance –Methods to report account’s status Public vs PrivatePublic vs Private –Make Variable declarations PRIVATE –Make Method declarations PUBLIC

8 March 2005 8R. Smith - University of St Thomas - Minnesota Object Methods General featuresGeneral features –Like other methods, but they can use the object’s instance fields –Refer to the object when calling the method –Does “Scanner” have instance fields? ConstructorsConstructors –Special methods named with the Class name –Optional – a ‘built in’ version is always provided –You can tailor it to your object Initializing the balance in BankAccountInitializing the balance in BankAccount

9 March 2005 9R. Smith - University of St Thomas - Minnesota Chapter 8 – Arrays Focus on 1 dimensional arrays on examFocus on 1 dimensional arrays on exam Array declaration and initializationArray declaration and initialization –Fixed size established when the program runs –Initializing with constants –Arrays of strings Array Reference VariablesArray Reference Variables –Changing the reference variable –Changing contents of an array –Passing as a method parameter or result

10 March 2005 10R. Smith - University of St Thomas - Minnesota Array Operations Min, max, average, sumMin, max, average, sum For loops – both typesFor loops – both types –Length attribute – arrayname.length First element is always #0First element is always #0 Files and sentinel valuesFiles and sentinel values –Make the array too large –Mark the end with a sentinel value

11 March 2005 11R. Smith - University of St Thomas - Minnesota LAB: Selection Sort Create a 15-element arrayCreate a 15-element array Fill it with random numbersFill it with random numbers –Use int, float, or double, as you prefer Print it out so it’s readablePrint it out so it’s readable Sort it using the selection sortSort it using the selection sort Print it out againPrint it out again Write the selection sort as a separate methodWrite the selection sort as a separate method Write the printout procedure as a separate methodWrite the printout procedure as a separate method

12 March 2005 12R. Smith - University of St Thomas - Minnesota Sorting – Selection Sort I know a lot of algorithmsI know a lot of algorithms –I never remember array sorting algorithms –The problem with arrays – everything is in a fixed location Selection SortSelection Sort –Start at one end Find the lowest value in the arrayFind the lowest value in the array Swap it with the first elementSwap it with the first element –Repeat the process for the array minus the first element

13 March 2005 13R. Smith - University of St Thomas - Minnesota Selection Sort Example Min = 12Min = 12 1 4 5 8 8 1212

14 March 2005 14R. Smith - University of St Thomas - Minnesota Coding the selection sort Two loopsTwo loops Outer loop looks at subsets of the arrayOuter loop looks at subsets of the array Inner loop does “min” on remaining arrayInner loop does “min” on remaining array JavadocJavadoc –Basics: p. 77-78 –@param tag: p. 259 –@return tag: p. 266-7

15 March 2005 15R. Smith - University of St Thomas - Minnesota That’s it. Questions?Questions? Creative Commons License This work is licensed under the Creative Commons Attribution-Share Alike 3.0 United States License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/us/ or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.


Download ppt "March 2005 1R. Smith - University of St Thomas - Minnesota QMCS 230: Today in Class Tomorrow’s ExamTomorrow’s Exam Today’s LabToday’s Lab SortingSorting."

Similar presentations


Ads by Google