Chapter06_Lecture1
Chapter06_Lecture1
Chapter 6
Course Main Objective
On successful completion of this course, students will be able to:
• Explain the steps in creating an executable computer program using object-oriented concepts.
• Design object-oriented solutions for moderately complex applications involving multiple objects.
• Explain the benefits of code reuse by learning how to make use of off-the-shelf Java libraries.
Assessment Percentage of Total Assessment
Score
Assignments and Quizzes and Class activates 10%
Mid-term Exam 20%
Final Exam 50% (30 theoretical + 20 practical)
Lab evaluation exercises and Project 20% (10 exercises + 10 Project)
• Required Textbooks: Gaddis, Tony. Starting Out with Java: From Control
Structures through Objects. Pearson. 6th edition. Pearson.
6-9
Objects and Classes
6-10
Objects and Classes
keyboard Scanner
variable object
Objects and Classes
This expression creates a
Example: Random object in memory.
rand Random
variable object
Objects and Classes
This expression creates a
Example: PrintWriter object in memory.
outputFile PrintWriter
variable object
Primitive Variables
Objects and Classes
• Examples:
• Scanner
• Random
• PrintWriter
• See ObjectDemo.java
Writing a Class, Step by Step
6-20
Writing a Class, Step by Step
• The Rectangle class will also have the following methods:
• setLength. The setLength method will store a value in an object’s length
field.
• setWidth. The setWidth method will store a value in an object’s width field.
• getLength. The getLength method will return the value in an object’s
length field.
• getWidth. The getWidth method will return the value in an object’s width
field.
• getArea. The getArea method will return the area of the rectangle, which is the
result of the object’s length multiplied by its width.
6-21
UML Diagram
6-22
UML Diagram for Rectangle class
Rectangle
length
width
setLength()
setWidth()
getLength()
getWidth()
getArea()
6-23
Writing the Code for the Class Fields
6-24
Access Specifiers
• An access specifier is a Java keyword that indicates how a field or
method can be accessed.
• public
• When the public access specifier is applied to a class member, the member
can be accessed by code inside the class or outside.
• private
• When the private access specifier is applied to a class member, the member
cannot be accessed by code outside the class. The member can be accessed
only by methods that are members of the same class.
6-25
Header for the setLength Method
/**
The setLength method stores a value in the
length field.
@param len The value to store in length.
*/
public void setLength(double len)
{
length = len;
}
Examples: Rectangle.java, LengthDemo.java
6-27
Rectangle.java
LengthDemo.java
Creating a Rectangle object
6-30
Calling the setLength Method
box.setLength(10.0);
The box A Rectangle object
variable holds the
address of the length: 10.0
address
Rectangle width: 0.0
object.
6-32
Writing and Demonstrating the getArea Method
/**
object's area.
*/
• The methods that retrieve the data of fields are called accessors.
• The methods that modify the data of fields are called mutators.
• Each field that the programmer wishes to be viewed by other classes needs an accessor.
• Each field that the programmer wishes to be modified by other classes needs a mutator.
6-34
Accessors and Mutators
• For the Rectangle example, the accessors and mutators are:
• setLength : Sets the value of the length field.
public void setLength(double len) …
• An object hides its internal, private fields from code that is outside the
class that the object is an instance of.
• Only the class's methods may directly access and make changes to the
object’s internal data.
• Code outside the class must use the class's public methods to operate on an
object's private fields.
Data Hiding
6-38
Stale Data
• Rather than use an area variable in a Rectangle class:
public double getArea()
{
return length * width;
}
• This dynamically calculates the value of the rectangle’s area when the method is
called.
• Now, any change to the length or width variables will not leave the area of the
rectangle stale.
6-39
UML Data Type and Parameter Notation
6-40
UML Data Type and Parameter Notation
6-41
UML Data Type and Parameter Notation
6-42
UML Data Type and Parameter Notation
6-43
Converting the UML Diagram to Code
• Putting all of this information together, a Java class file can be built
easily using the UML diagram.
• The UML diagram parts match the Java class file structure.
class header
ClassName
{
Fields Fields
Methods Methods
}
6-44
Converting the UML Diagram to Code
The structure of the class can be public class Rectangle
compiled and tested without having {
bodies for the methods. Just be sure to private double width;
put in dummy return values for methods private double length;
that have a return type other than void.
public void setWidth(double w)
{
Rectangle }
public void setLength(double len)
{
- width : double }
- length : double public double getWidth()
{ return 0.0;
+ setWidth(w : double) : void }
+ setLength(len : double): void public double getLength()
{ return 0.0;
+ getWidth() : double }
+ getLength() : double public double getArea()
+ getArea() : double { return 0.0;
}
6-45 }
Converting the UML Diagram to Code
Once the class structure has been public class Rectangle
{
tested, the method bodies can be
private double width;
written and tested. private double length;
• Fields and methods that are declared as previously shown are called
instance fields and instance methods.
• Objects created from a class each have their own copy of instance
fields.
• Instance methods are methods that are not declared with a special
keyword, static.
6-47
Instance Fields and Methods
• Note that each room represented in this example can have different
dimensions.
Rectangle kitchen = new Rectangle();
Rectangle bedroom = new Rectangle();
Rectangle den = new Rectangle();
6-48
States of Three Different Rectangle Objects
The kitchen variable
holds the address of a
length: 10.0
address
Rectangle Object.
width: 14.0
6-49
Constructors
6-50
Constructors
• Constructors have a few special properties that set them apart from
normal methods.
• Constructors have the same name as the class.
6-51
Constructor for Rectangle Class
/**
Constructor
*/
length = len;
width = w;
6-52
Constructors in UML
Rectangle
Notice there is no
return type listed
- width : double for constructors.
- length : double
+Rectangle(len:double, w:double)
+ setWidth(w : double) : void
+ setLength(len : double): void
+ getWidth() : double
+ getLength() : double
+ getArea() : double
6-53
Uninitialized Local Reference Variables
• Reference variables can be declared without being initialized.
Rectangle box;
• This statement does not create a Rectangle object, so it is an uninitialized local reference
variable.
• A local reference variable must reference an object before it can be used, otherwise a compiler
error will occur.
• box will now reference a Rectangle object of length 7.0 and width 14.0.
6-54
The Default Constructor
• If you do not write a constructor, Java provides one when the class is
compiled. The constructor that Java provides is known as the default
constructor.
• It sets all of the object’s numeric fields to 0.
• It sets all of the object’s boolean fields to false.
• It sets all of the object’s reference variables to the special value null.
6-55
The Default Constructor
• The default constructor is a constructor with no parameters, used to initialize an
object in a default configuration.
• The only time that Java provides a default constructor is when you do not write
any constructor for a class.
• See example: First version of Rectangle.java
6-56
Writing Your Own No-Arg Constructor
• A constructor that does not accept arguments is known as a no-arg
constructor.
• The default constructor (provided by Java) is a no-arg constructor.
• We can write our own no-arg constructor
public Rectangle()
{
length = 1.0;
width = 1.0;
}
6-57
The String Class Constructor
• For instance:
6-58
The String Class Constructor
• Because they are used so often, String objects can be created with a
shorthand:
6-59
Passing Objects as Arguments
• When you pass a object as an argument, the thing that is passed into
the parameter variable is the object's memory address.
• See DieArgument.java
Overloading Methods and Constructors
• Two or more methods in a class may have the same name as long as
their parameter lists are different.
6-61
Overloaded Method add
public int add(int num1, int num2)
{
int sum = num1 + num2;
return sum;
}
6-62
Method Signature and Binding
• A method signature consists of the method’s name and the data types of the method’s
parameters, in the order that they appear. The return type is not part of the signature.
Signatures of the
add(int, int)
add methods of
add(String, String)
previous slide
• The process of matching a method call with the correct method is known as binding. The
compiler uses the method signature to determine which version of the overloaded method
to bind the call to.
6-63
Rectangle Class Constructor Overload
6-64
Rectangle Class Constructor Overload
If we were to add the no-arg constructor we wrote previously to our
Rectangle class in addition to the original constructor we wrote,
what would happen when we execute the following calls?
The first call would use the no-arg constructor and box1 would have a length of 1.0
and width of 1.0.
The second call would use the original constructor and box2 would have a length
of 5.0 and a width of 10.0.
6-65
The BankAccount Example
BankAccount.java BankAccount
AccountTest.java -balance:double
+BankAccount()
6-66
Scope of Instance Fields
6-67
Shadowing
• A parameter variable is, in effect, a local variable.
• Within a method, variable names must be unique.
• A method may have a local variable with the same name as an instance field.
• This is called shadowing.
• The local variable will hide the value of the instance field.
• Shadowing is discouraged and local variable names should not be the same as
instance field names.
6-68
Packages and import Statements
6-69
Some Java Standard Packages
6-70
Object Oriented Design
Finding Classes and Their Responsibilities
6-71