QMCS 230: Today in Class - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

QMCS 230: Today in Class

Description:

You can save data in one method and pass it to another. It's a metaphor (boy, is it! ... create a new object of that class ('new' ... Creative Commons License ... – PowerPoint PPT presentation

Number of Views:17
Avg rating:3.0/5.0
Slides: 12
Provided by: ricks6
Category:
Tags: qmcs | class | license | today

less

Transcript and Presenter's Notes

Title: QMCS 230: Today in Class


1
QMCS 230 Today in Class
  • A note on methods
  • Objects
  • Creating your own object
  • Lab 13

2
Method calls
  • A question about methods
  • Its not enough to just write the method
  • You have to call it from another method
  • Otherwise it never, ever runs
  • To call it
  • Type its name and an open parenthesis
  • Give the list of variables and literals to pass
    to it
  • DO NOT include types (except as casts)
  • Close parenthesis, semicolon

3
What do we mean by objects ??
  • Its computer science jargon
  • Its a way of combining some data with some
    methods to manipulate it
  • You can customize the methods to the data being
    stored
  • Its a controlled way of storing data
  • You can save data in one method and pass it to
    another
  • Its 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
Working with Objects
  • Dot Notation
  • Object name dot method (parentheses)
  • Applies that method to that object
  • If there are parameters in parentheses, theyre
    applied too
  • Examples
  • string1.equals(string2)
  • outputfile.println(Writing this to the output
    file)
  • inputfile.exists()

5
We use Classes to define Objects
  • The 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
  • Weve been doing non-object classes
  • Whats in a real class for an object?
  • Fields variables that hold the objects data
  • Methods for implementing the objects behavior
  • Constructors create a new object of that class
    (new)
  • Setters or mutators changes variables in the
    object
  • Getters or accessors retrieve variables from
    the object

6
An example object
  • A carpet for a room (Lab 8)
  • What are the fields?
  • What setters and getters would we have?
  • Methods hinted at by the lab
  • Setter with width, length, cost, room name
  • Getters
  • Retrieve the total price
  • Print out the room description and carpet cost

7
Really creating an object
  • Class name object name file name.java
  • Then come variable declarations
  • Outside of all those methods
  • Can declare primitives or objects
  • Then come methods
  • Constructors have the objects name
  • Overloading
  • 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 object

8
A Bank Account Object
  • What the object does
  • Keeps track of the balance
  • Keeps track of the most recent transaction
  • Fields
  • 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 balance
  • Mutator do a transaction
  • Accessors
  • Retrieve/print recent transaction information
  • Retrieve the balance

9
Lab 13 Bank Account
  • Take 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 transaction
  • Update the bank balance
  • Write the balance and memo to System.out
  • When end of file reached, write a Final Balance
    message
  • Use a BankAccount object

10
How to do it
  • Create two files
  • Java file for lab 13 main
  • Java file for BankAccount object
  • Create a third file lab13data.txt
  • Fill it with transactions.
  • First should have a positive number
  • Memo Starting Balance
  • Rest start with positive or negative numbers,
    each with memo
  • Goal 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
Thats it.
  • 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.
Write a Comment
User Comments (0)
About PowerShow.com