0% found this document useful (0 votes)
42 views23 pages

Chapt 02

The document discusses object-oriented programming concepts in Java including encapsulation, classes, objects, attributes, methods, instantiation, and delegation. Key points are: - Classes represent real-world entities and contain attributes to represent characteristics and methods to represent operations. - Objects are instances of classes that have state defined by their attribute values. - Methods allow accessing and modifying an object's attributes. - The new keyword instantiates objects which initialize their state through constructor methods. - Delegation involves coordinating classes to accomplish tasks without depending on implementation details.

Uploaded by

lekan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views23 pages

Chapt 02

The document discusses object-oriented programming concepts in Java including encapsulation, classes, objects, attributes, methods, instantiation, and delegation. Key points are: - Classes represent real-world entities and contain attributes to represent characteristics and methods to represent operations. - Objects are instances of classes that have state defined by their attribute values. - Methods allow accessing and modifying an object's attributes. - The new keyword instantiates objects which initialize their state through constructor methods. - Delegation involves coordinating classes to accomplish tasks without depending on implementation details.

Uploaded by

lekan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Programming by Delegation

Some examples and/or figures were borrowed (with permission)


from slides prepared by Prof. H. Roumani
Encapsulate real-world entities in a class
Class usually represents a noun (i.e., a thing)
One-word class names begin with a capital letter
E.g., First, Rectangle3, Check01
Multi-word names begin each word with capital
E.g., FirstApp, PrintStream
Instances of a class are called objects

EECS1020 F14 (Steven C.) 2


Characteristics are represented as attributes
Attribute also usually represents a noun
One-word attribute name all in lowercase
E.g., width, height
Multi-word names begin second and subsequent
words with capital
E.g., countPositive, cardNumber
Constant attribute name all in UPPER_CASE with
words separated with an underscore

EECS1020 F14 (Steven C.) 3


Operations are represented as methods
Method usually represents a verb (i.e., an action)
Always followed by parentheses (even if empty)
Additional data (called parameters) included in
parentheses if necessary
One-word method name all in lowercase
E.g., equals(anotherObject), round()
Multi-word names begin second and subsequent
words with capital
E.g., scale(x, y, w, h), getArea()

EECS1020 F14 (Steven C.) 4


Assume r represents a Rectangle3 object
Attributes of type int: width, height
Attribute access syntax
objectIdentifier.attributeName
Examples
int currentWidth = r.width;
int newWidth = 8;
r.width = newWidth ;

EECS1020 F14 (Steven C.) 5


Assume r represents a Rectangle3 object
Method getArea() returns area as int
Method invokation syntax
objectIdentifier.methodName(parameters)
Examples
int area = r.getArea();

EECS1020 F14 (Steven C.) 6


Use the keyword new to instantiate (i.e.,
create) an object
Invoke the classs constructor method to
initialize the objects state
Object declaration and instantiation syntax
ClassName identifier = new ClassName();
Example
Rectangle3 r = new Rectangle3();

EECS1020 F14 (Steven C.) 7



int width = 8;
int height = 5;
Rectangle3 r = new Rectangle3();
r.width = width;
r.height = height;
int rArea = r.getArea();
System.out.println(rArea);

EECS1020 F14 (Steven C.) 8


Uses Procedural Paradigm
Performs computation, not data storage
Represent computations, not objects
E.g., Math class
All methods and attributes are static
Can be called without first declaring an object
E.g., Math.PI, Math.E, Math.round(), Math.log()
Non-utility classes may also have some static
methods and/or attributes

EECS1020 F14 (Steven C.) 9


Can be run from the command-line
Starting point for a Java application
Coordinates use of helper classes
(i.e., components)

EECS1020 F14 (Steven C.) 10


Determine what needs to be done
Which helper class can accomplish each task
Abstract the details of how each is
accomplished
Bread analogy in text (p. 56)
Difficult to grow, harvest, and mill wheat, to bake
into bread
Instead, coordinate with a farmer, miller, and baker

EECS1020 F14 (Steven C.) 11


The client develops the main class
Understands the big picture, the purpose of the
application
Knows what each component does but not how it
does it
The implementer develops a component
Focuses only on the inner details of one component
Client and Implementer share info on a
need-to-know basis

EECS1020 F14 (Steven C.) 12


CLIENT

Interface
Interface

Interface
IMPLEMENTER

Interface

EECS1020 F14 (Steven C.) 13


Hide implementation details from clients
Apply to classes, methods, and/or
attributes
Features with public access appear in the API and
are accessible to clients
Features with private access are not in the API
and are not accessible to clients
Features with protected access are in the API, but
are accessible only to other implementers
Features with no specified access are not in the
API and are available only classes in the same
package (i.e., directory)

EECS1020 F14 (Steven C.) 14


Guarantee between client and implementer
Precondition
What the client must satisfy
Postcondition
What the implementer must deliver
Liability
Pre. is satisfied and post. is satisfied Good
Pre. is satisfied and post. is not satisfied
Implementer at fault
Pre. is not satisfied Client at fault
If no precondition stated, then client need not satisfy
anything

EECS1020 F14 (Steven C.) 15


Methods in the Java specify contracts as
follows:
Precondition is always true unless stated otherwise
Postcondition is specified under Returns and Throws
Example:
double squareRoot(double x)
Returns the square root of the given argument.
Parameters:
x - an argument.
Returns:
the positive square root of x.
Throws:
an exception if x < 0.

EECS1020 F14 (Steven C.) 16


java.awt
Provides support for drawing graphics.
AWT = Abstract Windowing Toolkit

Contains over 3000 java.beans Provide support for Java Beans.

components
java.io Provides support for file and other I/O operations.
java.lang
Provides the fundamental Java classes.

Class details
This package is auto-imported by the compiler.
java.math Provides support for arbitrary-precision arithmetic

contained in TYPE java.net Provides support for network access.

API and Java API java.rmi


Provides support for RMI.
RMI = Remote Method Invocation

Organized into
java.security Provides support for the security framework.
Provides support for databases access over JDBC

packages and
java.sql JDBC = Java Database Connectivity,
SQL = Structured Query Language

subpackages java.text Provides formatting for text, dates, and numbers.

Examples java.util
Miscellaneous utility classes including JCF.
JCF = Java Collection Framework

type.lib.Rectangle3 javax.crypto Provides support for cryptographic operations.

java.util.Scanner javax.servlet
Provides support for servlet and JSP development.
JSP = Java Server Pages

javax.swing
Provides support for GUI development.
GUI = Graphical User Interface

javax.xml
Provides support for XML processing.
EECS1020 F14 (Steven C.)
XML = eXtensible Markup Language 17
Indicate use of Java Standard Library (other than
java.lang.*) or other Java library (e.g., TYPE)
Import one or all classes in a subpackage (using *)
Import statement syntax
import package.subpackage.class; // imports a single
class
import package.subpackage.*; // imports all classes in
subpackage
Example
import java.util.Scanner; // imports only the Scanner
class
import type.lib.*; // imports all classes in the lib
subpackage

EECS1020 F14 (Steven C.) 18


import java.util.Scanner; // place at top of file
Captures user input from the terminal
Parses lines, words, and primitive data types
import java.io.PrintStream; // place at top of file
Outputs text to the terminal
Formats output
Field width
Specify number of decimal places

EECS1020 F14 (Steven C.) 19


Scanner input = new Scanner(System.in);
Tokenizes input (i.e., separates using whitespace)

next() nextInt()
Returns the next word Parses next token as int
nextLine() nextDouble()
Returns the next line Parses next token as double
nextBoolean() nextLong()
nextChar() nextFloat()

EECS1020 F14 (Steven C.) 20


PrintStream output = new
PrintStream(System.out);

print(variable) or print(string literal)


Outputs text to the terminal
println(variable) or println(string literal)
Outputs text to the terminal and appends a newline
character
printf(format string, variable...)
Outputs formatted text to the terminal

EECS1020 F14 (Steven C.) 21


Format string syntax (see p. 108)
%[flags][width][.precision]conversion
flag: , or 0
width: field width (text: left aligned; digits: right
aligned)
precision: number of decimals
conversion: d (integer), f (real), s (text), or n
(newline)
Can also include non-format text
Example
double x = 15.753;
output.printf(Cost: %.2f, x); // outputs Cost: 15.75

EECS1020 F14 (Steven C.) 22


See page 70

Template for all of your 1020 Java programs

Memorize it

EECS1020 F14 (Steven C.) 23

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy