1 object oriented programming mr. hanley. 2 programs built from smaller components today’s...

26
1 Object Oriented Programming mr. Hanley

Upload: bonnie-robinson

Post on 17-Dec-2015

220 views

Category:

Documents


0 download

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 classesThese 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?

ScannerSystemRandomMathString

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 behaveNow 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 objectInside of a frame, radiusTF is instanciated as a JTextField with the following command;JTextField radiusTF = new JTextField();

7

An example ObjectSo, what happens when we create an object?Our computer allocates memory for the object

radiusTFtext “”

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

Objects have their own data

Each object has its own instance variables or stateThis object, for example has a background color of grey and is editable

radiusTFtext “”

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

Objects communicate via messages

To control objects, we send them messagesTo set the text for a JTextField, we send the message setText with a String inside the ()radiusTF.setText(“Radius will go here!”);

radiusTFtext “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

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 JTextFieldString temp = radiusTF.getText();

radiusTFtext “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

Constructors are cool

Constructors are special methodsThey give an object its initial state by setting up all of its variablesConstructors ALWAYS have the same name as the class!!!

radiusTFtext “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

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

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

//From page 162 of your textpublic 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

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

//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 commandFor example, to create 4 student objectsStudent s1 = new Student();Student s2 = new Student();Student s3 = new Student();Student s4 = new Student();

18

First, the house constructionWhenever an object is instanciated, a constructor is activatedThe zero arg constructor simply sets the name to be blank and the test scores to 0A constructor is called for each objectThe constructor is called 4 timesIn the StudentFrame, the one arg constructor is used to pass the Student Name in upon creation

19

What happens when we instantiate 4 students?

s1name “”

test1 0

test2 0

test3 0

getScore()setScore()setName()getName()getAverage()getHighScore()toString()

s2name “”

test1 0

test2 0

test3 0

getScore()setScore()setName()getName()getAverage()getHighScore()toString()

s3name “”

test1 0

test2 0

test3 0

getScore()setScore()setName()getName()getAverage()getHighScore()toString()

s4name “”

test1 0

test2 0

test3 0

getScore()setScore()setName()getName()getAverage()getHighScore()toString()

20

What kinds of things appear in class files?1. Import statements

a. Used to identify where classes are coming from

2. Extends relationshipsa. Inheritance relationshipb. Builds upon the class it extendsc. All methods and variables from ancestors are

inherited

21

What kinds of things appear in class files?3. Implements relationships (one or more)

a. Means that certain methods MUST be presentb. These methods have specific names and

parametersc. You can leave them blank at first but must at least

“stub code” them

4. Global variable declarationsa. Global variables exist for the life of the object

22

What kinds of things appear in class files?5. Methods

a. Methods are sub commands that exist in the classb. They must be called or invoked in order to begin

executingc. Public methods are callable by client programsd. Private methods can only be called by this classe. When a method is called, any parameters that are

in the () must be supplied

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

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

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 classesa. Used when they are only needed by this particular

class

26

SummaryClasses are used to provide reusable software blueprints to other programmersThis concept is incredibly powerfulUpon creating instances of these classes, we gain access to all of the data and logic that these blueprints contain