1 / 26

Object Oriented Programming

Object Oriented Programming. mr. Hanley. Programs built from smaller components. Today’s software programs are generally built from pieces of code known as classes

Télécharger la présentation

Object Oriented Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Object Oriented Programming mr. Hanley

  2. 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)

  3. 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.

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

  5. 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?

  6. 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();

  7. An example Object radiusTF text “” bounds [120, 80, 50, 20] background color Color.grey • So, what happens when we create an object? • Our computer allocates memory for the object foreground color Color.black editable true visible true public String getText() public JTextField() public void setText(String txt)

  8. Objects have their own data radiusTF text “” bounds [120, 80, 50, 20] background color Color.grey • Each object has its own instance variables or state • This object, for example has a background color of grey and is editable foreground color Color.black editable true visible true public String getText() public JTextField() public void setText(String txt)

  9. Objects communicate via messages radiusTF text “Radius will go here” bounds [120, 80, 50, 20] background color Color.grey • To control objects, we send them messages • To set the text for a JTextField, we send the message setText with a String inside the () • radiusTF.setText(“Radius will go here!”); foreground color Color.black editable true visible true public String getText() public JTextField() public void setText(String txt)

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

  11. Constructors are cool radiusTF text “Radius will go here” bounds [120, 80, 50, 20] background color Color.grey • Constructors are special methods • They give an object its initial state by setting up all of its variables • Constructors ALWAYS have the same name as the class!!! foreground color Color.black editable true visible true public String getText() public JTextField() public void setText(String txt)

  12. radiusTF areaTF circumferenceTF text “1” text “3.14” text “6.28” bounds [120, 280, 50, 20] bounds [120, 80, 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() What happens when multiple objects are created?

  13. 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

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

  15. ////Compute and return a student's average public int getAverage() { int average; //will hold the average of the 3 quizzes average = (int)Math.round((test1+test2+test3)/3.0); return average; } • Compute and return a student's highest score public int getHighScore() { int highScore; highScore = test1; if (test2 > highScore) highScore = test2; if (test3 > highScore) highScore = test3; return highScore; } } public void setScore(int i, int score) { if (i == 1) test1 = score; else if (i == 2) test2 = score; else test3 = score; } //Get the score on the indicated test public int getScore(int i) { if (i == 1) return test1; else if (i == 2) return test2; else return test3; }

  16. //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

  17. 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();

  18. 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

  19. s1 s2 s4 s3 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() setScore() setName() getName() getAverage() getHighScore() toString() getScore() setScore() setName() getName() getAverage() getHighScore() toString() getScore() setScore() setName() getName() getAverage() getHighScore() toString() getScore() setScore() setName() getName() getAverage() getHighScore() toString() What happens when we instantiate 4 students?

  20. What kinds of things appear in class files? • Import statements • Used to identify where classes are coming from • Extends relationships • Inheritance relationship • Builds upon the class it extends • All methods and variables from ancestors are inherited

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

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

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

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

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

  26. 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

More Related