Chapter 5 - Writing a Problem Domain Class Definition1 Chapter 5 Writing a Problem Domain Class Definition.

Slides:



Advertisements
Similar presentations
Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view class.
Advertisements

Based on Java Software Development, 5th Ed. By Lewis &Loftus
1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
Object-Oriented Application Development Using VB.NET 1 Chapter 8 Understanding Inheritance and Interfaces.
Chapter 11: Implementing Inheritance and Association Visual Basic.NET Programming: From Problem Analysis to Program Design.
Road Map Introduction to object oriented programming. Classes
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 3rd Edition.
Object-Oriented Application Development Using VB.NET 1 Creating a String Array Code to create a String array: ' declare a String array with 4 elements.
ECE122 L6: Problem Definition and Implementation February 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 6 Problem Definition and Implementation.
Class template Describing a generic class Instantiating classes that are type- specific version of this generic class Also are called parameterized types.
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
Object-Oriented Application Development Using VB.NET 1 Chapter 7 Adding Responsibilities to Problem Domain Classes.
Chapter 10 Classes Continued
Week 3 Recap CSE 115 – Spring Constructor Special capability of a class that sets up the initial state of the object. Constructor definitions are.
Object-Oriented Application Development Using VB.NET 1 Chapter 9 Implementing Association Relationships.
Object-Oriented Application Development Using VB.NET 1 Chapter 8 Understanding Inheritance and Interfaces.
CS708 Fall 2004 Professor Douglas Moody –MW – 2:15-3:55 pm –Friday – 6:00-9:20 pm – – –Web Site: websupport1.citytech.cuny.edu.
Chapter 7 - Generalization/Specialization and Inheritance1 Chapter 7 Generalization/Specialization and Inheritance.
Chapter 9 - Implementing Association Relationships1 Chapter 9 Implementing Association Relationships.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Writing Classes (Chapter 4)
Chapter 10: Writing Class Definitions Visual Basic.NET Programming: From Problem Analysis to Program Design.
You gotta be cool. Introduction to Classes, Objects and Strings Introduction Defining a Class with a Member Function Defining a Member Function with a.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
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.
Classes CS 21a: Introduction to Computing I First Semester,
Centre for Computer Technology ICT214 Object Oriented Design and Programming Week 02 – Classes, Objects, Instances Richard Salomon and Umesh Patel Centre.
Object-Oriented Application Development Using VB.NET 1 Chapter 6 Writing a Problem Domain Class Definition.
Object-Oriented Application Development Using VB.NET 1 Chapter 6 Writing a Problem Domain Class Definition.
Chapter 4 -2 part Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
Chapter 8 - Additional Inheritance Concepts and Techniques1 Chapter 8 Additional Inheritance Concepts and Techniques.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
© 2004 Pearson Addison-Wesley. All rights reserved September 12, 2007 Encapsulation ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Dr. Azeddine Chikh IS444: Modern tools for applications development.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Object-Oriented Application Development Using VB.NET 1 Chapter 8 Understanding Inheritance and Interfaces.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
© 2004 Pearson Addison-Wesley. All rights reserved September 14, 2007 Anatomy of a Method ComS 207: Programming I (in Java) Iowa State University, FALL.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
Chapter 3 (B) 3.5 – 3.7.  Variables declared in a function definition’s body are known as local variables and can be used only from the line of their.
Classes Modeling the Object. Objects model the world Classes are programmer defined types that model the parts of a system Class serve as blueprints for.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 3 A First Look at Classes and Objects.
Chapter 6 - More About Problem Domain Classes1 Chapter 6 More About Problem Domain Classes.
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.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
Copyright © 2011 Pearson Education, Inc. Starting Out with Java: Early Objects Fourth Edition by Tony Gaddis Chapter 3: A First Look at Classes and Objects.
3 Introduction to Classes and Objects.
More About Objects and Methods
Chapter 3: Using Methods, Classes, and Objects
Creating Your OwnClasses
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Chapter 3 Introduction to Classes, Objects Methods and Strings
Corresponds with Chapter 7
Classes, Encapsulation, Methods and Constructors (Continued)
Defining Classes and Methods
Outline Anatomy of a Class Encapsulation Anatomy of a Method
Object Oriented Programming in java
CIS 199 Final Review.
Classes CS 21a: Introduction to Computing I
Classes and Objects CGS3416 Spring 2019.
Chapter 4 Test Review First day
Presentation transcript:

Chapter 5 - Writing a Problem Domain Class Definition1 Chapter 5 Writing a Problem Domain Class Definition

Chapter 5 - Writing a Problem Domain Class Definition2 Chapter 5 Topics Writing class definitions Defining attributes Writing methods Creating an instance Writing a tester class Invoking methods Working with multiple instances Writing a constructor method

Chapter 5 - Writing a Problem Domain Class Definition3 Naming Conventions Class names begin with a capital letter –Customer, Boat Attribute names begin with lowercase letters, with subsequent words capitalized –address, phoneNumber Method names begin with lowercase letters, with subsequent words capitalized, and usually contain a verb describing what the method does –setAddress, getPhoneNumber

Chapter 5 - Writing a Problem Domain Class Definition4 Developing a PD Class Definition Class Definition Structure –Class definition The Java code written to represent a class –Class header Line of code that identifies the class and some of its characteristics Keywords: –public »Indicates class has public availability –class »Indicates this line of code is a class header

Chapter 5 - Writing a Problem Domain Class Definition5

6

7 Developing a PD Class Definition Defining Attributes –Define attributes by declaring variables for each one –Specify accessibility of a variable: public –Allows any class to access the variable directly private –Prohibits direct access »Requires accessor method for access by other classes –Accessible only from within class where it is defined protected –Allows both subclasses and classes defined within the same package to have direct access

Chapter 5 - Writing a Problem Domain Class Definition8 Developing a PD Class Definition Writing Methods –Object interaction via client-server model Client object –Object sending the message »Invokes server method and possibly sends values in the form of arguments Server object –Object receiving the message »May return value to client

Chapter 5 - Writing a Problem Domain Class Definition9

10 Developing a PD Class Definition Writing Methods –Method Header (4 parts) Accessibility –public, private, or protected Data type –void or data type of returned value Method name –Following specified conventions Parameter list –Variable declarations that match the argument parameters

Chapter 5 - Writing a Problem Domain Class Definition11

Chapter 5 - Writing a Problem Domain Class Definition12 Developing a PD Class Definition Writing Methods –Accessor methods (standard method) Get accessor methods (getters) –Named with prefix “get” followed by attribute name –Retrieve attribute values Set accessor methods (setters) –Named with prefix “set” followed by attribute name –Change attribute values

Chapter 5 - Writing a Problem Domain Class Definition13

Chapter 5 - Writing a Problem Domain Class Definition14

Chapter 5 - Writing a Problem Domain Class Definition15

Chapter 5 - Writing a Problem Domain Class Definition16 Testing a PD Class Tester class –Used to simulate the way a client might send messages to a server To test the proper operation of the server’s methods –Consists of a main method that instantiates a client class object and invokes its methods

Chapter 5 - Writing a Problem Domain Class Definition17 Testing a PD Class Creating an Instance –Define a reference variable –Specify a data type –Use new keyword to create the instance Customer firstCustomer = new Customer(); –Test interactions Use a sequence diagram to show interactions between client and server –See Figure 5-8, pp. 150

Chapter 5 - Writing a Problem Domain Class Definition18

Chapter 5 - Writing a Problem Domain Class Definition19

Chapter 5 - Writing a Problem Domain Class Definition20

Chapter 5 - Writing a Problem Domain Class Definition21

Chapter 5 - Writing a Problem Domain Class Definition22

Chapter 5 - Writing a Problem Domain Class Definition23

Chapter 5 - Writing a Problem Domain Class Definition24 Testing a PD Class Creating Multiple Instances –The previous process can also be used to test multiple instances from a single tester –Example: Bradshaw Marina’s customers See Figure 5-16, pp. 157

Chapter 5 - Writing a Problem Domain Class Definition25

Chapter 5 - Writing a Problem Domain Class Definition26 Writing a Constructor Method Constructor Method –Automatically invoked when a class instance is created using the new operator Has same name as the class Has no return type –Java creates a default constructor if no constructor has been explicitly written for a class public Customer() { }

Chapter 5 - Writing a Problem Domain Class Definition27 Writing a Constructor Method Parameterized Constructor –Has parameter list to receive arguments for populating the instance attributes by: Calling the accessor (setter) methods for each instance variable or Setting the value of the instance variable directly –See pp. 160 –See Figure 5-18, pp. 162

Chapter 5 - Writing a Problem Domain Class Definition28 Writing a tellAboutSelf Method tellAboutSelf Method –Single method that provides requestor with a String that contains all the instance variable names and values –Alternative to invoking individual accessor (getter) methods –See pp. 163 and Figure 5-20 on pp.164