ISM 3232 - Intermediate Business Programming. 

Chapter 3 Student Learning Objectives.

You should be able to:


Access Methods

A private variable cannot be accessed by other objects.

How do we set the values of private variables within an object?

How do we get information from an object?

By convention, many classes define access methods to assign and retrieve the values of selected private variables. This is how the object interacts with other objects. The "set_variable" access method will take a parameter which contains the value of the variable.

A parameter is the formal definition of the how information is passed into a method. A method may have none, one, or more parameters. The parameter list follows the name of the method and is enclosed within parentheses:

MethodModifiers ResultType MethodName (FormalParameterList) MethodBody

Example: public void setName(String str)

 

Parameter Scope

Scope defines where a variable can be used in a program

Drawing boxes around program modules is a good way to visualize scoping rules.

 

Arguments and Parameters

Although "parameter" and "argument" are often used interchangeably, we will use parameter to refer to the formal definition, and argument to refer to the actual value that is passed when the method is invoked.

Example of defining vs. invoking:

 

Parameters and the Generality Principle

Exercise: Modify the CyberPet and the TestCyberPet classes to add a name variable, and then to set and then retrieve the value. (See pp. 119 and 125 for the solution.)


Constructor Methods

Examples:

public CyberPet()
{
   isSleeping = false;
   isEating = true;
}
public CyberPet(String str)
{
  name = str;
}

Note:

 

Default Constructors

Example:


Constructor Overloading

 

Invoking a Constructor Method

A method call (invocation) must exactly match the parameter names, types, and order of the parameters in a method definition. If the compiler can't find a match, it will complain.

Retrieving Information from an Object

Now that we know how to create an object and initialize its instance variables, we need to be able to retrieve those values, by defining more methods for the class.

In the case of CyberPet, we need a get_name() method, which will return the current value of the pet's name variable.

In this example, first getName() is defined.

Then, 2 pet objects are created.

Then we invoke the getName() method and output the value of pet1's name variable.

 

Click here to view the version of the CyberPet (2) class definition that includes these constructor methods and access methods.

Value vs. Reference

The effect of passing an argument (value) differs, depending on whether the argument is a reference value or a primitive value

For primitive type parameters, a copy of the argument’s value is passed to the method.

 

For reference parameters, a reference to an object is passed to the method.

 


Selection Control in Java: If ...

Review: a selection control structure changes the result based on the evaluation of some condition(s) as TRUE or FALSE. The flow of control in selection looks like the following flowcharts. The flow of control can be one-way, two-way, or multi-way.

This is an example of one-way selection. If the boolean expression evaluates to true, the statement will be executed. Otherwise, it will be skipped.

Two-way selection incorporates "else" - the flow of control goes one of 2 ways depending upon the evaluation of the condition.

 

Multi-way selection: multiple conditions are checked to determine the flow of control.

Examples:

Note that the compiler ignores indenting. The compiler uses the keywords, the semi-colons, and the brackets to determine the beginning and end of statements and blocks. However, indenting is extremely important for readability.

Boolean Expressions

Now suppose we want to define a method that returns the current state of the pet, i.e., whether it is eating or sleeping.

 

Click here to review the version of the CyberPet (3) that includes the getState() method (an example of using If ... Else).

The Dangling Else Problem


Object Inheritance

See ToString() example, pp. 149-152.


For Review: #1, 3, 5, 7, 10.