Download presentation
Presentation is loading. Please wait.
Published byBarnard Quinn Modified over 8 years ago
1
Object Oriented Programming and Data Abstraction Rowan University Earl Huff
2
Objects and classes (Chapter 1) Understanding class definitions (Chapter 2) Object Interaction (Chapter 3)
4
Objects Represent ‘things’ from the real world Example: “an old blue car in the parking lot” Classes Represent the kind of all objects Example: “car”
5
Methods Operations objects can invoke. Return value – the result of a method. Some methods do not return anything (void return type) Parameters Additional information required by an object’s method to execute.
6
Specifies the kind of data that can be passed to a parameter Int – Integer (i.e. 4,5,99,etc.) String – “Hello”, “blue” Bool – Boolean, only two values, true and false
7
A single class can create many instances (objects) An object has attributes that defines it (example: attributes for car object could be make, model, color, year, etc.) Values for an object’s attributes are stored in fields The class defines an object’s fields but each object stores its own set of values (known as the state of the object)
9
From an external perspective We see what an object can do (methods) Example: A cash register. Shows the amount owed. Cashier enters the amount given. Determines if change needs to be given out. Shows the amount of change (even if no change is needed) How does the register knows the amount provided? How does it how much change to give back?
10
In order to understand an object’s behavior or how it is implemented, we need to take a look inside. All Java classes possess similar structure on the inside. Let’s take a look!
11
public class Car { Inner part omitted. } public class ClassName { Fields Constructors Methods } The outer wrapper Inner contents of a class
12
Also known as reserved words Words with special meanings in the language All keywords are lowercase (Java is case- sensitive) Examples: public, class, private, int, break, etc.
13
Fields (also called instance variables) store values for an object. Collectively define the state of an object Some change often, some rarely or not at all. public class CashRegister { private int balance; private int given; private int change; } Visibility modifier type variable name private int balance
14
Initializes an object Have the same name as the class Close association with the fields Store initial values into the fields public CashRegister(int owed) { balance = owed; given = 0; change = 0; }
15
Assignment statements used to store values into fields and other variables. Example: variable = expression; balance = owed; One value per variable; previous value is lost. Variable names Lot of freedom over choice of names Choose expressive names to make code easier to understand Avoid single-letter or cryptic names
16
Methods possess consistent structure – header and body Header – provides the method’s signature public int getBalance() The body encloses the method’s statements.
17
Accessor (get) methods Has a return type that is NOT void Returns a value of the type given in the header Contains a return statement to return the value. Mutator (set) methods Used to mutate (change) an object’s state. Have a return type of void Typically contains one or more parameters with the same type as the field. Consist of assignment statements.
18
Visibility modifierreturn type (void for mutators, otherwise identify type) method nameparameter (mutator methods) public void insertBalance(int amount) { balance = balance + amount; return balance; } return statement (accessor methods) assignment statement (mutators)
19
Public void printTransaction() { System.out.println(“##########”); System.out.println(“Balance: $“ + balance); System.out.println(“Amount tendered: $“ + given); System.out.println(“Changed owed: $“ + change); System.out.println(“##########”); }
20
Conditional statements allow us to control the behavior of an object. Provides flexibility to change the outcome of the behavior depending on an observed event. Example: “What happens if amount tendered is less than the balance owed?”
21
if (amount tendered is less than the balance owed) { ask for more money to pay off remaining balance; } else { calculate change owed back; display transaction details; }
22
‘if’ keywordBoolean condition to be testedactions if condition is true if (perform some test) { Statements if test is true; } else { Statements if test is false; } ‘else’ keywordactions if condition is false
23
Fields Store values through the life of an object. Accessible throughout the class. Parameters Received from outside the method. Short lived Help a method complete its task Local variables Defined within a method, method sets their own values Used for temporary calculation and storage. Accessible only from within method.
24
Each block defines a new scope. Class, method and statement Scopes can be nested Fields Scope – the whole class Lifetime – lifetime of the containing object Local variable Scope – the block it is declared in Lifetime – the time of execution of the block in which it is executed
26
Abstraction is the ability to ignore details of parts to focus attention on a higher level of a problem. Modularization is the process of dividing a whole into well-defined parts, which can be built and examined separately, and which interact in well-defined ways.
27
public class Make { private int name; private int origin; Constructor and methods omitted. }
28
public Class Car { private Make make; private String model; private int year; Constructor and methods omitted. }
29
Primitive Types Predefined in the Java language Examples include int, Boolean and double Store directly in variables Object Types Defined by classes. Some classes are defined by the standard Java System (i.e. String) Other classes are those we write ourselves Not stored directly in variables; instead a reference to the object is stored
30
Problem 1 int a; int b; a = 32; b = a; a = a + 1; System.out.println(b); Problem 2 Person a; Person b; a = new Person(“Everett”); b = a; a.changeName(“Delmar”); System.out.println(b.getName());
31
The ‘division’ operator (/), when applied to int operands, returns the result of an integer division (quotient). The ‘modulo’ operator (%) returns the remainder of an integer division. Example: 19 / 5 = 3 19 % 5 = 4
32
&& (and) || (or) ! (not) == (equal to) != (not equal to) > (greater than) >= (greater than or equal to) < (less than) < (less than or equal to)
33
Using Car’s setMake method accord.setMake(new Make(“Honda”,”Japan”)); In class Make public Make(String name, String origin)
34
External Method Call To make an external method call: accord.getYear(); Object.methodName(param list) Internal Method Call No variable name is required this Used as a reference to the invoking object, not for method calls printCarInfo();
35
Null is a special value in Java Object fields are initialized to null by default You can attest for and assign null private String model; if (model != null) { … } model = null;
36
Useful for gaining insights into program behavior Whether or not there is an error Set breakpoints Examine variables Step through code.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.