CS 303E Class 6 Classes and Class Behavior - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

CS 303E Class 6 Classes and Class Behavior

Description:

A driver program to exercise the capabilities of the Time class. ... In Java programs there are normally many classes, but only one of them has the a ... – PowerPoint PPT presentation

Number of Views:46
Avg rating:3.0/5.0
Slides: 13
Provided by: MikeS2
Category:

less

Transcript and Presenter's Notes

Title: CS 303E Class 6 Classes and Class Behavior


1
CS 303E Class 6Classes and Class Behavior
Don't know much geography Don't know much
trigonometry Don't know much about algebra Don't
know what a slide rule is for -Sam Cooke
2
Object Behavior
  • The book uses the example of a bank account
    class.
  • READ IT!! Very good.
  • In class we will use a time class so you have two
    different examples to look at.
  • The time class is contained on the first page of
    notes.
  • The class is used to represent times of the day.
  • It is not complete only a start

3
The Time Class
  • Represents a time of day to nearest minute.
  • What can we do with a time of day or time object
  • increment it to the next minute
  • increment it by some specified amount
  • find out how many minutes have passed since
  • get a String version of the time
  • Not a complete list. Only a starting point.
    Much more could be added.

4
The TimeTester Class
  • A driver program to exercise the capabilities of
    the Time class.
  • Only creates one object but could create many
  • Time timeOne new Time()
  • Once the object created methods may be called to
    manipulate the object or find out about it.
  • timeOne.setTime(1000)

Time Object
timeOne
1000
myMinutes
5
Classes
  • Class
  • are factories used to create objects
  • specify the methods you can use for objects that
    belong to the class
  • contain the implementation detail. The data
    layout for objects and the code for methods.
  • Hold static methods, objects, and primitives.
  • Today we will look at the 3rd item, the
    implementation details

6
The main Method
  • Up till now the only method has been main
  • In Java programs there are normally many classes,
    but only one of them has the a main method.
  • main is where the program starts to execute.
  • The vast majority of classes do not have a main
    method.

7
Other Methods
  • Object behavior is defined by the method in the
    class that created the object
  • Method headers contain the following parts
  • access specifier (public, private, or protected)
  • the return type of the method(int, double,
    String, etc., void)
  • the name of the method (all lower case except
    internal words)
  • A list of parameters the method requires
    (parameters receive the arguments sent when the
    method was called or executed)

8
Method Implementation
  • Inside the method is the code that carries out
    the function or job of the method
  • return is a keyword which causes the method to
    end and send back whatever data (object variable
    or primitive variable) that follows the word
    return.
  • the data type after return must match the return
    type in the method header
  • return in usually the last statement in the
    method unless the return type is void.

9
Instance Variables
  • Instance Variables are contained inside objects
    and are used to describe the status or state of
    the object.
  • Instance variables are defined with
  • an access modifier (private, public, or
    protected)
  • the data type of the variable (double, int,
    String, etc.)
  • The name of the variable (descriptive name, all
    lowercase except capital letters at the start of
    internal words.)

10
More on Instance Variables
  • Instance variables are declared inside the class,
    but outside all of the class methods.
  • Instance variables are almost always private.
  • The variable can only be seen or used by the
    methods inside the class, not by any other
    classes.
  • An objects instance variables may only be changed
    by calling methods on the object.
  • Instance variables may be object variables or
    primitive variables
  • There is no limit on the number of instance
    variables a class / object may have.

11
Parameters
  • Parameters receive the information and data sent
    via arguments.
  • The arguments and parameters must match up in
    number, type, and order
  • Otherwise a syntax error or logic error can
    occur.
  • The parameters listed in the method header are
    the explicit parameters
  • The object the method was called on is sent as an
    implicit parameter.

12
Calling Methods
  • In TimeTester
  • timeOne.increment(10)
  • In Time
  • public void increment(int minutesToAdd)

minutesToAdd
10
this
Time Object
1000
myMinutes
timeOne
Write a Comment
User Comments (0)
About PowerShow.com