Summer A-2000, Project Course-- Carnegie Mellon University 1 Financial Engineering Project Course.

Slides:



Advertisements
Similar presentations
Inner Classes. Nested Classes  An nested class is a class that is defined inside another class.  To this point we have only studied top-level classes.
Advertisements

Unit 08 & 091 Nested Classes Introduction Inner Classes Local Classes Anonymous Classes Exercises.
Just Java Chapter 81 Lecture 4 “Just Java” Chapter 8 – Interfaces and Cloning Chapter 9 – Packages, Visibility and Nested classes.
Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Financial Engineering Project Course. Lecture 3 Object Oriented Design Inheritance Abstract Base Classes Polymorphism Using XML to represent the swap.
Summer A-2000, Project Course-- Carnegie Mellon University 1 Financial Engineering Project Course.
OOP Java1 Event Handling Overview Listeners, Adapters and Event Sources Inner classes Event Handling Details Applets and GUI Applications Event.
Inner Classes. Lecture Objectives Learn about inner classes. Know the differences between static and non- static inner classes. Designing and using inner.
Processing XML Processing XML using XSLT Processing XML documents with Java (DOM) Next week -- Processing XML documents with Java (SAX)
Unit 081 Introduction to Nested Classes Nested classes are classes defined within other classes The class that includes the nested class is called the.
OOP in Java – Inner Classes Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.
Understanding class definitions Looking inside classes.
Inner Classes. Inner classes All the classes so far have been “top level” It is possible (and useful) to define a class inside another class Inner classes.
Java CourseWinter 2009/10. Introduction Object oriented, imperative programming language. Developed: Inspired by C++ programming language.
Introduction to Methods
Lecture From Chapter 6 & /8/10 1 Method of Classes.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
ACM/JETT Workshop - August 4-5, ExceptionHandling and User Interfaces (Event Delegation, Inner classes) using Swing.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
1 XML at a neighborhood university near you Innovation 2005 September 16, 2005 Kwok-Bun Yue University of Houston-Clear Lake.
CSC 142 B 1 CSC 142 Java objects: a first view [Reading: chapters 1 & 2]
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.
XML Processing in Java. Required tools Sun JDK 1.4, e.g.: JAXP (part of Java Web Services Developer Pack, already in Sun.
Sheet 1XML Technology in E-Commerce 2001Lecture 3 XML Technology in E-Commerce Lecture 3 DOM and SAX.
Learners Support Publications Classes and Objects.
FIRST JAVA PROGRAM. JAVA PROGRAMS Every program may consist of 1 or more classes. Syntax of a class: Each class can contain 1 or more methods. public.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
2-Dec-15 Inner Classes. 2 Inner classes All the classes so far have been “top level” It is possible (and useful) to define a class inside another class.
Java Inner Classes. Interfaces interface KeyListener { public void keyPressed(KeyEvent e); public void keyReleased(KeyEvent e); public void keyTyped(KeyEvent.
2-Dec-15 Inner Classes By Alguien Soy. 2 Inner classes All the classes so far have been “top level” It is possible (and useful) to define a class inside.
CSC 142 B 1 CSC 142 Java objects: a first view [Reading: chapters 1 & 2]
CS170 ygao JAVA, C4Slide 1. CS170 ygao JAVA, C4Slide 2.
CMSC 341 Java Packages, Classes, Variables, Expressions, Flow Control, and Exceptions.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
An Advanced Code Pattern: Inner Classes CSE301 University of Sunderland Harry R. Erwin, PhD Half Lecture.
Nested Classes CompSci 230 S Software Construction.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
LECTURE 8: EXCEPTIONS CSC 212 – Data Structures. Error Handling Goals  What should we do when an error occurs?  Should alert system to the error  May.
OOP Basics Classes & Methods (c) IDMS/SQL News
Object Oriented Programming in Java Habib Rostami Lecture 10.
7-Mar-16 Simple API XML.  SAX and DOM are standards for XML parsers-- program APIs to read and interpret XML files  DOM is a W3C standard  SAX is an.
Advanced Java class Nested Classes & Interfaces. Types of Nested Classes & Interfaces top-level nested –classes –interfaces inner classes –member –local.
Creating Java Applications (Software Development Life Cycle) 1. specify the problem requirements - clarify 2. analyze the problem - Input? Processes? Output.
1 DemoBasic_v3, DemoBasic_v4 JButton JLabel. 2 Registering an ActionListener Register by invoking the following from within constructor DemoBasicFrame.
Java for android Development Nasrullah Khan. Using instanceof in Android Development the classes such as Button, TextView, and CheckBox, which represent.
Inner Classes.
Topic: Inner Classes Course : JAVA PROGRAMMING Paper Code: ETCS-307 Faculty : Dr. Prabhjot Kaur Reader, Dept. of IT 1.
Inner Classes 27-Dec-17.
CompSci 230 S Software Construction
Exceptions, Interfaces & Generics
CompSci 230 S Programming Techniques
Inner Classes 11/14/ Dec-04 inner_classes.ppt.
Java Programming Language
Inner Classes 29-Nov-18.
Interfaces.
Classes and Objects.
Java Inner Classes.
CMPE212 – Reminders Assignment 3 due next Friday.
Inner Classes 17-Apr-19.
Inner Classes 21-Apr-19.
Inner Classes 1-May-19.
CSE 341 Lecture 11 b closures; scoping rules
Inner Classes 11-May-19.
Inner Classes 18-May-19.
Inner Classes.
...that can get messy when we have lots buttons!
Inner Classes 25-Oct-19.
Presentation transcript:

Summer A-2000, Project Course-- Carnegie Mellon University 1 Financial Engineering Project Course

Summer A-2000, Project Course-- Carnegie Mellon University 2 Lecture 5 Validation against a DTD More Java Details Validating the swap agreement using Sun’s JAXP API

Summer A-2000, Project Course-- Carnegie Mellon University 3 Checking the Structure of an XML document with an XML parser Document Structure Rules (DTD) Valid XML Invalid XML XML Rules Checker (Parser)

Summer A-2000, Project Course-- Carnegie Mellon University 4 Operation of a Tree-based Parser Tree-Based Parser Application Logic Document Tree Valid XML DTD XML Document

Summer A-2000, Project Course-- Carnegie Mellon University 5 The Agreement.xml file This document declares itself as conforming to this dtd.

Summer A-2000, Project Course-- Carnegie Mellon University 6 The FixedFloatSwap.dtd <!ELEMENT FixedFloatSwap (Notional, Fixed_Rate, NumYears, NumPayments) >

Summer A-2000, Project Course-- Carnegie Mellon University 7 The FixedFloatSwap.dtd <!ELEMENT FixedFloatSwap (Notional, Fixed_Rate, NumYears, NumPayments) > A document that conforms to this dtd must have these elements in this order.

Summer A-2000, Project Course-- Carnegie Mellon University 8 The FixedFloatSwap.dtd The tag names in the xml document. The content of each element

Summer A-2000, Project Course-- Carnegie Mellon University 9 Before we validate we have to cover some more Java Inner classes Exception Handling

Summer A-2000, Project Course-- Carnegie Mellon University 10 Inner Classes Nested Top Level Classes (not inner) Member Classes Local Classes Anonymous Classes

Summer A-2000, Project Course-- Carnegie Mellon University 11 Nested Top Level Class Nested top-level classes are not inner classes. Use as a convenient way to group related classes Since the class must be static and has no 'this' pointer, it has no access to the instance data of objects for its enclosing class. It behaves just like a 'normal' class or interface.

Summer A-2000, Project Course-- Carnegie Mellon University 12 //NestedTopLevelExample.java class Top { int i,j; static class SomeClass { // static makes it top-level nested int k; SomeClass() { System.out.println("Constructing SomeClass"); } void foo() { System.out.println("Hello"); } } Top() { System.out.println("Constructing a Top object"); }

Summer A-2000, Project Course-- Carnegie Mellon University 13 public class NestedTopLevelExample { public static void main(String args[]) { Top myTop = new Top(); Top.SomeClass myObject = new Top.SomeClass(); myObject.foo(); } Output Constructing a Top object Constructing SomeClass Hello

Summer A-2000, Project Course-- Carnegie Mellon University 14 Member Classes Member classes (there is no such thing as a 'member‘ interface) This inner class (it's not a top-level class) has no static keyword and can access the members of each object of its outer class. The class 'appears in every instance'.

Summer A-2000, Project Course-- Carnegie Mellon University 15 The parent class must declare an instance of an inner class, before it can invoke the inner class methods, assign to data fields (including private ones), and so on. Unlike nested top-level classes, inner classes are not directly part of a package and are not visible outside the class in which they are nested. Inner classes are often used for GUI event handlers.

Summer A-2000, Project Course-- Carnegie Mellon University 16 // MemberClassExample.java class Top { int i = 33; public class SomeClass { // access the outer object's state. private int k = i; SomeClass() { System.out.println("Constructing SomeClass"); } void foo() { System.out.println("Hello"); } } Top() { System.out.println("Constructing a Top object"); SomeClass sc = new SomeClass(); System.out.println(sc.k); }

Summer A-2000, Project Course-- Carnegie Mellon University 17 public class MemberClassExample { public static void main(String args[]) { Top myObject = new Top(); } // OUTPUT Constructing a Top object Constructing SomeClass 33

Summer A-2000, Project Course-- Carnegie Mellon University 18 Local Classes A Local class is an inner class. Typically, a local class is declared within a method. It is not a member of an enclosing class. It is visible only within the block. These classes are used primarily as "adapter classes". For example, a block of code that creates a Button object could use a local class to define a simple implementation of the ActionListener Interface. Then it could instantiate this simple implementation and pass the resulting object to the button's ActionListener method, thereby connecting the button to the "callback" code that is executed when the button is pressed.

Summer A-2000, Project Course-- Carnegie Mellon University 19 // Local Class example class Top { int i = 33; Top() { System.out.println("Constructing a Top object"); // define a class within a method class Wow { int t; Wow() { System.out.println("Building a Wow"); i = 8; t = 9; } Wow h = new Wow(); System.out.println(" h.t == " + h.t); System.out.println(" i == " + i); }

Summer A-2000, Project Course-- Carnegie Mellon University 20 public class LocalExample { public static void main(String args[]) { Top myObject = new Top(); } // OUTPUT Constructing a Top object Building a Wow h.t == 9 i == 8

Summer A-2000, Project Course-- Carnegie Mellon University 21 An anonymous class is refinement of inner classes. It allows you to combine the definition of the class with the instance allocation. Since it is instantiated in the same expression that defines it, it can only be instantiated once. This is very similar to local classes. When writing a simple adapter class, the choice between a named local class and an unnamed anonymous class typically comes down to a matter of style and code clarity, rather than any difference in functionality. The new class can't have a constructor. Anonymous Classes

Summer A-2000, Project Course-- Carnegie Mellon University 22 // Anonymous.java interface SmallClass { public void foo(); } class Top { int i = 33; void someMethod(SmallClass s) { s.foo(); } void anotherMethod() { someMethod(new SmallClass() { public void foo() { System.out.println("Really fun"); } }); }

Summer A-2000, Project Course-- Carnegie Mellon University 23 Top() { System.out.println("Constructing a Top object"); someMethod(new SmallClass() { public void foo() { System.out.println("Strange but fun"); } }); }

Summer A-2000, Project Course-- Carnegie Mellon University 24 public class Anonymous { public static void main(String args[]) { // We can't create interface objects // error: SmallClass s = new SmallClass(); Top myObject = new Top(); myObject.anotherMethod(); } // OUTPUT Constructing a Top object Strange but fun Really fun

Summer A-2000, Project Course-- Carnegie Mellon University 25 Validating two Agreement.xml files import java.io.File; import org.w3c.dom.*; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilder; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException;

Summer A-2000, Project Course-- Carnegie Mellon University 26 public class Simulator4 { public static void main(String argv[]) { if(argv.length != 2 ) { System.err.println("usage: java Simulator4” + “document1Name document2Name"); System.exit(1); } Validating two Agreement.xml files Are both xml files on the command line? // imports as before

Summer A-2000, Project Course-- Carnegie Mellon University 27 Validating two Agreement.xml files try { DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); docBuilderFactory.setValidating(true); docBuilderFactory.setNamespaceAware(true); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); This factory will produce parsers that validate!

Summer A-2000, Project Course-- Carnegie Mellon University 28 docBuilder.setErrorHandler( new org.xml.sax.ErrorHandler() { public void fatalError(SAXParseException e) throws SAXException {} public void error(SAXParseException e) throws SAXParseException { throw e; } public void warning(SAXParseException err) throws SAXParseException { System.out.println("** Warning" + ", line " + err.getLineNumber() + ", uri " + err.getSystemId()); System.out.println(" " + err.getMessage()); } ); Document doc1 = docBuilder.parse(new File(argv[0])); Document doc2 = docBuilder.parse(new File(argv[1])); The new object to handle validation errors End of method call

Summer A-2000, Project Course-- Carnegie Mellon University 29 docBuilder.setErrorHandler( new org.xml.sax.ErrorHandler() { public void fatalError(SAXParseException e) throws SAXException {} public void error(SAXParseException e) throws SAXParseException { throw e; } public void warning(SAXParseException err) throws SAXParseException { System.out.println("** Warning" + ", line " + err.getLineNumber() + ", uri " + err.getSystemId()); System.out.println(" " + err.getMessage()); } ); Document doc1 = docBuilder.parse(new File(argv[0])); Document doc2 = docBuilder.parse(new File(argv[1])); Validation errors caught here.

Summer A-2000, Project Course-- Carnegie Mellon University 30 Lab Exercise: I would like everyone to be checked off for writing A short Java program that validates the agreement.xml file against the FixedFloatSwap.dtd. If, for example, the notional tag appears twice in the xml file your program should display “oops” before terminating.