ISM 3232 - Intermediate Business Programming.

Chapter 2 Student Learning Objectives.

You should be able to:


Objects, Interfaces, and Interaction

What are the objects that interact in the following example?

Objects:

How do these objects interact?

Each object has "expertise," or functionality, in a specific, well-defined task. A TextField object stores and retrieves its own data. The applet serves as the interface between the TextFields and the orderForm by getting the data from one and passing it to the other. It represents the transaction between the customer and the sales clerk.

Java Class Hierarchy

Classification: Organizing according to similarities. Can be hierarchical. Subclasses inherit characteristics from superclasses, but add more, become more specific.

Example: automobile --> 2-door convertible --> BMW Z3

(What's another subclass of automobile? of 2-door convertible?)

Java classes: "Object" is the superclass from which all others descend. A Component is an Object, a TextComponent is also a Component, and a TextField is also a TextComponent, a Component, and an Object. These are all defined within the java.awt class library.

 

Class Definition

How can we define a class? We must answer the following 5 questions:

Example: The Rectangle Class Definition

Length and width are private variables because they are encapsulated within the Object and hidden from being able to be manipulated by other objects. The Rectangle method assigns initial values to length and width. The calculateArea method processes the variables and returns a result (the area of the rectangle).

Thus, when another Object interacts with the rectangle, it needs to supply input values, and expects to receive an output value, but doesn't need to know how the result was processed. That process, and the variables used in that process, are what is private and hidden. Public describes the elements of the interface (how the Object communicates with other Objects).

How do we execute a Rectangle class?

The class definition, as mentioned above, is just a template. In order to try it out, we must also define a test class with a main() method, which will create instances of the Rectangle, provide it with data, and receive results.

Example: RectangleUser class definition

Exercise:

CyberPet Example.

A reminder: to develop the CyberPet application, we need to answer the 5 questions above, plus use the Software Development Process steps from Chapter 1.

Problem Specification: The CyberPet will eat or sleep on command, and will report what it is doing.

Problem Decomposition: objects, data, methods, public, private.

Class Definition includes defining the data and methods.

Data: Since a pet is sleeping or not, is eating or not, and cannot be doing both at the same time, we can use boolean variables to store its state.

Methods: We need 3 methods: to be able to create (construct, or instantiate) a pet, to command it to eat, and to command it to sleep.

Which aspects of the class are private, and which are public?

The methods are how the pet interacts with other objects, so must be public. The data (variables) are internal and are thus private.

Now we have specified the design of the class and we are ready to transform it to Java code. What are the features of the Java code?

Class Header: gives the class a name (identifier), whether it's public or private, and its optional Pedigree (does it extend and/or inherit from other classes?).

Identifiers

  • An identifier is a name for a variable, method, or class.
  • Rule: An identifier in Java must begin with a letter, and may consist of any number of letters, digits, and underscore (_) characters.
  • Legal: CyberPet, eat, sleep, pet1, pet_2
  • Illegal: Cyber Pet, 30days, pet$, n!

The scope  of an identifier determines where it may be used in the program. Instance variables (isEating) and instance methods (eat()) have class scope and may be used anywhere within the class.

In general, the format for class headers is:

ClassModifiersopt class ClassName Pedigreeopt

 

Field Declaration of Instance Variables: initial state - initial values of data. Are usually declared private. Can also be protected (access allowed only by the Object's own subclasses), or, very rarely, public (access by any class).

FieldModifiersopt TypeId VariableId Initializeropt

 

Method Definition.

A method definition includes a header and a body, which contains the instructions that are invoked or executed whenever the method is called (using the name that is defined in the header).

A class's public methods make up its interface with other objects. A method should be declared public if it is used to communicate with other objects. If used for internal purposes only, it should be declared private.

 

The CyberPet's eat and sleep methods are declared public because they are used to communicate commands from other objects. They have type void because they do not return any results. They have empty parenthesis because they have no parameters (any needed information passed into the methods).

Exercise: Which statements in the above example are assignment statements? Which are output statements? Which is an example of a qualified name?


Testing the Cyberpet

How can we create a CyberPet instance and give it commands? One way, similar to what we did in Chapter 1, is to create another test class, TestCyberPet.

Pseudocode for TestCyberPet

  1. Declare reference variables.
  2. Instantiate pet1 and pet2 by creating 2 new objects of type CyberPet.
  3. Command pet1 to sleep.
  4. Command pet1 to eat.
  5. Command pet2 to sleep.

To convert to Java code, we need a class header and a main method.

This test class creates 2 new CyberPet objects, gives them names (Pet1 and Pet2). They both have an initial state of IsEating, because the CyberPet class definition sets it this way.

Then it calls the public methods, sleep and eat, to command each pet to change its initial state one or more times.

 

What is the output of this application, once it is compiled and run?

 

Output:

main() is starting

Pet is sleeping

Pet is eating

Pet is sleeping

main() is finished

There are actually 5 lines of output. Why?

Because each time the eat or sleep method is called, that call produces a line of output. Note how control is transferred each time a method is invoked.

(2) Testing the CyberPet class with an Applet (instead of an application)

(3) A third way of testing the CyberPet class is to incorporate the main() method directly into the class code. See the example on p. 88.


Object Oriented Design

How does CyberPet demonstrate object-oriented design principles?


Input in Java

Using the Buffered Reader class

BufferedReader input = new BufferedReader (new InputStreamReader(System.in));

String inputString = input.readLine();

What if the program needs numeric input? We must convert the input stream into a numeric data type, using the Integer class methods such parseInt(). We also need to protect from erroneous input, by using IOException.

Example: Grader


Primitive Data Types


Review: Study the Java Language Summary on pp. 90-99. Be able to recognize, define, and use the following Java features:

Review Exercises: #5, 10, 13, 16, 18, 21.