Programming Languages Chapter 3 The Focus This Creating Objects And Invoking Methods Those Objects

subject Type Homework Help
subject Pages 2
subject Words 985
subject Authors Barbara Ericson, Mark J. Guzdial

Unlock document.

This document is partially blurred.
Unlock all pages and 1 million more documents.
Get Access
page-pf1
Chapter 3
The focus of this chapter is on creating objects and invoking methods on those objects. We start
the chapter by emphasizing the importance of names. One way to introduce this concept is ask
what is the first thing you do when you get a pet? You name a pet so that you don’t have to say,
“Please walk the dog that we got two days ago”. Or what is one of the first things we do when
In section 3.2 we explain that we need to read data from files and into memory before we can do
computation on the values in the file. We also explain that we need to name things so that we
can access them once they are in memory. This is like an algebraic equation with several
variables.
In section 3.3 we introduce methods (and functions). We have students invoke class methods
and object methods. We point out that strings are immutable by showing that methods that
In section 3.4 we explain that we need to define classes so that the computer ‘knows’ what we
mean by certain types. You can point out that we expect certain things based on what type
something is. For example, if I ask a person what kind of car she drives she will probably give
me the make and model. If I am buying a used car and it doesn’t turn left, I won’t be happy
because I expect a working car to be able to turn left.
We explain that classes are like object factories. They know how to produce objects of that type.
You can show this concept with play dough and cookie cutters. We show how to create World
objects and Turtle objects and how to tell the turtles to do things like move forward, turn left,
In section 3.5 we show how to create a method. This includes showing the syntax, explaining
that methods must be inside the class definition (students often put it after the last closing
parenthesis that finishes the class definition), but not inside another method (students do this
quite a bit, too). We explain how to compile and invoke the method. We demonstrate writing a
first method that draws a square, but has the amount to go forward by specified in each forward
statement in the method. We next show that it is easier to specify the amount to go forward by
with a local variable just once in the program. And, finally we pass in the amount to go forward
by as a parameter to the method. We don’t start with this method even though it is much more
page-pf2
trouble, ask her or him to “play the turtle” and describe what the turtle is doing. Another fun
thing is to have the students write methods for drawing letters using the turtles.
Early on show the students that even though the interactions pane is reset when they compile, the
history still has the previous declarations. They can use the up arrow to see previous statements
from the history and hit enter to execute the current command.
At some point show students that they can save the interactions history in DrJava to a file (and
optionally edit it first) and then reload it as a script using the Tools menu (Figure 4). This saves
Figure 4. Tools menu with commands to Save the Interactions History and Load it as a script
In section 3.6 we show how to create a Picture object and show it. We show how to select a file
for use in creating the picture. We show several different ways to combine statements to
emphasize variable substitution. We point out that classes always start with a capital letter and
that variable and method names always start with a lowercase letter. This is something that you
have to mention many times, before students pay attention to it. Students also have trouble

Trusted by Thousands of
Students

Here are what students say about us.

Copyright ©2022 All rights reserved. | CoursePaper is not sponsored or endorsed by any college or university.