Download presentation
Presentation is loading. Please wait.
Published byAlison Hart Modified over 9 years ago
1
p Chapter 2 introduces Object Oriented Programming. p OOP is a relatively new approach to programming which supports the creation of new data types and operations to manipulate those types. p This presentation introduces OOP. Object Oriented Programming Data Structures and Other Objects Using Java
2
Abstract Data Type (ADT) p A new type p Contains some form of data representation p Provides operations for manipulating the data
3
Object-Oriented Programming p A methodology for how programs are written p Extension of ADT concept to include inheritance capability p An abstraction of the real world real world animal mammal canine dog Lakota
4
Key Principles of ADTs and OOP p Encapsulation p A way of organizing all the components of an object into one entity p E.g. class libraries p Information Hiding p A way of separating the descriptive specification for an object from its implementation p E.g. primitive data types such as double
5
Object p Defines a new type of data p Includes data members (usually hidden) called instance variables p Includes member methods that define the permissible operations on the data members p Includes constructors – special methods that provide initializations for instance variables
6
Example - Throttle - 6 - 5 - 4 - 3 - 2 - 1 - Off Throttle Top 6 Position 4 getFlowisOnshift shutOff Hidden
7
Example - Throttle - 6 - 5 - 4 - 3 - 2 - 1 - Off Throttle Top 6 Position 4 shift shutOff isOngetFlow
8
Java Class p In Java (and some other programming languages), objects are built using the mechanism called a class p A class encapsulates all of the components of the object, including instance variables, constructor(s), and method definitions
9
Defining a New Class in Java public class public class { <methods>}
10
Throttle Class public class Throttle { private int top; private int position; <methods>} Name begins with capital letter Instance variables
11
Constructor Methods p Responsible for initializing instance variables p Can have more than one, as long as they have different signatures p Can have no constructor – default constructor p A method with no return type
12
Throttle Constructor public Throttle(int size) Parameters: size, the number of on positions Precondition Size > 0 Postcondition Throttle initialized with specified # of on positions; initially off Throws: IllegalArgumentException
13
public Throttle(int size) { if (size <= 0) throw new IllegalArgumentException (“Size <= 0: “ + size); top = size; position = 0; }
14
Throttle Constructor public Throttle( ) Parameters: none Precondition: none Postcondition Throttle initialized with 5 on positions; initially off Throws: IllegalArgumentException
15
public Throttle( ) { top = 5; position = 0; }
16
Methods p Accessor – gives information about an object without altering it p Modifier – changes the “status” of an object (generally by changing instance variables)
17
Throttle Accessor getFlow( ) public double getFlow( ) Get the current flow of this Throttle Returns: The current flow rate
18
public double getFlow( ) { return (double) position / (double) top; }
19
Reasons for Accessor Methods p Programmer using Throttle doesn’t need to worry how its implemented p Could later change Throttle implementation without affecting existing user programs p Method can be thoroughly tested p Information hiding keeps programmers from using instance variables in unintended ways (e.g. setting position to negative)
20
Throttle Accessor isOn( ) public boolean isOn( ) Check whether Throttle is on Returns If Throttle flow is above zero, returns true; otherwise returns false.
21
public boolean isOn( ) { return (position > 0); } Question: why doesn’t the programmer just check the value of position directly?
22
Throttle Modifier shutOff( ) public void shutOff( ) Turn off this Throttle Precondition: none Postcondition: the Throttle’s flow is shut off
23
public void shutOff( ) { position = 0; }
24
Throttle Modifier shift( ) public void shift( int amount) Move Throttle position up or down Parameters: amount – the amount to move the position up or down Postconditions: Throttle’s position has been moved by amount. If result is more than top position, then position set to top. If result is less than zero position, then position set to zero.
25
public void shift(int amount) { if (amount > top - position) position = top; else if (position + amount < 0) position = 0; else position += amount; } Why isn’t this: (position + amount > top)?
26
Methods Activating (Calling) Methods public boolean isOn() { return (getFlow() > 0); } Could just reference position instance variable
27
Creating and Using Objects Prior examples: int[] values = new int[20]; Scanner input = new Scanner(System.in);
28
Creating and Using Objects Throttle control = new Throttle(100); Declares new variable of type Throttle } } Creates new Throttle object Initializes Throttle size to 100
29
Creating and Using Objects Throttle control = new Throttle(); Initializes Throttle size to default value
30
Creating and Using Objects Throttle control = new Throttle();. control.shift(3); Activates shift( ) method for object control Object name Period operator Method name Method parameters
31
final int SIZE = 8; final int SPOT = 3; Throttle small = new Throttle(SIZE); small.shift(SPOT); System.out.print(“Small throttle position = “); System.out.println(SPOT + “ out of “ + SIZE + “.”); System.out.println(“The flow is now: “ + small.getFlow());
32
Throttle tiny = new Throttle(4); Throttle huge = new Throttle(10000);tinyhuge top position 4 0 top position 10000 0 ThrottleThrottle Each object has its own copy of the instance variables
33
Null References Throttle control;. control = new Throttle(100); } What is the state of control during this time?
34
Null References Throttle control = null;. control = new Throttle(100);
35
Null References Throttle control;. control = new Throttle(100);. control = null; // no longer needed
36
Assignments with Reference Variables Throttle t1; Throttle t2; t1 = new Throttle(100); t1.shift(25); t2 = t1; top position 100 25 Throttle t1t2 ?? t2.shift(-5); 20
37
Assignments with Reference Variables Throttle t1; Throttle t2; t1 = new Throttle(100); t1.shift(25); t2 = new Throttle(100); t2.shift(25); t1t2 top position 100 25 top position 100 25 ThrottleThrottle
38
(t1 == t2) ? Throttle t1; Throttle t2; t1 = new Throttle(100); t1.shift(25); t2 = t1; top position 100 25 Throttle t1t2 TRUE
39
(t1 == t2) ? Throttle t1; Throttle t2; t1 = new Throttle(100); t1.shift(25); t2 = new Throttle(100); t2.shift(25); t1t2 top position 100 25 top position 100 25 ThrottleThrottle FALSE
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.