A simple (and generally useless) class - PowerPoint PPT Presentation

About This Presentation
Title:

A simple (and generally useless) class

Description:

Class definition consists of. Method definitions. Declarations of instance variables ... As an exercise, we will design and implement a Name class. Download ... – PowerPoint PPT presentation

Number of Views:24
Avg rating:3.0/5.0
Slides: 18
Provided by: DonSl1
Category:

less

Transcript and Presenter's Notes

Title: A simple (and generally useless) class


1
A simple (and generally useless) class
  • The Laugher class
  • Download Laugher.zip demo
  • Three versions
  • Laugher1
  • Laugher2
  • Laugher3

2
Laugher1 Constructors and Methods
  • Basic class definition
  • Constructor
  • Single method laugh()
  • No instance variables
  • In the method
  • Identify the prototype
  • Identify the body
  • Identify the label
  • Identify the return type
  • What does the constructor do?
  • Guarantees that the object starts its life with a
    sensible set of values in its instance variables
  • In this case, no instance variables, ctor does
    nothing

3
Laugher2 Parameters and Overloading
  • There are two methods with the same name laugh()
    -gt overloading
  • What is different about them?
  • Label?
  • Return type?
  • Parameter list?
  • What difference does the parameter make?

4
Laugher3 Instance Variables
  • A String variable inside the class but outside
    any methods
  • Every method and ctor has access to it
  • Maintains the state of the object (what is the
    current defaultSyllable?)
  • Made private so that only class methods have
    access to it

5
Defining Classes
  • Class definition consists of
  • Method definitions
  • Declarations of instance variables
  • We will see these today
  • Method definition consists of
  • Prototype of the method
  • Method body (sequence of Java statements)
  • Prototype consists of
  • Return type
  • Name of the methods
  • Parameter list
  • A constructor is the same as a method
  • Except it lacks a return type

6
Public vs Private
  • Methods and instance variables are labeled public
    or private
  • To permit (public) or prevent (private) access
    from code outside of the class
  • What is labeled private?
  • Instance variables
  • Helper methods purpose to help implement other
    methods
  • What is labeled public?
  • Methods that are part of the interface
  • In other words, that define the behavior of the
    class

7
Kinds of variables
  • Parameters
  • Variables declared within a method prototype
  • Initialized with the values given by the
    arguments of the invoking message
  • Local variables
  • Variables declared within the body of a method
  • Must be initialized explicitly by the program
  • Both local variables and parameters are
  • Created when the method is invoked
  • Destroyed when the method returns
  • Local to the method that invokes them
  • Cannot be seen by other methods or parts of the
    program
  • Different methods can have local variables and
    parameters of the same name

8
Kinds of variables
  • Instance variables
  • Declared outside of method bodies
  • Created when the object is created
  • Destroyed when the object is destroyed
  • Used to preserve information between method
    invocations
  • Each method has access to an instance variable
  • Values of the instance variables determine the
    state of the object

9
Static methods
  • Methods not associated with any particular object
  • They are associated with the class as a whole
  • main() is a static method

10
Designing a class Process
  • Decide on the behavior the class will provide
  • Starting to think about methods
  • Determine the way the class will be used
  • Interface
  • Think about the prototypes of the methods
  • Write a sample program using the class
  • Does the design make sense so far?
  • Write a skeleton of the class definition
  • Class definition shell
  • Prototypes and empty method bodies

11
InteractiveIO Class
  • A class to be used to get information from the
    user and display information to the monitor
  • Determine the behavior
  • Write a message to the monitor
  • Prompt for and read a string from the keyboard

12
InteractiveIO Class
  • Interface and Prototypes
  • Declare an InteractiveIO reference
  • InteractiveIO interIO
  • Instantiate (create) a new InteractiveIO object
  • interIO new InteractiveIO()
  • public interactiveIO()
  • Send the object a message to write a String to
    the monitor
  • interIO.write(Send this message to monitor.)
  • public void write(String str)
  • Prompt for and read a String from the keyboard
  • String str
  • str interIO.promptAndRead(Enter a name )
  • public String promptAndRead(String str)

13
InteractiveIO Class
  • Sample Program
  • import java.io.
  • class tryInteractiveIO
  • public static void main(String arg)
  • throws Exception
  • InteractiveIO interIO
  • String line
  • interIO new InteractiveIO()
  • line interIO.promptAndRead(Enter a word
    )
  • interIO.write(line)

14
InteractiveIO Class
  • Class definition skeleton
  • class InteractiveIO
  • // instance variables if we need them
  • public InteractiveIO // ctor
  • // statements
  • // write str to monitor
  • public void write(String str)
  • // statements
  • // write str to monitor, read String from
    keyboard,
  • // return reference to it
  • public String promptAndRead(String str)
  • // statements

15
Implementing InteractiveIO
  • Writing the method bodies
  • Start working an any methods
  • You can stop working on one method to go work on
    another
  • Download InteractiveIO.zip demo
  • Look at improvements to InteractiveIO found in
    class InteractiveIO2

16
Name class
  • As an exercise, we will design and implement a
    Name class
  • Download Name.zip demo
  • Include methods to print a name and allow
    interactive input of a name
  • To print we need the target passed as parameter
  • To read, we will use a static method

17
Static methods
  • Defined in the same way as other methods, except
    for the keyword static as a label
  • This method belongs to the class, not to a
    particular instance of it
  • Must be invoked independently of any instance of
    the class
  • Use the class name as the receiver of the message
  • Name.read(brKey)
  • May not access any instance variables
  • which belong to particular instances of a class
Write a Comment
User Comments (0)
About PowerShow.com