ObjectOrientedProgrammingSlideshow

Document Sample
ObjectOrientedProgrammingSlideshow Powered By Docstoc
					Object Oriented Programming

             mr. Hanley




                              1
Programs built from smaller
components
 Today’s software programs are generally built
 from pieces of code known as classes
 These classes serve all kinds of different
 purposes, from displaying visual elements on the
 screen to keeping track of files, dates and times,
 words and customers, aliens and heroes
 (including Master Chief of course)


                                                  2
What is a class?



 But what is a class? Aren’t I in a class? I’m confused!!!
 A class is similar to a blue print for a house. It includes
 a set of instructions that describe how a house is made,
 what kind of elements a house will contain as well as
 what kinds of things a the house will be able to do once
 its constructed.

                                                           3
What classes written by other people
have we used so far?
 Scanner
 System
 Random
 Math
 String



                                       4
How are these classes?
 These classes have been provided by various
 programmers and offered to us for free (all right,
 got to love a freebie!!!)
 Each of these is a blue print in java for how a
 specific software component (object) will behave
 Now I’m really confused, can you explain
 further?

                                                      5
Ok, so what is an object?
 An object is like the constructed house, while a
 ____________ is like the blue print.
 Ok, I think I’m starting to get it, can you give me
 an example?
 JTextField is a class, radiusTF is an object
 Inside of a frame, radiusTF is instanciated as a
 JTextField with the following command;
 JTextField radiusTF = new JTextField();

                                                       6
                     radiusTF

An example Object    text “”
                     bounds [120, 80, 50, 20]
                     background color
 So, what happens    Color.grey
 when we create an   foreground color
 object?             Color.black

 Our computer        editable true
                     visible true
 allocates memory
 for the object      public String public
                     getText()     JTextField()
                     public void
                     setText(String
                     txt)

                                                  7
Objects have their      radiusTF

own data                text “”
                        bounds [120, 80, 50, 20]
                        background color
 Each object has its    Color.grey
 own instance           foreground color
 variables or state     Color.black

 This object, for       editable true
                        visible true
 example has a
 background color of    public String public
                        getText()     JTextField()
 grey and is editable   public void
                        setText(String
                        txt)

                                                     8
Objects
communicate via             radiusTF
                            text “Radius will go here”

messages                    bounds [120, 80, 50, 20]

 To control objects, we     background color
                            Color.grey
 send them messages         foreground color
 To set the text for a      Color.black

 JTextField, we send the    editable true
 message setText with a     visible true
 String inside the ()       public String public
 radiusTF.setText(“Radius   getText()     JTextField()

 will go here!”);           public void
                            setText(String
                            txt)

                                                         9
You can also                      radiusTF
                                  text “Radius will go here”

examine the state                 bounds [120, 80, 50, 20]
                                  background color
 An object can report its state   Color.grey
 via a message typically name     foreground color
 get----()                        Color.black
 Example, getText() will send     editable true
 the caller whatever phrase is    visible true
 currently in the JTextField
                                  public String public
 String temp =                    getText()     JTextField()
 radiusTF.getText();              public void
                                  setText(String
                                  txt)

                                                               10
Constructors are               radiusTF
                               text “Radius will go here”

cool                           bounds [120, 80, 50, 20]

 Constructors are special      background color
                               Color.grey
 methods                       foreground color
 They give an object its       Color.black

 initial state by setting up   editable true
 all of its variables          visible true

 Constructors ALWAYS           public String public
 have the same name as         getText()     JTextField()

 the class!!!                  public void
                               setText(String
                               txt)

                                                            11
What happens when
multiple objects are
created?
radiusTF                          circumferenceTF                   areaTF
text “1”                          text “6.28”                       text “3.14”
bounds [120, 80, 50, 20]          bounds [120, 280, 50, 20]         bounds [120, 480, 50, 20]
background color Color.grey       background color Color.grey       background color Color.grey
foreground color Color.black      foreground color Color.black      foreground color Color.black
editable true                     editable false                    editable false
visible true                      visible true                      visible true

public String getText()           public String getText()           public String getText()

public void setText(String txt)   public void setText(String txt)   public void setText(String txt)
public JTextField()               public JTextField()               public JTextField()




                                                                                                      12
Cool, what can objects do?
 As we mentioned before, a
 blueprint lays out how a house
 is put together (constructors),
 what things will be in the house
 (instance variables) and what
 kinds of things a house can do
 (methods)
 Let’s look at another class to
 see how these three parts of a
 blueprint work

                                    13
//From page 162 of your text               //Other methods
public class Student {
                                            public Student(String nm)
  //instance variables
  public String name;                           {
  private int test1;                                name = nm;
  private int test2;
  private int test3;                                test1 = 0;
                                                    test2 = 0;
 //Constructor method
                                                    test3 = 0;
 //Initialize a student's name to the
     empty string and his test scores to        }//set a students name
     zero
 public Student() {                         public void setName(String nm) {
   name = "";                                   name = nm;
   test1 = 0;
                                            }
   test2 = 0;
   test3 = 0;                              /Get a student's name
 }
                                            public String getName() {
                                                return name;
                                            }
                                                                               14
public void setScore(int i, int score) //Compute and return a student's average
                                         //

   {                                    public int getAverage()
                                        {
   if (i == 1) test1 = score;             int average; //will hold the average of the 3
   else if (i == 2) test2 = score;    quizzes
                                          average =
    else test3 = score;               (int)Math.round((test1+test2+test3)/3.0);
  }                                       return average;
//Get the score on the indicated test  }
 public int getScore(int i) {            Compute and return a student's highest score
   if (i == 1)
                                              public int getHighScore() {
     return test1;
   else if (i == 2)                               int highScore;
     return test2;                                highScore = test1;
   else                                           if (test2 > highScore) highScore = test2;
     return test3;
                                                  if (test3 > highScore) highScore = test3;
 }
                                                  return highScore;
                                              }
                                         }                                                    15
//Return a string representation of a
    student's name, test scores and
    average
public String toString(){
     String str;
     str = "Name:     " + name + "\n" +
       "Test 1:     " + test1 + "\n" +
       "Test 2:     " + test2 + "\n" +
       "Test 3:     " + test3 + "\n" +
       "Average: " + getAverage();
     return str;
 }
} //end of class



                                          16
How is the student class used?
 An application can create instances of a class by
 declaring a variable of that class and by using
 the new command
 For example, to create 4 student objects
 Student s1 = new Student();
 Student s2 = new Student();
 Student s3 = new Student();
 Student s4 = new Student();
                                                 17
First, the house construction
 Whenever an object is instanciated, a
 constructor is activated
 The zero arg constructor simply sets the name to
 be blank and the test scores to 0
 A constructor is called for each object
 The constructor is called 4 times
 In the StudentFrame, the one arg constructor is
 used to pass the Student Name in upon creation

                                               18
What happens when we instantiate 4
students?
 s1               s2               s3               s4
name “”          name “”          name “”          name “”

 test1 0          test1 0          test1 0          test1 0

 test2 0          test2 0          test2 0          test2 0

 test3 0          test3 0          test3 0          test3 0

getScore()       getScore()       getScore()       getScore()
setScore()       setScore()       setScore()       setScore()
setName()        setName()        setName()        setName()
getName()        getName()        getName()        getName()
getAverage()     getAverage()     getAverage()     getAverage()
getHighScore()   getHighScore()   getHighScore()   getHighScore()
toString()       toString()       toString()       toString()
                                                                    19
What kinds of things appear in class
files?
1. Import statements
  a. Used to identify where classes are coming from
2. Extends relationships
  a. Inheritance relationship
  b. Builds upon the class it extends
  c. All methods and variables from ancestors are
     inherited


                                                      20
What kinds of things appear in class
files?
3. Implements relationships (one or more)
  a. Means that certain methods MUST be present
  b. These methods have specific names and
     parameters
  c. You can leave them blank at first but must at least
     “stub code” them
4. Global variable declarations
  a. Global variables exist for the life of the object

                                                         21
What kinds of things appear in class
files?
5. Methods
  a. Methods are sub commands that exist in the class
  b. They must be called or invoked in order to begin
     executing
  c. Public methods are callable by client programs
  d. Private methods can only be called by this class
  e. When a method is called, any parameters that are
     in the () must be supplied

                                                    22
What kinds of things appear in class
files?
6. Constructors
  a. Constructors are special methods that get called
     whenever a new command is issued for this class
  b. Constructors MUST have the same name as the
     class
  c. A class may have multiple constructors with
     different parameters
  d. The job of the constructor is to give the object an
     initial state

                                                           23
What kinds of things appear in class
files?
7.    Constants
     a.   Constants are cool, they allow a programmer to define a
          value that won’t change throughout the program
     b.   PI, speed of light in a vaccum, number of onces in a liter
     c.   final double OZSPERLITER = 33.8140227;
     d.   Capital letters are used by tradition by C and C++
          programmers for constants, please stick with this tradition




                                                                        24
What kinds of things appear in class
files?
8. Program Comments
  a. //Used to document the program for other
     programmers
  b. /* This method works for multiline comments*/
9. Inner classes and other classes
  a. Used when they are only needed by this particular
     class


                                                     25
Summary
 Classes are used to provide reusable software
 blueprints to other programmers
 This concept is incredibly powerful
 Upon creating instances of these classes, we
 gain access to all of the data and logic that these
 blueprints contain



                                                  26

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:10
posted:9/25/2012
language:English
pages:26