Object Modeling Concepts and Example

advertisement
Object Modeling
CS 2302 SPRING 2015
Object Modeling
Concepts
1/12/2015
CS 2302-SPRING 2015
2
Object Modeling Concepts
In this part we will discuss some of the concepts you studied last semester about using Java to
represent the important characteristics of objects in the real world
6/27/2016
3
Describing the Real World
Computer programs generally deal with real world objects and processes
The object oriented approach identifies to categories that help organize our understanding of
the important characteristics software needs to work with
◦ State: Descriptive characteristics
◦ Behavior: Actions the objects engage in
Groups of objects with similar characteristics are identified. These are classes.
6/27/2016
4
Abstraction
Abstraction is the process by which important charateristics of real world objects are selected
for use in software
6/27/2016
5
How it Looks in Java
Java classes are used to represent classes in the real world
State characteristics, that have been chosen in the process of abstraction, are represented by
instance variables
Behavior is represented by instance methods
6/27/2016
6
Object-Oriented Modeling
The process we have discussed here can be called object-oriented modeling.
In summary: start with the real world, select characteristics that are important, represent these
characteristics using Java.
6/27/2016
7
Java Language Concepts
We will discuss a few Java language concepts that are particularly important for object-oriented
modeling.
6/27/2016
8
Acess
Which parts of a program can directly use a particular item
For variables and methods define inside classes, this access is determined by certain key words
used in the definition
The words are: public, private and protected
One of these words will generally appear first in a definition
◦ public: any part of the program can directly access that item
◦ private: only code within the class can directly access that item
◦ protected: only code within the class or within classes in the same package or within classes that
inherit from this class (discussed later in the course) can access the item
If none of these three key words is present in a definition, then only code within the class or
within classes in the same package can access the item
6/27/2016
9
Instance Variables Should be private
Best software engineering practice suggests this rule
Access to variables will be allowed and controlled through methods
6/27/2016
10
Method Access
Methods that are not private should leave the instance variables in a consistent state
Methods that may leave instance variables in an inconsistent state should be private. These are
helper methods used by public methods
◦ Example: a class stores a data using a month variable and a day variable. A method adds one to the day.
If the original data is January 31, then the new date will be January 32.
◦ This method must be private
◦ A public method using this method must 'clean up' the discrepancy.
6/27/2016
11
Special Categories of Methods
Some types of methods are so common that they have been given names: getters, setters and
constructors
A getter method, more formally called an accessor, has no parameters, returns a value and
makes no changes to instance variables
A setter method has one parameter, returns no value and assign the value of the parameter to
one of the instance variables
A constructor method has no return type specified, not even void and has the same name as
the class, including capitalization
6/27/2016
12
Constructors
Constructors are called when the new operator is used to create a new object of the class
A constructor may specify parameters
◦ Parameters are often used to give initial values to instance variables
If the programmer does not define any constructors then the Java system provides a constructor,
called the default constructor that does the very minimum necessary to create an object.
If the programmer does define at least one constructor, then the default constructor is not
supplied. However, the programmer could define an equivalent constructor if it is needed.
6/27/2016
13
Object Modeling
Example
1/12/2015
CS 2302-SPRING 2015
14
Object Modeling Example
In this part we will create three classes that will illustrate some of the concepts we just
discussed.
This will be a continuing example that we will not complete today.
1/12/2015
CS 2302-SPRING 2015
15
The Problem
At Kennesaw, the Student L!fe office sponsors over 250 student groups. They keep track of the
groups and their memberships. Groups are organized into about a dozen categories, such as
'Pre-professional'.
To start, we will design and implement an object-oriented model of the information about
groups and their members.
We will extend this example from time to time during this semester.
1/12/2015
CS 2302-SPRING 2015
16
Identifying Classes
Groups
◦ Groups are organized around common interests and activities
Categories
◦ Groups of student groups. For example. the 'American Medical Student Association (AMSA)' group and
the 'Project Management Institute Student Chapter (PMISC)' group are part of the 'Pre-professional'
category
People
◦ Students who belong to clubs and are club officers
1/12/2015
CS 2302-SPRING 2015
17
Identifying State
Groups
◦ Name of group
◦ Description of group
Categories
◦ Category name
◦ Description
People
◦ Name
◦ E-mail address
1/12/2015
CS 2302-SPRING 2015
18
More About State
There are important aspects of the problem that we will not model right now. Later on we will
have the necessary concepts and Java tools.
Groups have people as members
A group has a president. We will keep it simple, just one president
Groups belong to categories
1/12/2015
CS 2302-SPRING 2015
19
Identifying Behavior
At this stage we will not need anything beyond 'getters', 'setters' and constructors
1/12/2015
CS 2302-SPRING 2015
20
Packages
Although the author does not use packages with the examples in the text, it is good practice to
use them.
Packages provide an excellent way to organize code when an application is made up of several
files
Package names can be simple identifiers, starting with a lower case letter
Package names can be composite, a sequence of identifiers joined with periods
For examples in this course we will use composite names starting with the identifier
cs2302sp15
1/12/2015
CS 2302-SPRING 2015
21
Java Implementation
Create a package for the code: cs2302sp15.groups
The first step is to create classes
The class names should be capitalized: this is the standard Java style.
Class names will be singular. This is not a standard style, but is suggested by some practitioners.
1/12/2015
CS 2302-SPRING 2015
22
Implementing State
Add instance variables to the classes
These will all be type String
All the variables will be private access
1/12/2015
CS 2302-SPRING 2015
23
Getters and Setters
Getters and setters can be added by using the 'generate code' feature of IntelliJ.
Most IDE's of this level will provide a generate code feature.
1/12/2015
CS 2302-SPRING 2015
24
Constructors
Add a constructor to each class
The constructor will take a parameter for each instance variable in the class and will use the
value of the parameter to initialize the instance variable.
1/12/2015
CS 2302-SPRING 2015
25
Creating Objects
Create a new class for a program.
Add the main method
Create a Group object:
◦ Name: Association for Computing Machinery Student Chapter (ACM)
◦ Description: Join us at our website and forums at http://acm.kennesaw.edu We provide assistance to
students, engage students' interests that are relative to computer science, work on variety of computing
projects, and compete in programming competitions.
1/12/2015
CS 2302-SPRING 2015
26
Download