Chapter 12 Java Code Examples Showing Problem Domain

Chapter 12 Java Code Examples Showing Problem Domain Classes

Chapter Twelve Objectives Students learn Java Programming Syntax: Class Structure Sending Messages in Driver Programs Implementing key concepts of classes, objects, attributes, methods, encapsulation, messages, inheritance, polymorphism and association relationships

Introduction to Java Two slashes indicate the beginning of a comment that extends to the end of the line. Java is case sensitive (i. e. John is different JOHN). Each java statement ends with a semicolon but statements can continue from line to line without a continuation character. Each block of code is enclosed with in curly braces {}. Blocks of code are nested within each other. Example:

A block of Java code Java provides standard programming constructs, including if. . than…else, do…while, do…until and standard numeric operations.

Class Structure In java each “program” must be a class that can have attributes and methods. One source code file is created for each class, with the filename the same as the class name: the class Person is Person. java. When the file is compiled, it is named Person. class. Internal documentation is included at the top of the file as comments beginning with two slashes. The first line in the example declares the name of the class: public class Person. The key word public means that objects can be accessed by anything in the system. By

Class Structure (contd…) After the class name there is a curly brace to indicate the beginning of the class and everything inside the block belongs to the class. The end curly brace ends the class. Attributes of the person class are declared next: name and date. Of. Birth are simply variables declared in the class. A constructor is a special method used to create new object of the class.

Class Structure Standard methods are often called accessor methods because they allow access of the attributes of the class. Accessor methods that set the value of an attribute begin with the word set, as in set. Name, set. Date. Of. Birth. Methods that get the value of an attribute begin with get, as in get. Name, get. Date. Of. Birth. Method

Sending messages in Driver Programs New classes must be tested. One way to test classes is to create a driver program. That creates objects of the class and sends messages to the objects. This concept is the same as a driver program in structured program.

Sending messages in Driver Programs Person person 1, declares a variable that the driver program can assign as a reference to the new object The statement person 1. get. Name() is a message to the object referenced by the variable person 1. The object is person 1 and the message is get. Name(). The statement person 1. get. Date. Of. Birth() is another message to the person 1 object. The next line sends messages to the second person object again asking for its name and date of birth.

Sending messages in Driver Programs Note that these two objects exist as long as the driver program is running, which is for an instant. If a Java application or applet created these objects they would be running as long as the applet or application is running. To exist beyond that it would be necessary to store the new objects in a file or a database.

Sending messages in Driver Programs

Inheritance and Polymorphism It is relatively easy to create a subclass that inherits attributes and methods. Patient. Person class code is shown in Figure 12. 7. To inherit, or extend the Person class, it is only necessary to add the phrase extends Person, as in public class Patient. Person extends Person. The attributes declared are the two additional attributes employer and insurance. Company. You don’t have to redeclare the attributes of Person. Get and set methods included only apply to the additional attributes. When patient is declared it will automatically have

Inheritance and Polymorphism


Patient. Person Class A name, a date, an employer and an insurance are the four arguments in the constructor The constructor then invokes the constructor of the Person class, the superclass using the keyword super followed by the two string arguments the Person constructor expects. All of the code in Person class constructor get executed, additional statements in the Person class constructor get executed. The result is that all four values are assigned to the attributes of the new Patient. Person.

Doctor. Person subclass Also adds two attributes and four get and set methods, but they are different attributes and methods than those in the Patient. Person class. The constructor expects four strings as arguments and the superclass constructor of person is also invoked.

Polymorphism When two different types of objects respond in their own special way to the same massage. The tell. About. Self() method in Doctor. Person has the same name as the method in Patient. Person and note that the statements in both methods are similar. However when a Doctor. Person and Patient. Person is asked to tell abut itself, it responds in its own way. Note that “n” inserts a new line.


• The system in medical clinic is only interested in persons if they are either doctor or patients. • To make the Person class abstract with Java the key word abstract is inserted before the class name.

Association Relationships In association relationship is to include an attribute in each class to hold a reference to and object of the other class. The reference point is actual objects not foreign key.

The last statement in the constructor literally asks the Patient. Person object to associate with this new treatemt, by sending a message patient. Person. ass ociate. With. Treate mt(this). The key word “this” is a reference to the treatment being created.

Patient. Person 2 The Patient. Person class has to be revised to allow to associate with many treatments. The array named treatments will contain object reference of the Treatment class declared as private Treatment[] treatments.


Patient. Person 2 The expanded constructor adds statements to create the actual array of up to ten Treatment reference and initialize the treatment. Count to zero. A standard method named associated. With. Treatment is added for Patient. Person. The single statement in the method first adds one to the counter as ++treatment. Count using the increment operator.
![Patient. Person 2 public void associated. With. Treatment (Treatment a. Treatment) { treatments[treatment. Count++] Patient. Person 2 public void associated. With. Treatment (Treatment a. Treatment) { treatments[treatment. Count++]](http://slidetodoc.com/presentation_image_h2/a6c6b47dbc54d546526a556fee87b53f/image-25.jpg)
Patient. Person 2 public void associated. With. Treatment (Treatment a. Treatment) { treatments[treatment. Count++] = a. Treatment; }

Person In this sequence diagram shown a scenario in which the user asks a Patient. Person to get information about all of its Treatments. To get this job done an additional method is added named get. All. Treatments to Patient. Person 2. A driver program to test the treatment class and expanded Patient. Person 2 class. Three objects are created (Brian, Kevin and Ida). Next 7 treatment objects are created. Note that the new treatments are not assigned to object reference in the driver program. After that the driver program asks each person to tell. About. Self(). Then each person is asked to get. All. Treatments().

Patient. Person 2 public String get. All. Treatments() { String all. Treatments; all. Treatments = “Treatment for Patient” + get. Name() + “ include n”; for(int i = 0; i<treatment. Count; i++) { // Append to string here } }

Sequence Diagram

Person. Driver 2 Program

Output produced by the Person. Driver 2 program
- Slides: 30