Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 12 Java Code Examples Showing Problem Domain Classes.

Similar presentations


Presentation on theme: "Chapter 12 Java Code Examples Showing Problem Domain Classes."— Presentation transcript:

1 Chapter 12 Java Code Examples Showing Problem Domain Classes

2 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

3 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: Introduction to Java

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

5 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 convention names of the classes are capitalized. Class Structure

6 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 dateOfBirth are simply variables declared in the class. A constructor is a special method used to create new object of the class. Class Structure (contd…)

7 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 setName, setDateOfBirth. Methods that get the value of an attribute begin with get, as in getName, getDateOfBirth. Method names begin with lower case. Class Structure

8 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.

9 Person person1, declares a variable that the driver program can assign as a reference to the new object The statement person1.getName() is a message to the object referenced by the variable person1. The object is person1 and the message is getName(). The statement person1.getDateOfBirth() is another message to the person1 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

10 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

11

12 Inheritance and Polymorphism It is relatively easy to create a subclass that inherits its attributes and methods. PatientPerson 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 PatientPerson extends Person. The attributes declared are the two additional attributes employer and insuranceCompany. 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 all four attributes and all eight get and set methods.

13 Inheritance and Polymorphism

14

15 PatientPerson 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 PatientPerson.

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

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

18

19 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.

20 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.

21 The last statement in the constructor literally asks the PatientPerson object to associate with this new treatemt, by sending a message patientPerson.ass ociateWithTreate mt(this). The key word “this” is a reference to the treatment being created.

22 PatientPerson2 The PatientPerson 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.

23

24 The expanded constructor adds statements to create the actual array of up to ten Treatment reference and initialize the treatmentCount to zero. A standard method named associatedWithTreatment is added for PatientPerson. The single statement in the method first adds one to the counter as ++treatmentCount using the increment operator. PatientPerson2

25 public void associatedWithTreatment (Treatment aTreatment) { treatments[treatmentCount++] = aTreatment; } PatientPerson2

26 Person In this sequence diagram shown a scenario in which the user asks a PatientPerson to get information about all of its Treatments. To get this job done an additional method is added named getAllTreatments to PatientPerson2. A driver program to test the treatment class and expanded PatientPerson2 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 tellAboutSelf(). Then each person is asked to getAllTreatments().

27 public String getAllTreatments() { String allTreatments; allTreatments = “Treatment for Patient” + getName() + “ include \n”; for(int i = 0; i<treatmentCount; i++) { // Append to string here } PatientPerson2

28 Sequence Diagram

29 PersonDriver2 Program

30 Output produced by the PersonDriver2 program


Download ppt "Chapter 12 Java Code Examples Showing Problem Domain Classes."

Similar presentations


Ads by Google