object oriented programming (oop): just an extension to...
TRANSCRIPT
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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