object oriented programming (oop): just an extension to...

95
Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP Object oriented programming (OOP): Just an extension to modularity Prof. Dionne Aleman MIE250: Fundamentals of object-oriented programming University of Toronto MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 1 / 44

Upload: others

Post on 30-Jun-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

Object oriented programming (OOP):Just an extension to modularity

Prof. Dionne Aleman

MIE250: Fundamentals of object-oriented programmingUniversity of Toronto

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 1 / 44

Page 2: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

Why objects?1

I Reduce complexity

I Facilitate understanding and modification

I Help testing and debugging

I Promote reusability

1Sound familiar?MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 2 / 44

Page 3: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

What is an object?

I Objects sound mysterious and complex, but are in fact very simple.

I Objects are essentially glorified variables.

I They are just wrappers around other bits of information (variablescalled member fields).

I They can also have internal functions, called methods, to interactwith member fields and do whatever we want.

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 3 / 44

Page 4: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

Can you think of any examples we have seen so far?

I String objectFields Probably an array of chars, maybe an int for length

Methods .length(), .contains(), .equals(String s2), etc.

I BufferedReader objectFields No idea

Methods .readLine(), probably others that we haven’t learned

I Math objectFields E, PI, maybe more

Methods .sqrt(double x), .sin(double x), .Random(), etc.

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 4 / 44

Page 5: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

Can you think of any examples we have seen so far?

I String objectFields Probably an array of chars, maybe an int for length

Methods .length(), .contains(), .equals(String s2), etc.

I BufferedReader objectFields No idea

Methods .readLine(), probably others that we haven’t learned

I Math objectFields E, PI, maybe more

Methods .sqrt(double x), .sin(double x), .Random(), etc.

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 4 / 44

Page 6: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

Can you think of any examples we have seen so far?

I String objectFields Probably an array of chars, maybe an int for length

Methods .length(), .contains(), .equals(String s2), etc.

I BufferedReader objectFields No idea

Methods .readLine(), probably others that we haven’t learned

I Math objectFields E, PI, maybe more

Methods .sqrt(double x), .sin(double x), .Random(), etc.

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 4 / 44

Page 7: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

Can you think of any examples we have seen so far?

I String objectFields Probably an array of chars, maybe an int for length

Methods .length(), .contains(), .equals(String s2), etc.

I BufferedReader objectFields No idea

Methods .readLine(), probably others that we haven’t learned

I Math objectFields E, PI, maybe more

Methods .sqrt(double x), .sin(double x), .Random(), etc.

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 4 / 44

Page 8: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

What’s the common theme?

I We don’t really know what is happening inside those objects.

I Do we care?I Nope.

I Is it easier for us that way?I You betcha.

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 5 / 44

Page 9: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

What’s the common theme?

I We don’t really know what is happening inside those objects.

I Do we care?I Nope.

I Is it easier for us that way?I You betcha.

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 5 / 44

Page 10: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

What’s the common theme?

I We don’t really know what is happening inside those objects.

I Do we care?

I Nope.

I Is it easier for us that way?I You betcha.

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 5 / 44

Page 11: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

What’s the common theme?

I We don’t really know what is happening inside those objects.

I Do we care?I Nope.

I Is it easier for us that way?I You betcha.

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 5 / 44

Page 12: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

What’s the common theme?

I We don’t really know what is happening inside those objects.

I Do we care?I Nope.

I Is it easier for us that way?

I You betcha.

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 5 / 44

Page 13: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

What’s the common theme?

I We don’t really know what is happening inside those objects.

I Do we care?I Nope.

I Is it easier for us that way?I You betcha.

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 5 / 44

Page 14: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

Anatomy of an object: “Encapsulation”

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 6 / 44

Page 15: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

Java program structureThe .java file

Header files

The class

Global variables (NEW)

Function

Function

One function is the main function

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 7 / 44

Page 16: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

Java class structureThe .java file

Header files

The class

Member variables (fields)

Function

Function

No main function!

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 8 / 44

Page 17: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

“Class” v. “Object”

Class The blueprint from which individual objects are made

Object An instance of a class

For example:1 String s;

Variable s is an instance (object) of type (class) String.

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 9 / 44

Page 18: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

The Distance Calculator programGet a time and speed from the user and print out the distance traveled.

Get speedinput

from user

Get timeinput

from userCalculatedistance

Printdistanceto screen

Quit

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 10 / 44

Page 19: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

The Distance Calculator programGet a time and speed from the user and print out the distance traveled.

Get speedinput

from user

Get timeinput

from userCalculatedistance

Printdistanceto screen

Quit

Get speedinput

from userValid

number?

no

yes Get timeinput

from userValid

number?

no

yes Calculatedistance

Printdistanceto screen

Quit

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 10 / 44

Page 20: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

The Distance Calculator programGet a time and speed from the user and print out the distance traveled.

Get speedinput

from user

Get timeinput

from userCalculatedistance

Printdistanceto screen

Quit

Get speedinput

from userValid

number?

no

yes Get timeinput

from userValid

number?

no

yes Calculatedistance

Printdistanceto screen

Quit

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 10 / 44

Page 21: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

The Distance Calculator programGet a time and speed from the user and print out the distance traveled.

Get speedinput

from user

Get timeinput

from userCalculatedistance

Printdistanceto screen

Quit

Get speedinput

from userValid

number?

no

yes Get timeinput

from userValid

number?

no

yes Calculatedistance

Printdistanceto screen

Quit

Get user input

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 10 / 44

Page 22: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

The Distance Calculator programGet a time and speed from the user and print out the distance traveled.

Get speedinput

from user

Get timeinput

from userCalculatedistance

Printdistanceto screen

Quit

Get speedinput

from userValid

number?

no

yes Get timeinput

from userValid

number?

no

yes Calculatedistance

Printdistanceto screen

Quit

Get user input

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 10 / 44

Page 23: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

The Distance Calculator programGet a time and speed from the user and print out the distance traveled.

Get speedinput

from user

Get timeinput

from userCalculatedistance

Printdistanceto screen

Quit

Get speedinput

from userValid

number?

no

yes Get timeinput

from userValid

number?

no

yes Calculatedistance

Printdistanceto screen

Quit

Get user input

Do thingswith it

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 10 / 44

Page 24: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

Remember this code?

Get userinput

Do thingswith it Quit

1 public class DistanceV5 {2

3 public static void main(String [] args) {4

5 DistanceCalculator dc = new DistanceCalculator ();6 dc.getUserInput ();7 dc.printDistance ();8

9 } // end of main function10

11 } // end of class

src/DistanceCalculatorOOP/DistanceV5.java

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 11 / 44

Page 25: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

Remember this code?

Get userinput

Do thingswith it Quit

1 public class DistanceV5 {2

3 public static void main(String [] args) {4

5 DistanceCalculator dc = new DistanceCalculator ();6 dc.getUserInput ();7 dc.printDistance ();8

9 } // end of main function10

11 } // end of class

src/DistanceCalculatorOOP/DistanceV5.java

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 11 / 44

Page 26: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

Remember this code?

Get userinput

Do thingswith it Quit

1 public class DistanceV5 {2

3 public static void main(String [] args) {4

5 DistanceCalculator dc = new DistanceCalculator ();6 dc.getUserInput ();7 dc.printDistance ();8

9 } // end of main function10

11 } // end of class

src/DistanceCalculatorOOP/DistanceV5.java

Declare DistanceCalculatorobject dc

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 11 / 44

Page 27: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

Remember this code?

Get userinput

Do thingswith it Quit

1 public class DistanceV5 {2

3 public static void main(String [] args) {4

5 DistanceCalculator dc = new DistanceCalculator ();6 dc.getUserInput ();7 dc.printDistance ();8

9 } // end of main function10

11 } // end of class

src/DistanceCalculatorOOP/DistanceV5.java

Use dc toget user input

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 11 / 44

Page 28: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

Remember this code?

Get userinput

Do thingswith it Quit

1 public class DistanceV5 {2

3 public static void main(String [] args) {4

5 DistanceCalculator dc = new DistanceCalculator ();6 dc.getUserInput ();7 dc.printDistance ();8

9 } // end of main function10

11 } // end of class

src/DistanceCalculatorOOP/DistanceV5.java

Use dc todo things with it

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 11 / 44

Page 29: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

See it in action

I Let’s re-build the Distance Calculator game as an OOP.

I We will go step-by-step:1. Start with a simple object.2. Slowly add in useful functionality.

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 12 / 44

Page 30: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

The steps to build an object oriented program

1. How do we want to the non-object part of the program (e.g., themain function) to look?

2. What classes do we need?

3. What data should go in them?

4. What methods to access and manipulate the data do we need?

5. Write the code.

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 13 / 44

Page 31: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

Step 1: We already know what the main function shouldlook like

1 public class DistanceV5 {23 public static void main(String [] args) {45 DistanceCalculator dc = new DistanceCalculator ();6 dc.getUserInput ();7 dc.printDistance ();89 } // end of main function

1011 } // end of class

src/DistanceCalculatorOOP/DistanceV5.java

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 14 / 44

Page 32: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

Step 2: What classes do we need?

I A class to hold all speed and time data and do the calculations.

I Let’s call the class DistanceCalculator.

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 15 / 44

Page 33: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

Step 3: What data should go in it?

I Speed

I Time

I Anything else?

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 16 / 44

Page 34: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

Step 3: What data should go in it?

I Speed

I Time

I Anything else?

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 16 / 44

Page 35: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

Step 3: What data should go in it?

I Speed

I Time

I Anything else?

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 16 / 44

Page 36: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

Step 3: What data should go in it?

I Speed

I Time

I Anything else?

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 16 / 44

Page 37: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

Step 3: The class with data

I Note the file name must be the class name, just like in a regularprogram.

I All the data are basically like global variables to the class.

I What is private?

1 public class DistanceCalculator {23 // member fields (variables): everything we need to know4 private double speed; // speed from user in mph5 private double time; // time from user in min67 }

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 17 / 44

Page 38: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

Step 4: How do we want to access/manipulate the data?

Action Method nameGet user input getUserInput()Print distance printDistance()

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 18 / 44

Page 39: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

this is the best practice

Use this to preface member fields and other member methods inmethods for clarity and specificity:

1 public class DistanceCalculator {23 private double speed; // speed from user in mph4 private double time; // time from user in min56 public void setValues(double speed , double time) {7 this.speed = speed; // No conflict thanks to "this"!8 this.time = time;9 System.out.println("New distance calculation values:");

10 this.printValues ();11 }1213 }

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 19 / 44

Page 40: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

Step 5: Write the code

To do together:1. Write the DistanceCalculator class.2. Write the main function.

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 20 / 44

Page 41: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

One thing missing ...

The code works, but there is one thing missing that is stronglyrecommended for reliable performance of object oriented programs.

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 21 / 44

Page 42: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

ConstructorsA constructor is a method with no return type that is called when theobject is declared. It usually just initializes variable values.

1 public class DistanceCalculator {2 private double speed;3 private double time;45 // a "void" constructor:6 // DistanceCalculator dc = new DistanceCalculator ();7 public DistanceCalculator () {8 this.speed = 0;9 this.time = 0.;

10 }1112 // an alternate constructor:13 // DistanceCalculator dc = new DistanceCalculator (5.2, 20.75);14 public DistanceCalculator(double speed , double time) {15 this.speed = speed;16 this.time = time;17 }18 }

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 22 / 44

Page 43: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

Controlling data

I An important aspect of good OOP is controlling how otherprograms/functions can interact with our object’s data.

I We don’t want another program(mer) messing up the raw datainside a class we’ve created.

I If someone needs access to an object’s data, we should anticipatethat need and write a function that:1. Makes that access easy.2. Prevents someone else screwing up the data in our object.

I If data has to be accessed, it will be done on our terms.

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 23 / 44

Page 44: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

Access modifiers

Access modifiers control the access we have to an object’s data.

Modifier Within class Outside class, insame package

Outside class,outside package

public X X X

protected X X

private X

Unless you have a good reason to do otherwise, member variables shouldbe private.

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 24 / 44

Page 45: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

Putting it together ...

I Where have we seen public before?

I What does that mean?

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 25 / 44

Page 46: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

Putting it together ...

I Where have we seen public before?

I What does that mean?

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 25 / 44

Page 47: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

public static void main

I We understand public now, but what is static?

I static means that particular member function (or variable) is thesame across all objects of that class.

I In other words, we do not need to declare an object of that class inorder to use a static function or variable

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 26 / 44

Page 48: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

public static void main

I We understand public now, but what is static?

I static means that particular member function (or variable) is thesame across all objects of that class.

I In other words, we do not need to declare an object of that class inorder to use a static function or variable

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 26 / 44

Page 49: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

static example

Say we want to use DistanceCalculator like this:

1 double dist = DistanceCalculator.calcDist(speed , time);

Do we need to declare an actual DistanceCalculator object in order torun that line of code?

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 27 / 44

Page 50: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

static example

Say we want to use DistanceCalculator like this:

1 double dist = DistanceCalculator.calcDist(speed , time);

Do we need to declare an actual DistanceCalculator object in order torun that line of code?

No:There’s no object in that statement

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 27 / 44

Page 51: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

When to use static

So, we use static when an actual object of a class isn’t needed:

1 public class DistanceCalculator {2 // all the other class members and methods3 ...45 // a static method6 public static double calcDist(int speed , int time) {7 return speed * time;8 }9 }

Also works for members, not just methods

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 28 / 44

Page 52: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

public or not? static or not?

public static

Math.sqrt()

Math.PI

str.equals()

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 29 / 44

Page 53: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

public or not? static or not?

public static

Math.sqrt() X

Math.PI

str.equals()

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 29 / 44

Page 54: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

public or not? static or not?

public static

Math.sqrt() X X

Math.PI

str.equals()

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 29 / 44

Page 55: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

public or not? static or not?

public static

Math.sqrt() X X

Math.PI X

str.equals()

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 29 / 44

Page 56: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

public or not? static or not?

public static

Math.sqrt() X X

Math.PI X X

str.equals()

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 29 / 44

Page 57: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

public or not? static or not?

public static

Math.sqrt() X X

Math.PI X X

str.equals() X

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 29 / 44

Page 58: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

public or not? static or not?

public static

Math.sqrt() X X

Math.PI X X

str.equals() X X

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 29 / 44

Page 59: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

The Lucky Sevens program

I Let’s re-build the Lucky Sevens game as an OOP.

I We will go step-by-step:1. Start with a simple object.2. Slowly add in useful functionality.

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 30 / 44

Page 60: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

The steps to build an object oriented program

1. How do we want to the non-object part of the program (e.g., themain function) to look?

2. What classes do we need?

3. What data should go in them?

4. What methods to access and manipulate the data do we need?

5. Write the code.

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 31 / 44

Page 61: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

Step 1: The program should look something like this

1 public class LuckySevens_OOP {23 public static void main(String [] args) {45 int startingCash = getUserStartingCash ();6 LuckySevens game = new LuckySevens(startingCash);78 game.play();9 game.summarize ();

10 game.checkFairness ();1112 } // end main1314 }

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 32 / 44

Page 62: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

Step 2: What classes do we need?

I A class to hold all of the Lucky Sevens game data and run the game.

I Let’s call the class LuckySevens.

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 33 / 44

Page 63: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

Step 3: What data should go in it?

I Starting cash

I Number of rolls until bankruptcy

I Peak cash

I Roll distribution (to check for fairness)

I Amount won on 7

I Amount lost on non-7

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 34 / 44

Page 64: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

Step 3: What data should go in it?

I Starting cash

I Number of rolls until bankruptcy

I Peak cash

I Roll distribution (to check for fairness)

I Amount won on 7

I Amount lost on non-7

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 34 / 44

Page 65: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

Step 3: What data should go in it?

I Starting cash

I Number of rolls until bankruptcy

I Peak cash

I Roll distribution (to check for fairness)

I Amount won on 7

I Amount lost on non-7

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 34 / 44

Page 66: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

Step 3: What data should go in it?

I Starting cash

I Number of rolls until bankruptcy

I Peak cash

I Roll distribution (to check for fairness)

I Amount won on 7

I Amount lost on non-7

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 34 / 44

Page 67: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

Step 3: What data should go in it?

I Starting cash

I Number of rolls until bankruptcy

I Peak cash

I Roll distribution (to check for fairness)

I Amount won on 7

I Amount lost on non-7

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 34 / 44

Page 68: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

Step 3: What data should go in it?

I Starting cash

I Number of rolls until bankruptcy

I Peak cash

I Roll distribution (to check for fairness)

I Amount won on 7

I Amount lost on non-7

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 34 / 44

Page 69: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

Step 3: What data should go in it?

I Starting cash

I Number of rolls until bankruptcy

I Peak cash

I Roll distribution (to check for fairness)

I Amount won on 7

I Amount lost on non-7

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 34 / 44

Page 70: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

Step 3: The class with data

I Note the file name must be the class name, just like in a regularprogram.

I All the data are basically like global variables to the class.

I What is private?

1 public class LuckySevens {23 // member fields (variables): everything we want to know about a game4 private int winValue; // amount won on roll of 75 private int loseValue; // amount lost on non -7 roll6 private int startingCash; // starting cash value7 private int nRolls; // number of rolls until bankruptcy8 private int maxCash; // peak winnings in the game9 private int [] rollFreq = new int [13]; // frequency of roll values

1011 }

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 35 / 44

Page 71: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

Step 5: Write the code

To do together:1. Write the LuckySevens class.2. Write the main function.

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 36 / 44

Page 72: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

Point class example

I Say we want a program that will store many points in a Cartesiancoordinate system

I We will want to do some calculations on these points, e.g.,determine which is closest to the origin, determine which points areon a particular line, etc.

I Each point consists of an (x , y) coordinate

I We could store an array of x -coordinates and an array ofy -coordinates, but that would be cumbersome

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 37 / 44

Page 73: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

Start simple

I We know our object will need two member variables to store thex -coordinate and y -coordinate

I Let’s start with the following capabilities:I Initialize the pointI Modify the coordinatesI Print out the coordinates

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 38 / 44

Page 74: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

What do we want our main program to look like?

1 public class GridSystem {23 public static void main(String [] args) {4 Point p1 = new Point();5 Point p2 = new Point (4,5);67 System.out.print("Point p1: ");8 p1.print();9

10 System.out.print("Point p2: ");11 p2.print();1213 p1 = p2;14 System.out.print("New point p1: ");15 p1.print();16 }1718 }

src/GridSystemOOP/GridSystem.java

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 39 / 44

Page 75: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

Let’s build the Point class

Again, building it TOGETHER.

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 40 / 44

Page 76: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

Good job! Now let’s make it more interesting

Any ideas?

I Translate point a certain amount

I Calculate distance to originI Function to determine which point is closest to the origin

I Calculate distance between pointsI Function to determine which points are closest togetherI Function to determine which points are furthest apart

I Determine if a point is on a lineI Function to determine how many and which points are on a lineI Function to determine how many and which points are not on a line

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 41 / 44

Page 77: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

Good job! Now let’s make it more interesting

Any ideas?

I Translate point a certain amount

I Calculate distance to originI Function to determine which point is closest to the origin

I Calculate distance between pointsI Function to determine which points are closest togetherI Function to determine which points are furthest apart

I Determine if a point is on a lineI Function to determine how many and which points are on a lineI Function to determine how many and which points are not on a line

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 41 / 44

Page 78: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

Good job! Now let’s make it more interesting

Any ideas?

I Translate point a certain amount

I Calculate distance to origin

I Function to determine which point is closest to the origin

I Calculate distance between pointsI Function to determine which points are closest togetherI Function to determine which points are furthest apart

I Determine if a point is on a lineI Function to determine how many and which points are on a lineI Function to determine how many and which points are not on a line

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 41 / 44

Page 79: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

Good job! Now let’s make it more interesting

Any ideas?

I Translate point a certain amount

I Calculate distance to originI Function to determine which point is closest to the origin

I Calculate distance between pointsI Function to determine which points are closest togetherI Function to determine which points are furthest apart

I Determine if a point is on a lineI Function to determine how many and which points are on a lineI Function to determine how many and which points are not on a line

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 41 / 44

Page 80: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

Good job! Now let’s make it more interesting

Any ideas?

I Translate point a certain amount

I Calculate distance to originI Function to determine which point is closest to the origin

I Calculate distance between points

I Function to determine which points are closest togetherI Function to determine which points are furthest apart

I Determine if a point is on a lineI Function to determine how many and which points are on a lineI Function to determine how many and which points are not on a line

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 41 / 44

Page 81: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

Good job! Now let’s make it more interesting

Any ideas?

I Translate point a certain amount

I Calculate distance to originI Function to determine which point is closest to the origin

I Calculate distance between pointsI Function to determine which points are closest together

I Function to determine which points are furthest apart

I Determine if a point is on a lineI Function to determine how many and which points are on a lineI Function to determine how many and which points are not on a line

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 41 / 44

Page 82: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

Good job! Now let’s make it more interesting

Any ideas?

I Translate point a certain amount

I Calculate distance to originI Function to determine which point is closest to the origin

I Calculate distance between pointsI Function to determine which points are closest togetherI Function to determine which points are furthest apart

I Determine if a point is on a lineI Function to determine how many and which points are on a lineI Function to determine how many and which points are not on a line

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 41 / 44

Page 83: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

Good job! Now let’s make it more interesting

Any ideas?

I Translate point a certain amount

I Calculate distance to originI Function to determine which point is closest to the origin

I Calculate distance between pointsI Function to determine which points are closest togetherI Function to determine which points are furthest apart

I Determine if a point is on a line

I Function to determine how many and which points are on a lineI Function to determine how many and which points are not on a line

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 41 / 44

Page 84: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

Good job! Now let’s make it more interesting

Any ideas?

I Translate point a certain amount

I Calculate distance to originI Function to determine which point is closest to the origin

I Calculate distance between pointsI Function to determine which points are closest togetherI Function to determine which points are furthest apart

I Determine if a point is on a lineI Function to determine how many and which points are on a line

I Function to determine how many and which points are not on a line

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 41 / 44

Page 85: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

Good job! Now let’s make it more interesting

Any ideas?

I Translate point a certain amount

I Calculate distance to originI Function to determine which point is closest to the origin

I Calculate distance between pointsI Function to determine which points are closest togetherI Function to determine which points are furthest apart

I Determine if a point is on a lineI Function to determine how many and which points are on a lineI Function to determine how many and which points are not on a line

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 41 / 44

Page 86: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

The new Point class

Build it ...

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 42 / 44

Page 87: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

How about a Line class?

I Member variables:I PointI Slope

I Methods:I Constructor by Point object and slope value, or maybe by two PointsI Find y -interceptI Find rootI Check if a point is on the line

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 43 / 44

Page 88: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

How about a Line class?

I Member variables:

I PointI Slope

I Methods:I Constructor by Point object and slope value, or maybe by two PointsI Find y -interceptI Find rootI Check if a point is on the line

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 43 / 44

Page 89: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

How about a Line class?

I Member variables:I PointI Slope

I Methods:I Constructor by Point object and slope value, or maybe by two PointsI Find y -interceptI Find rootI Check if a point is on the line

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 43 / 44

Page 90: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

How about a Line class?

I Member variables:I PointI Slope

I Methods:

I Constructor by Point object and slope value, or maybe by two PointsI Find y -interceptI Find rootI Check if a point is on the line

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 43 / 44

Page 91: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

How about a Line class?

I Member variables:I PointI Slope

I Methods:I Constructor by Point object and slope value, or maybe by two Points

I Find y -interceptI Find rootI Check if a point is on the line

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 43 / 44

Page 92: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

How about a Line class?

I Member variables:I PointI Slope

I Methods:I Constructor by Point object and slope value, or maybe by two PointsI Find y -intercept

I Find rootI Check if a point is on the line

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 43 / 44

Page 93: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

How about a Line class?

I Member variables:I PointI Slope

I Methods:I Constructor by Point object and slope value, or maybe by two PointsI Find y -interceptI Find root

I Check if a point is on the line

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 43 / 44

Page 94: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

How about a Line class?

I Member variables:I PointI Slope

I Methods:I Constructor by Point object and slope value, or maybe by two PointsI Find y -interceptI Find rootI Check if a point is on the line

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 43 / 44

Page 95: Object oriented programming (OOP): Just an extension to modularitymorlab.mie.utoronto.ca/MIE250/notes/MIE250_04-OOP.pdf · 2015-03-30 · Overview DistanceCalculatorOOP Accessmodifiers

Overview Distance Calculator OOP Access modifiers Lucky Sevens OOP Grid System OOP

Points and Lines

I Let’s build a program that uses both Point and Line objects.

I We may want to revise some of our Point code to use the Lineclass.

MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 44 / 44