#### Transcript Week 3 presentation - Computing Sciences

```Week 3
Introduction to Computer Science
and Object-Oriented Programming
COMP 111
George Basham
Week 3 Topics
3.1.1 Constructing Objects
3.1.2 Accessor and Mutator
Methods
3.1.3 The API Documentation
3.1.4 Implementing a Test
Program
3.1.5 Object References
3.1.1 Constructing Objects
• We will learn how to construct objects that
allow us to go beyond our earlier String
object and System.out object creation
examples
• Let’s look at the Java class library
Rectangle class
• A Rectangle object isn’t a rectangular
shape, it is an object that contains a set of
numbers that describes a rectangle
3.1.1 Constructing Objects Cont.
• Each rectangle is described by the x and y
coordinates of its top-left corner and its
width and height
• Rectangle box = new
Rectangle(5,10,20,30)
x
y
width
height
=
=
=
=
5
10
20
30
3.1.1 Constructing Objects Cont.
• The new operator makes a Rectangle
object
• It uses parameters in the call to one of the
constructors to initialize the data of the
object
• It returns the object’s address in memory
• In our example, the address (object
reference) is stored in the object variable
identified by box, of type Rectangle
3.1.2 Accessor and Mutator Methods
• A method of a class that accesses an
object and returns some information about
it without changing the object is called an
accessor method
• A method whose purpose is to modify the
state of an object (its data) is called a
mutator method
3.1.2 Accessor and Mutator Methods Cont.
• The length method of the String class
is an accessor method. It does not modify
the state of the string object, it just counts
the number of stored characters
• The Rectangle class has a number of
accessor methods, getX, getY,
getWidth and getHeight
• double width = box.getWidth();
3.1.2 Accessor and Mutator Methods Cont.
• The translate method of the Rectangle
class is a mutator method
• Translate moves a rectangle by a certain
distance in the x and y directions
• box.translate(15, 25);
• The above method call moves the
rectangle by 15 units in the x direction and
25 units in the y direction
3.1.2 Accessor and Mutator Methods Cont.
Illustration:
Effect of translate
method
Using the translate method to move a
rectangle
3.1.3 The API Documentation
• The classes and methods of the Java
library are listed in the API documentation
(application programming interface)
• The API documentation can be found on
the web at http://java.sun.com
• The API for each class starts out with a
purpose section, then summary tables for
the constructors and methods
• Click on a method link to get detailed info
3.1.4 Implementing a Test Program
1. Provide a new class
2. Supply a main method
3. Inside the main method, construct one or
more objects
4. Apply methods to the objects
5. Display the results of the method calls
import java.awt.Rectangle;
public class MoveTester
{
public static void main(String[] args)
{
Rectangle box = new Rectangle(5, 10, 20, 30);
// Move the rectangle
box.translate(15, 25);
// Print information about the moved rectangle
System.out.println("After moving, the top-left
corner is:");
System.out.println(box.getX());
System.out.println(box.getY());
}
}
Output: text message then 20 followed by 35
/**
* Test the translate method.
*/
protected void testTranslate()
{
Rectangle box = new Rectangle(5, 10, 20, 30);
box.translate(15, 25);
assertEquals(20.0, box.getX(), .0001);
assertEquals(35.0, box.getY(), .0001);
}
Example: JUnit test method.
Upon doing Run Tests a green checkmark indicates
successful test, a red X indicates that test failed.
3.1.5 Object References
• In Java a variable whose type is a class
does not hold an object, it holds the
memory location of an object.
• Object reference is the technical term to
denote the memory location of an object.
• Rectangle box = new
Rectangle(5,10,20,30)
• The variable box refers to the object that
the new operator constructed.
3.1.5 Object References Cont.
• The new operator returned a reference to
the new object, and that reference is
stored in the box variable.
• Again, the box variable does not contain
the object, it refers to the object
• Remember that number variables actually
store numbers, not a reference to the
number
3.1.5 Object References Cont.
• A number variable and an object variable
behave differently when you make a copy of the
variable:
// number variable 1:
int luckyNbr = 13;
// number variable 2 has value 13:
int luckyNbr2 = luckyNbr;
// number variable 2 now has value 12, but
// number variable 1 still has value 13:
luckyNbr2 = 12;
3.1.5 Object References Cont.
• Note object variable behavior:
// object variable 1:
Rectangle box = new Rectangle(5, 10, 20, 30);
// object variable 2 will have same value
// as object variable 1, a MEMORY ADDRESS:
Rectangle box2 = box;
// calling a mutator method on object variable
// 2 will change the object referenced by both
// variables:
box2.translate(15, 25);
3.1.5 Object References Cont.
• Thus, both these statements will produce
identical output since the object variables
reference the same object:
System.out.println(box);
System.out.println(box2);
Output:
java.awt.Rectangle[x=20,y=35,width=20,
height=30]
Reference: Big Java 4th Edition by Cay
Horstmann
3.1.1 Constructing Objects (section
2.6 in Big Java)
3.1.2 Accessor and Mutator Methods
(section 2.7 in Big Java)
3.1.3 The API Documentation (section
2.8 in Big Java)
3.1.4 Implementing a Test Program
(section 2.9 in Big Java)
3.1.5 Object References (section 2.10
in Big Java)
```