Presentation is loading. Please wait.

Presentation is loading. Please wait.

March 2005 1/18R. Smith - University of St Thomas - Minnesota QMCS 230: Today in Class A note on methodsA note on methods ObjectsObjects Creating your.

Similar presentations


Presentation on theme: "March 2005 1/18R. Smith - University of St Thomas - Minnesota QMCS 230: Today in Class A note on methodsA note on methods ObjectsObjects Creating your."— Presentation transcript:

1 March 2005 1/18R. Smith - University of St Thomas - Minnesota QMCS 230: Today in Class A note on methodsA note on methods ObjectsObjects Creating your own ‘object’Creating your own ‘object’ Lab 13Lab 13

2 March 2005 2/18R. Smith - University of St Thomas - Minnesota ‘Method calls’ A question about methodsA question about methods It’s not enough to just write the methodIt’s not enough to just write the method You have to “call” it from another methodYou have to “call” it from another method Otherwise it never, ever runsOtherwise it never, ever runs To call itTo call it –Type its name and an open parenthesis Give the list of variables and literals to pass to itGive the list of variables and literals to pass to it DO NOT include types (except as casts)DO NOT include types (except as casts) –Close parenthesis, semicolon

3 March 2005 3/18R. Smith - University of St Thomas - Minnesota What do we mean by ‘objects’ ?? It’s computer science jargonIt’s computer science jargon It’s a way of combining some data with some methods to manipulate itIt’s a way of combining some data with some methods to manipulate it –You can customize the methods to the data being stored It’s a “controlled” way of storing dataIt’s a “controlled” way of storing data –You can save data in one method and pass it to another It’s a metaphor (boy, is it!)It’s a metaphor (boy, is it!) –Objects are the basis for all sorts of symbolic modeling of real world phenomena using computer based logic –You can create models of anything you can talk about

4 March 2005 4/18R. Smith - University of St Thomas - Minnesota Working with Objects Dot NotationDot Notation Object name “dot” method (parentheses);Object name “dot” method (parentheses); –Applies that “method” to that “object” –If there are parameters in parentheses, they’re applied too Examples:Examples: –string1.equals(“string2”); –outputfile.println(“Writing this to the output file”); –inputfile.exists()

5 March 2005 5/18R. Smith - University of St Thomas - Minnesota We use “Classes” to define Objects The class is the ‘template’ for the objectThe class is the ‘template’ for the object –Think of a cookie cutter – the class is the cutter, the object is the cookie –Think of a printing press – the class is the press and the object is the printed page –Or something We’ve been doing non-object classesWe’ve been doing non-object classes What’s in a “real” class for an object?What’s in a “real” class for an object? –Fields – variables that hold the object’s data –Methods for implementing the object’s behavior Constructors – create a new object of that class (“new”)Constructors – create a new object of that class (“new”) Setters or mutators – changes variables in the objectSetters or mutators – changes variables in the object Getters or accessors – retrieve variables from the objectGetters or accessors – retrieve variables from the object

6 March 2005 6/18R. Smith - University of St Thomas - Minnesota An example object A carpet for a room (Lab 8)A carpet for a room (Lab 8) What are the “fields?”What are the “fields?” What setters and getters would we have?What setters and getters would we have? Methods hinted at by the labMethods hinted at by the lab –Setter with width, length, cost, room name –Getters: Retrieve the total priceRetrieve the total price Print out the room description and carpet costPrint out the room description and carpet cost

7 March 2005 7/18R. Smith - University of St Thomas - Minnesota Really creating an object Class name = object name = file name.javaClass name = object name = file name.java Then come variable declarationsThen come variable declarations –Outside of all those methods –Can declare primitives or objects Then come methodsThen come methods –Constructors have the object’s name OverloadingOverloading –We can have different methods with the same name –Each must have a different list of arguments –Java sorts them out according to how they are ‘called’ Trying it with the Carpet objectTrying it with the Carpet object

8 March 2005 8/18R. Smith - University of St Thomas - Minnesota A Bank Account Object What the object doesWhat the object does –Keeps track of the balance –Keeps track of the most recent transaction FieldsFields –balance – a double with the current balance –lastValue – a double with amount of last transaction (+/-) –lastMemo – string describing the last transaction Constructor – start with zero balanceConstructor – start with zero balance Mutator – do a transactionMutator – do a transaction AccessorsAccessors –Retrieve/print recent transaction information –Retrieve the balance

9 March 2005 9/18R. Smith - University of St Thomas - Minnesota Lab 13 – Bank Account Take a file containing transactionsTake a file containing transactions –One line per transaction –Line starts with a signed double – the amount –Rest of line is the ‘memo’ explaining the transaction For each transactionFor each transaction –Update the bank balance –Write the balance and ‘memo’ to System.out When end of file reached, write a “Final Balance” messageWhen end of file reached, write a “Final Balance” message Use a BankAccount objectUse a BankAccount object

10 March 2005 10/18R. Smith - University of St Thomas - Minnesota How to do it Create two filesCreate two files –Java file for lab 13 “main” –Java file for BankAccount object Create a third file “lab13data.txt”Create a third file “lab13data.txt” –Fill it with transactions. –First should have a positive number Memo: “Starting Balance”Memo: “Starting Balance” –Rest start with positive or negative numbers, each with memo Goal: make “Main” as short as possibleGoal: make “Main” as short as possible –Must still meet formatting and commenting requirements –Main should have the Scanner and the input loop –Everything else should be in the BankAccount methods

11 March 2005 11/18R. 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 1/18R. Smith - University of St Thomas - Minnesota QMCS 230: Today in Class A note on methodsA note on methods ObjectsObjects Creating your."

Similar presentations


Ads by Google