Section 3.1 Introduction
In this chapter, we take a look at Java methods and parameters. Methods and parameters are the primary mechanisms for passing information into and out of an object. We will once again focus on the
OneRowNim
simulation that we designed in the previous chapter. That version was sufficient to introduce us to Java objects and classes, but it was limited in its ability to communicate with other objects.In this chapter, we want to expand
OneRowNim
to make our simulation more realistic. We begin by learning how to pass information to an object. That will enable us to specify the number of sticks to remove using a single method. We then consider special methods called constructors, which are used to initialize an object’s state when it is created. We also learn how to retrieve information from an object. That will enable us to request a OneRowNim
object for several different bits of information. Then we consider the if-else
and while
control structures which allow us to define more useful methods and write more realistic test programs.You have attempted of activities on this page.