This page is optimized for mobile devices, if you would prefer the desktop version just click here

3.2 Java3002: creating and manipulating turtles and pictures in a  (Page 8/12)

Good OOP practice

Good object-oriented programming practice says that most of the instance variables encapsulated in an object should be declared private. If thereis a need to make the contents of those variables available outside the object, that should be accomplished by defining public accessor methods. (Accessor methods are often referred to as getter methods because the name of the accessor method often includes the word "get".)

Setter methods

If there is a need for code outside the object to store information in the object's private instance variables, this should be accomplished by writingpublic setter methods. Code in the setter methods can filter incoming data to make certain that the state of the object doesn't becomecorrupt as a result of outside influences.

Pass and return by value

Everything in Java is passed and returned by value , not by reference.

Each of the accessor methods shown in Listing 4 returns a copy of the reference belonging to either a Turtle object or a World object.

Pass to the println method

As you saw earlier, each of the three references is passed to the println method in Listing 1 causing information about the objects to be displayed on the command-linescreen.

The toString method

Although it isn't obvious in Listing 1 , the code in the println method calls a method named toString on the incoming object reference and displays the string value returned by that method. I discuss the toString method in some detail here and here .

An overridden method

The toString method is overridden (not overloaded) in the World and Turtle classes so as to return a string value describing the object.

The Ericson javadocs

Normally, the javadocs would tell you what information is contained in that string, but that is not the case in Ericson's javadocs. You would have toget into her source code, (which is readily available), to get that information. However, you can see the information that is contained in thestring values for the two different types of objects in the last three lines of text in Figure 2 .

The beginning of the run method

This is where thing start to get interesting. Listing 5 shows the beginning of the public method named run .

Listing 5. The beginning of the run method.
public void run(){ //Replace the default all-white picture with another// picture. mars.setPicture(new Picture("Prob01.jpg"));

Recall that the code in the main method in Listing 1 calls the run method on the object immediately after it is instantiated.

A turtle on a white background

I told you earlier that if you were to eliminate the call to the run method, you would see a turtle at the center of the world with a white background.

The background is a Picture object

The background of a World object consists of an object of Ericson's Picture class. (A Picture object is encapsulated in the World object.)

By default, the Picture object encapsulated in a World object is all white and is exactly the right size and shape to completely fill the area inside the world's border (see Figure 1 ).

Can be replaced

As you will see shortly, we can replace the default Picture object with a new Picture object of our own choosing.

<< Chapter < Page Page > Chapter >>

Read also:

OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.
Jobilize.com uses cookies to ensure that you get the best experience. By continuing to use Jobilize.com web-site, you agree to the Terms of Use and Privacy Policy.