Chapter 12 Java Code Examples Showing Problem Domain Classes.

Slides:



Advertisements
Similar presentations
Chapter 6 Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and.
Advertisements

Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Classes  All code in a Java program is part of a class  A class has two purposes  Provide functions to do work for the programmer  Represent data.
Chapter 7 Understanding More Complex Requirements Models Using Generalization / Specialization and Whole-Part Hierarchies.
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
Object-Oriented Application Development Using VB.NET 1 Chapter 8 Understanding Inheritance and Interfaces.
Inheritance The objectives of this chapter are: To explore the concept and implications of inheritance Polymorphism To define the syntax of inheritance.
Road Map Introduction to object oriented programming. Classes
Advanced Object-Oriented Programming Features
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
© The McGraw-Hill Companies, 2006 Chapter 7 Implementing classes.
Object-Oriented Application Development Using VB.NET 1 Chapter 8 Understanding Inheritance and Interfaces.
Chapter 7 - Generalization/Specialization and Inheritance1 Chapter 7 Generalization/Specialization and Inheritance.
Games and Simulations O-O Programming in Java The Walker School
Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Chapter 5 - Writing a Problem Domain Class Definition1 Chapter 5 Writing a Problem Domain Class Definition.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Comments are for people Header comments supply basic information about the artifact.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
JAVA Tokens. Introduction A token is an individual element in a program. More than one token can appear in a single line separated by white spaces.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
OOP IN PHP `Object Oriented Programming in any language is the use of objects to represent functional parts of an application and real life entities. For.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
Creating a Java Application and Applet
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
1 Chapter 6 Programming with Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive.
Classes, Interfaces and Packages
Chapter 6 - More About Problem Domain Classes1 Chapter 6 More About Problem Domain Classes.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
Basic Syntax อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 2.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
OOP Basics Classes & Methods (c) IDMS/SQL News
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Topic: Classes and Objects
Class Inheritance Part I
Java Primer 1: Types, Classes and Operators
Chapter 3: Using Methods, Classes, and Objects
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Nested class.
CSC 113 Tutorial QUIZ I.
Chapter 3 Introduction to Classes, Objects Methods and Strings
T. Jumana Abu Shmais – AOU - Riyadh
Defining Classes and Methods
Java Programming, Second Edition
Workshop for Programming And Systems Management Teachers
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Inheritance and Polymorphism
Object Oriented Programming in java
Defining Classes and Methods
Chapter 11 Inheritance and Polymorphism Part 1
Chapter 6 Objects and Classes
Presentation transcript:

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

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

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

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

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…)

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

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.

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

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 its attributes and methods. PatientPerson class code is shown in Figure 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.

Inheritance and Polymorphism

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.

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.

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.

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

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.

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

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

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().

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

Sequence Diagram

PersonDriver2 Program

Output produced by the PersonDriver2 program