Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 Object Oriented Programming mr. Hanley. 2 Programs built from smaller components Today’s software programs are generally built from pieces of code known."— Presentation transcript:

1 1 Object Oriented Programming mr. Hanley

2 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 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 4 What classes written by other people have we used so far? Scanner System Random Math String

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

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

9 9 Objects communicate via messages 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!”); radiusTF text “Radius will go here” bounds [120, 80, 50, 20] background color Color.grey foreground color Color.black public String getText() editable true visible true public void setText(String txt) public JTextField()

10 10 You can also examine the state 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(); radiusTF text “Radius will go here” bounds [120, 80, 50, 20] background color Color.grey foreground color Color.black public String getText() editable true visible true public void setText(String txt) public JTextField()

11 11 Constructors are cool 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!!! radiusTF text “Radius will go here” bounds [120, 80, 50, 20] background color Color.grey foreground color Color.black public String getText() editable true visible true public void setText(String txt) public JTextField()

12 12 What happens when multiple objects are created? radiusTF text “1” bounds [120, 80, 50, 20] background color Color.grey foreground color Color.black public String getText() editable true visible true public void setText(String txt) public JTextField() circumferenceTF text “6.28” bounds [120, 280, 50, 20] background color Color.grey foreground color Color.black public String getText() editable false visible true public void setText(String txt) public JTextField() areaTF text “3.14” bounds [120, 480, 50, 20] background color Color.grey foreground color Color.black public String getText() editable false visible true public void setText(String txt) public JTextField()

13 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 14 //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; } //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; }

15 15 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; } // //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; }

16 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 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 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 19 What happens when we instantiate 4 students? s1 name “” test1 0 test2 0 test3 0 getScore() setScore() setName() getName() getAverage() getHighScore() toString() s2 name “” test1 0 test2 0 test3 0 getScore() setScore() setName() getName() getAverage() getHighScore() toString() s3 name “” test1 0 test2 0 test3 0 getScore() setScore() setName() getName() getAverage() getHighScore() toString() s4 name “” test1 0 test2 0 test3 0 getScore() setScore() setName() getName() getAverage() getHighScore() toString()

20 20 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

21 21 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

22 22 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

23 23 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

24 24 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

25 25 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

26 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


Download ppt "1 Object Oriented Programming mr. Hanley. 2 Programs built from smaller components Today’s software programs are generally built from pieces of code known."

Similar presentations


Ads by Google