Chapter 6 - More About Problem Domain Classes1 Chapter 6 More About Problem Domain Classes.

Slides:



Advertisements
Similar presentations
Topics Introduction Types of Errors Exceptions Exception Handling
Advertisements

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Four Defining Your Own Classes.
Written by: Dr. JJ Shepherd
Exceptions Don’t Frustrate Your User – Handle Errors KR – CS 1401 Spring 2005 Picture – sysprog.net.
Exception Handling Chapter 15 2 What You Will Learn Use try, throw, catch to watch for indicate exceptions handle How to process exceptions and failures.
Exceptions, Templates, And The Standard Template Library (STL) Chapter 16.
 Both System.out and System.err are streams—a sequence of bytes.  System.out (the standard output stream) displays output  System.err (the standard.
Copyright © 2012 Pearson Education, Inc. Chapter 16: Exceptions, Templates, and the Standard Template Library (STL)
Object-Oriented Application Development Using VB.NET 1 Chapter 8 Understanding Inheritance and Interfaces.
Road Map Introduction to object oriented programming. Classes
Slides prepared by Rose Williams, Binghamton University Chapter 9 Exception Handling.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Slides prepared by Rose Williams, Binghamton University Chapter 9 More Exception Handling.
Object-Oriented Application Development Using VB.NET 1 Chapter 7 Adding Responsibilities to Problem Domain Classes.
Chapter 12: Advanced Topics: Exception Handling Visual Basic.NET Programming: From Problem Analysis to Program Design.
Chapter 10 Classes Continued
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.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
CSCI 1730 January 17 th, 2012 © by Pearson Education, Inc. All Rights Reserved.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch.
Chapter 12: Exception Handling
Chapter 5 - Writing a Problem Domain Class Definition1 Chapter 5 Writing a Problem Domain Class Definition.
Chapter 10: Writing Class Definitions Visual Basic.NET Programming: From Problem Analysis to Program Design.
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.
The Java Programming Language
Sahar Mosleh California State University San MarcosPage 1 System.out.println for console output System.out is an object that is part of the Java language.
Input, Output, and Processing
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.
ECE122 Feb. 22, Any question on Vehicle sample code?
Chapter 8 - Additional Inheritance Concepts and Techniques1 Chapter 8 Additional Inheritance Concepts and Techniques.
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.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Object-Oriented Application Development Using VB.NET 1 Chapter 8 Understanding Inheritance and Interfaces.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
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.
Chapter 5 Introduction to Defining Classes
1 Chapter 5: Defining Classes. 2 Basics of Classes An object is a member of a class type What is a class? Fields & Methods Types of variables: –Instance:
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.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Introduction to Object-Oriented Programming Lesson 2.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Java Programming, Second Edition Chapter Three Using Methods, Classes, and Objects.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
Classes - Intermediate
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
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.
Variables and Constants Chapter 4 Review. Declaring Variables A variable is a name for a value stored in memory. Variables and constants are used in programs.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 4th Edition.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Inheritance and Polymorphism
Java Primer 1: Types, Classes and Operators
Exception Handling Chapter 9.
Chapter 3 Introduction to Classes, Objects Methods and Strings
Defining Your Own Classes
Chapter 6 Methods: A Deeper Look
Exception Handling Chapter 9 Edited by JJ.
Introduction to Classes and Objects
Object Oriented Programming in java
CIS 199 Final Review.
Presentation transcript:

Chapter 6 - More About Problem Domain Classes1 Chapter 6 More About Problem Domain Classes

Chapter 6 - More About Problem Domain Classes2 Chapter 6 Topics More about writing problem domain classes Writing and invoking custom methods Formatting numerical data for display Using static variables and methods Writing overloaded methods Working with exceptions

Chapter 6 - More About Problem Domain Classes3 Writing a Definition for the Slip Class Process –Write class header –Write attribute definition statements –Write a parameterized constructor Argument data types must be assignment compatible with parameter data types

Chapter 6 - More About Problem Domain Classes4

5 Writing a Definition for the Slip Class Process (cont.) –Write accessors to populate the attributes –Write a tellAboutSelf method Polymorphic method –Two methods with the same name residing in different classes that behave differently –See Figure 6-2, pp 172 –Write a tester class

Chapter 6 - More About Problem Domain Classes6 See Figure 6-4 for the Sequence diagram

Chapter 6 - More About Problem Domain Classes7

8 Writing Custom Methods Standard Methods –Written to store and retrieve values Accessor methods –getXXX(), setXXX() Custom Methods –Written to do some processing

Chapter 6 - More About Problem Domain Classes9 Writing Custom Methods Custom Methods –Process Write method header –Public accessibility (if required) –Appropriate return type to match data type of value returned (if any) Write code to implement required process Write tester class to demonstrate proper operation

Chapter 6 - More About Problem Domain Classes10

Chapter 6 - More About Problem Domain Classes11 Formatting Output NumberFormat & DecimalFormat Classes –NumberFormat Class Member of java.text package Provides methods to format numerical data as currency with commas, dollar signs, and decimal points Also provides for formatting currency for various countries

Chapter 6 - More About Problem Domain Classes12 Formatting Output NumberFormat & DecimalFormat Classes –NumberFormat Class Two steps to format data: –Invoke getCurrencyInstance method to obtain a NumberFormat instance »NumberFormat currencyFormat = NumberFormat.getCurrencyInstance(); –Invoke the format method for the instance obtained »System.out.println(“Currency: “ + currencyFormat.format(fee);

Chapter 6 - More About Problem Domain Classes13 Formatting Output NumberFormat & DecimalFormat Classes –DecimalFormat Class Member of java.text package Provides methods to format numerical data with commas and a decimal point

Chapter 6 - More About Problem Domain Classes14 Formatting Output NumberFormat & DecimalFormat Classes –DecimalFormat Class Two steps to format data: –Create an instance of DecimalFormat using the new operator and pass the format mask »DecimalFormat decimalFormat = new DecimalFormat(“##,##0.00”); –Invoke the format method for the instance obtained »System.out.println(“Decimal: “ + decimalFormat.format(fee);

Chapter 6 - More About Problem Domain Classes15 Formatting Output Using Escape Sequences –Escape sequence The backslash character (\) followed by the escape character Used to display: –Characters that do not appear on the keyboard –Characters that have special meanings within certain contexts Example: –Tab  \t –Double quote in an output String  “the \”real\” deal”

Chapter 6 - More About Problem Domain Classes16

Chapter 6 - More About Problem Domain Classes17

Chapter 6 - More About Problem Domain Classes18

Chapter 6 - More About Problem Domain Classes19 Using Static Variables and Methods Instance Variables and Methods –A new instance receives its own copy of all instance variables and methods Methods not actually copied - to avoid redundancy Class Variables and Methods –A new instance shares a copy of all class variables and methods Keyword –Static  used to declare class variables and methods as static See Figure 6-11, pp. 185

Chapter 6 - More About Problem Domain Classes20

Chapter 6 - More About Problem Domain Classes21

Chapter 6 - More About Problem Domain Classes22 Overloading Methods Method Signature –Consists of: Method name Its parameter list –Java identifies a method by its signature Overloaded method –Methods within the same class having the same name but a different signature

Chapter 6 - More About Problem Domain Classes23 Overloading Methods Overridden Methods (polymorphism) –A method with the same signature as an inherited method Replaces inherited method Polymorphic Method –A method in one class has the same signature as a method in another class

Chapter 6 - More About Problem Domain Classes24 Overloading Methods Overloading a Constructor –Multiple constructors with the same name and different signatures –Should have a constructor for each way it makes sense to instantiate objects of the class Overloading a Custom Method –Any method can be overloaded –Should have a method for each way it makes sense to input data to perform the required process See Figure 6-14, pp. 191

Chapter 6 - More About Problem Domain Classes25 overload constructor overload custom method Figure 6-14

Chapter 6 - More About Problem Domain Classes26

Chapter 6 - More About Problem Domain Classes27

Chapter 6 - More About Problem Domain Classes28 Working with Exceptions Exception –An object instance that notifies you of errors, problems, and other unusual conditions that may occur when your system is running –Keywords: try catch finally throw throws

Chapter 6 - More About Problem Domain Classes29

Chapter 6 - More About Problem Domain Classes30 Working with Exceptions Exception Process –When a client invokes a method that may create and throw an exception, the invoking code must be placed in a try block –Server method indicates it may throw an exception by including throws keyword in header –If exception is detected, server sends exception instance to invoking client using throw keyword –Client catches exception in a catch block –finally block executes regardless of whether an exception is caught

Chapter 6 - More About Problem Domain Classes31 Working with Exceptions Data Validation –If a method is to create and throw an exception, its header must contain the throws keyword followed by the exception class it throws public void setSlipId(int anId) throws Exception public void setWidth(int aWidth) throws Exception

Chapter 6 - More About Problem Domain Classes32 Working with Exceptions Catching Exceptions –If a method contains throws in its header, the invoking code must be prepared to catch the exception Otherwise JVM will terminate processing try { method that throws exception } catch (Exception e) { code that handles it } –See Figure 6-18, pp. 197 –See Figure 6-19, pp. 201