











Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!












CMSC 131 Fall 2008 Jan Plane (adapted from Bonnie Dorr)
This Set: Methods and Parameter Passing Basics of program design Pseudo-code Objects and classes Heaps Garbage Collection More about Creating Objects and classes in Java Methods Constructors, Accessors, Mutators Equality Printing an object
CMSC 131 Fall 2008 Jan Plane (adapted from Bonnie Dorr)
1
What customers want
What you plan to do
Your program
Did you meet requirements?
Delivery (documentation, etc.)
Bug fixes
New versions
CMSC 131 Fall 2008 Jan Plane (adapted from Bonnie Dorr)
2
In the Real World,
Requirements and Design Rule
CMSC 131 Fall 2008 Jan Plane (adapted from Bonnie Dorr)
3
Program Design
There are many aspects to good design
Architecture
Modeling
Requirements decomposition
Pseudo-code
In this class we will focus on latter
CMSC 131 Fall 2008 Jan Plane (adapted from Bonnie Dorr)
6
Sample Student Object
Name^ Kerry Keenan
ID^444230695
DOB 06-22-
Major^ CMSC
getAge date^ →^ age
getGrades semester^ →^ grades
etc.
etc.
CMSC 131 Fall 2008 Jan Plane (adapted from Bonnie Dorr)
7
Accessing State / Methods
If o is an object v is an instance variable of the object m is a method of the object Then o.v is how to access the data v in o o.m is how to invoke m in o So System is an object, with out an instance variable out is also an object, with println a method System.out.println is how to access this method! Suppose str is a String str is an object! Methods of this object: equals, compareTo, etc. str.equals, str.compareTo, etc. invokes these methods on that object
CMSC 131 Fall 2008 Jan Plane (adapted from Bonnie Dorr)
8
Object-Oriented Programming
Programs are collections of interacting
objects
Writing programs involves identifying what
the objects should be and programming them
Object-oriented languages provide features
to ease object-oriented programming
Defining objects involves indentifying
state
methods
CMSC 131 Fall 2008 Jan Plane (adapted from Bonnie Dorr)
9
Classes
“Blueprints” (“templates”) for objects
Classes include specifications of
Classes can include other information also, as will
be seen later
CMSC 131 Fall 2008 Jan Plane (adapted from Bonnie Dorr)
12
Heap = “Fresh Memory”
While a program is running, some memory is used
to store variables
Rest of memory is called heap and can be used for
other purposes, including storing new objects
CMSC 131 Fall 2008 Jan Plane (adapted from Bonnie Dorr)
13
Main Memory
Stack grows, shrinks
during program execution (why?)
So does “allocated
heap” (part of heap in use)
Unallocated part of
heap is called “free”
CMSC 131 Fall 2008 Jan Plane (adapted from Bonnie Dorr)
14
Object Creation
New space allocated in
heap to store instance variables
Reference (= address)
to this space is returned
sc
CMSC 131 Fall 2008 Jan Plane (adapted from Bonnie Dorr)
15
Strings Are Objects
Where is new in
String name = “Narita”;?
Java provides it!
String is special because it is used so often
Java automatically “fills in” new
You can too:
String name = new String(“Narita”);
CMSC 131 Fall 2008 Jan Plane (adapted from Bonnie Dorr)
18
Heap Issues
“cat” “dog” “cow”
CMSC 131 Fall 2008 Jan Plane (adapted from Bonnie Dorr)
19
Garbage Collection
This “heap management” or “memory
management” issue is central in CS
Java copes by invoking garbage collector to
reclaim unused but still-allocated heap space
Garbage collector reclaims memory in
allocated heap and returns it to free heap
In previous example, “cat” and “dog” would
be reclaimed
CMSC 131 Fall 2008 Jan Plane (adapted from Bonnie Dorr)
20
Example
“abc” “abc”
CMSC 131 Fall 2008 Jan Plane (adapted from Bonnie Dorr)
21
Contrasting Example
String a = new String (“abc”);
String b = a;
if (a == b){
println (“Equal”);
} else {
println (“Not equal”);
}
String a = "abc" String b = new String(a);
“abc”
CMSC 131 Fall 2008 Jan Plane (adapted from Bonnie Dorr)
24
Anatomy of an Instance
Variable Declaration
public int IDNum;
Visibility modifier Normal variable ceclaration
CMSC 131 Fall 2008 Jan Plane (adapted from Bonnie Dorr)
25
… for methods that do not return values
}
Anatomy of a Method
Declaration (1)
Visibility modifier
void keyword
method name parameter list
body
CMSC 131 Fall 2008 Jan Plane (adapted from Bonnie Dorr)
26
Anatomy of a Method
Declaration (2)
… for methods that return values
public int lastFour (){
… return id % 10000;
}
Visibility modifier
return type
method name parameter list
body
CMSC 131 Fall 2008 Jan Plane (adapted from Bonnie Dorr)
27
Return Type
Methods that return values must specify the
type of the value to be returned
The bodies of these methods use return to
indicate when a value is to be returned
The value being returned must have the
same type as the return type
CMSC 131 Fall 2008 Jan Plane (adapted from Bonnie Dorr)
30
Equality Testing
CMSC 131 Fall 2008 Jan Plane (adapted from Bonnie Dorr)
31
Objects to Strings
What happens if we try to print a Student
object?
invoke println using a Student object as an argument?
Student s1 = new Student ();
System.out.println (s1);
Something like this prints:
Student@82ba
CMSC 131 Fall 2008 Jan Plane (adapted from Bonnie Dorr)
32
Java Knows “How” To Print
Any Object
CMSC 131 Fall 2008 Jan Plane (adapted from Bonnie Dorr)
33
Static Data Members and
Static Methods
Not contained in or associated with an object
of that type
Accessed by the ClassName.variableName
or by ClassName.methodName
rather than by objectName.variableName or
by objectName.methodName
CMSC 131 Fall 2008 Jan Plane (adapted from Bonnie Dorr)
36
Get Methods (Accessors)
Sole purpose is to return values of state
public int getID () {
return id;
}
Why use them?
The state information may not always be stored in a single instance variable, since implementations may change
You give designers option of changing instance variables