AP Computer Science Chapter 2 Notes
AP Computer Science Chapter 2 Notes
An Introduction to Objects
and Classes
Classes
Class: Set of objects with the
same behavior, describes
characteristics and behavior of
an object.
Each class can have instances of
multiple objects
Written in a file all its own
Objects
Object: real world entity that you
can manipulate in your
programs (by invoking methods)
Each object belongs to a class
Object vs Class
Object
is an instance of a class
Cookie
Class
is a blueprint for an
object
Cookie
Cutter
Create an instance of an
object
Instantiation
of an object creates an
instance of an object and the
parameters passed into the
constructor will initialize the private
instance fields.keyword new
Car
An Object
An
An
Class
blueprint for
objects of a
particular type
Defines the
structure
(number, types)
of the attributes
Defines available
behaviors of its
objects
Object
Attributes
Behaviors
Class: Car
Attributes:
String model
Color color
int numPassengers
double amountOfGas
car
Object: my
Attributes:
model = "Mustang"
color = Color.YELLOW
numPassengers = 0
amountOfGas = 16.5
Behaviors:
Behaviors:
Add/remove a passenger
Get the tank filled
Report when out of gas
Class
Written by a
programmer
Example: class Robot which
keeps track of its initial
location, current location
and direction facing with
behaviors such as turn and
step forward.
vs.
Object
OOP
Object-Oriented Programming
An
An
An
OOP Benefits
Facilitates
team development
Easier
Easier
Band
Foot
Dance selection
pulldown list
Dancer
Go / Stop button
Control panel
Dance floor
Positioning
button
Waltz, etc.
Dance step
Dance group
name of a class
(and its source file)
always starts with
a capital letter.
Fields/Instance
Variables /private data
members/Attributes
Methods/Behaviors
Constuctors
SomeClass.java
import ...
import statements
Constructors
Methods
private fields
____________
public method
___________
____________
____________
public method
___________
Part 1 - Fields
Constitute
private memory of an
object
Each field has a data type (int, double,
String, boolean, char, or another type
of object)
Each field has a name given by the
programmer
Fields
Should
Can
From
class ClassName
{
...
accessSpecifier fieldType fieldName;
...
Fields (contd)
private datatype name;
Usually
private
An objects STATE
Each
An
What
Part 2 - Constructors
A
constructor is special
procedure for constructing an
object.
PURPOSE:
Initialize the
objects fields
Constructors
Always
Always
public
class
No
return type
May
Constructors
public class Car()
{
public Car(int m)
{
miles = m;
}
. . .
}
More on constructors
Not
a method
overloading
Overloaded Constructors
These are all different
public Car ( )
no parameters called
the
default constructor initializes
all private data to 0 or null.
public Car (int miles)
public Car (String name, int miles)
public Car (int miles, String name)
PUBLIC INTERFACE OF A
CLASS
The
constructors
and methods of a
class which can be
seen by all.
private method
public method
3. Methods
Example:
Method Definition
Written in the class
access specifier (We will always use public)
return type (int,double,char,String,boolean,void,
or another objects type MUCH LATER)
method name (meaningful and lowercase)
list of parameters (need types and names
separated by commas)
method body in { }
Sample method
public double addIt (int number)
{
total = total + number;
return total;
}
Name each colored part
We can also have overloaded methods
public double addIt(int x, int y)
Methods (contd)
A
2 Types of Methods
Accessor
Modifier/Mutator
will change
what is in a private field and
typically RETURN void.
METHOD PARAMETERS
These
METHOD IMPLEMENTATION
A
CALL to a method
For instance System.out.printl();
is a call to the method println in the
system class.
Use a dot
For now we will call a method from our main
file.
Objects
Contain
1.
properties/variables/fields
2. methods/behaviors
3. constructors
Object Construction
In main for now
To create an new object:
new className(parameters without types)
Object Construction
Examples
A Rectangle Object
an OBJECT DIAGRAM
Object Reference
We
Object Variables/Reference
Declare and optionally initialize:
Rectangle crispyCrunchy;
This only declares an object
reference, it does NOT create the
object itself until you use = new
Right now it refers to no object
BETTER KNOWN AS NULL
Example:
r is a reference to a Rectangle
as in previous slide.
r.GetArea( );
gotcha on AP multiple
choice questions (and on my tests)
Rectangle
r = new
Rectangle(5,10,20,30);
Rectangle cerealBox = r;
See
to
to
Testing a Class/Driver
class
Test class: a class with a main method that
contains statements to test another class.
VARIABLES
Lots
in a block of code
enclosed within a set of curly
braces.
Could be in a method
Could be in the main class file
They exist only within that block
of code die upon closing brace
General Definition of a
Variable
An
Holds
Every
General Definition of a
Variable
Instance
Local
Declaration vs Definition
age
int age ;
age
22
TypeName variableName;
Example:
Rectangle cerealBox;
String name ="Dave";
int x = 10;
Purpose:
To define a new variable of a particular type
Variable Catagories
class first letter capitalized
constant set only once never changed all capitalized
private instance field mySomething or specific name
variable noun
temporary variable - letter
accessor method verb getter
modifier method verb setter
method verb
Reserved Words
In
Reserved
Reserved
words include:
There
Programmer-Defined Names
In
The
Note
Names (contd)
Programmers
Style:
Style:
Names (contd)
Method
PI, CUBESIZE
It
Package
Package:
To
Example:
import java.awt.Rectangle;
We
ENCAPSULATION
Keeping
Encapsulation and
Information Hiding
A
ABSTRACTION
Strip
away to find
the essential
features of an
object.
Javadoc
Automatically
3 reasons to comment
First
Comments
Comments
Comments
// Convert to kilograms
}
balance is the balance of the object to the left of the
dot:
momsSavings.withdraw(500)
means
double newBalance = momsSavings.balance amount;
momsSavings.balance = newBalance;