Object Oriented Programming mr Hanley 1 Programs built
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 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? JText. Field is a class, radius. TF is an object Inside of a frame, radius. TF is instanciated as a JText. Field with the following command; 6
radius. TF An example Objectbounds [120, 80, 50, 20] text “” So, what happens when we create an object? Our computer allocates memory for the object background color Color. grey foreground color Color. black editable true visible true public String public get. Text() JText. Field() public void set. Text(String txt) 7
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 radius. TF text “” bounds [120, 80, 50, 20] background color Color. grey foreground color Color. black editable true visible true public String public get. Text() JText. Field() public void set. Text(String txt) 8
Objects communicate via messages To control objects, we send them messages To set the text for a JText. Field, we send the message set. Text with a String inside the () radius. TF. set. Text(“R adius will go here!”); radius. TF text “Radius will go here” bounds [120, 80, 50, 20] background color Color. grey foreground color Color. black editable true visible true public String public get. Text() JText. Field() public void set. Text(String txt) 9
You can also examine the state An object can report its state via a message typically name get----() Example, get. Text() will send the caller whatever phrase is currently in the JText. Field String temp = radius. TF. get. Text(); radius. TF text “Radius will go here” bounds [120, 80, 50, 20] background color Color. grey foreground color Color. black editable true visible true public String public get. Text() JText. Field() public void set. Text(String txt) 10
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!!! radius. TF text “Radius will go here” bounds [120, 80, 50, 20] background color Color. grey foreground color Color. black editable true visible true public String public get. Text() JText. Field() public void set. Text(String txt) 11
when multiple objects are created? radius. TF circumference. TF area. TF 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 foreground color Color. black editable true editable false visible true public String get. Text() public void set. Text(String txt) public JText. Field() 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 public class Student { //instance variables public String name; private int test 1; private int test 2; private int test 3; //Constructor method //Initialize a student's name to the empty string and his test scores to zero public Student() { name = ""; test 1 = 0; test 2 = 0; test 3 = 0; } //Other methods public Student(String nm) { name = nm; test 1 = 0; test 2 = 0; test 3 = 0; }//set a students name public void set. Name(String nm) { name = nm; } /Get a student's name public String get. Name() { return name; } 14
public void set. Score(int i, int score) { if (i == 1) test 1 = score; else if (i == 2) test 2 = score; else test 3 = score; } //Get the score on the indicated test public int get. Score(int i) { if (i == 1) return test 1; else if (i == 2) return test 2; else return test 3; } //Compute and return a student's average public int get. Average() { int average; //will hold the average of the 3 quizzes average = (int)Math. round((test 1+test 2+test 3)/3. 0); return average; } // l l. Compute and return a student's highest score public int get. High. Score() { int high. Score; high. Score = test 1; if (test 2 > high. Score) high. Score = test 2; if (test 3 > high. Score) high. Score = test 3; return high. Score; 15
//Return a string representation of a student's name, test scores and average public String to. String(){ String str; str = "Name: "n" + name + "Test 1: "n" + test 1 + "Test 2: "n" + test 2 + "Test 3: "n" + test 3 + "Average: " + get. Average(); return str; 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 s 1 = new Student(); Student s 2 = new Student(); Student s 3 = new Student(); Student s 4 = 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 Student. Frame, the one arg constructor is used to pass the Student Name in upon creation 18
What happens when we instantiate 4 students? s 1 s 2 s 3 s 4 name “” test 1 0 test 2 0 test 3 0 get. Score() set. Name() get. Average() get. High. Score() to. String() 19
What kinds of things appear in class files? 1. Import statements a. 2. Used to identify where classes are coming from Extends relationships a. b. c. Inheritance relationship Builds upon the class it extends All methods and variables from ancestors are inherited 20
What kinds of things appear in class files? 3. Implements relationships (one or more) a. b. c. 4. 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 a. Global variables exist for the life of the 21
What kinds of things appear in class files? 5. Methods a. b. c. d. e. 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 22
What kinds of things appear in class files? 6. Constructors a. b. c. d. 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 23
What kinds of things appear in class files? 7. Constants a. b. c. d. 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 24
What kinds of things appear in class files? 8. Program Comments a. b. 9. //Used to document the program for other programmers /* This method works for multiline comments*/ 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
- Slides: 26