Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples...

50
Lecture 4 a Interfaces (review of inheritance and abstract cl XML DOM a Examples ework 3
  • date post

    19-Dec-2015
  • Category

    Documents

  • view

    222
  • download

    1

Transcript of Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples...

Page 1: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3.

Lecture 4

• Java Interfaces (review of inheritance and abstract classes)

• The XML DOM

• Java Examples

• Homework 3

Page 2: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3.

Inheritance Review

IS-A versus IS-LIKE-A relationship

Shape

draw()

erase()

Circle

draw()

erase()

Shape

draw()

erase()

Circle

draw()

erase()

getRadius()

Page 3: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3.

void doStuff(Shape s){ Shape

s.erase();

… Circle Square Some class

s.draw(); not yet written

}

Circle c = new Circle();

doStuff (c)

Polymorphism via Dynamic Binding

The decision is made at run-time as to which function to call.

Page 4: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3.

BankAccount

CheckingAccount SavingsAccount SomeOtherAccount

Consider

Page 5: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3.

public class BankAccount {

public void deductFees() { …body… }

:

:

}

public class SavingsAccount extends BankAccount{

}

Writing the BankAccount class

But what should wewrite??

Page 6: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3.

Make the class Abstract

• Better

public abstract class BankAccount { public abstract void deductFees(); : : }

public class SavingsAccount extends BankAccount{ public abstract void deductFees() {…} }

No body..It’s up to thederived class to complete.

We can write this!

Page 7: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3.

Abstract Classes

• When you extend an existing class, you have a choice whether or not to redefine the methods of the superclass. If you don’t redefine the method, it will appear in the derived class as it appears in the superclass.

• Sometimes it is desirable to force programmers to redefine a method

• There may be no good (superclass) default for the method

• Only the subclass programmer can know how to implement the method

Page 8: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3.

Abstract Classes

• An abstract method has no implementation•You can’t construct objects of classes with abstract methods• You can only create objects of concrete classes• The class must be declared with the keyword abstract• It’s fine to have handles that reference abstract classes• Abstract classes, unlike interfaces, may have concrete methods and instance variables

Page 9: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3.

Another abstract base class

public abstract class Shape{

public abstract double area();public abstract double circumference();

}

Require computations only the derivedclass programmer knows.

Page 10: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3.

class Circle extends Shape{

public double area(){return PI*r*r;}

}

class Rectangle extends Shape{

}

Shape s = new Rectangle();

System.out.println(s.area());

Another abstract base class

Page 11: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3.

Interfaces

The interface keyword takes the concept of an abstract class one step further by preventing any function definitions at all!

Page 12: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3.

Interfaces

1. The core reason for interfaces is to be able to upcast to more than one base type. C++ multiple inheritance is not supported.

2. Usually better than abstract classes.3. Allows us to inherit a contract rather than an

implementation.4. When I implement an interface I make a promise that I will provide the required methods.

Page 13: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3.

INTERFACES

5. Have no instance variables6. Have only abstract methods (all parameters but no bodies)7. Have only public methods8. Are not classes…you can’t create interface objects9. May be referenced10. May contain constants11. Are implemented not extended as in inheritance

Page 14: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3.

INTERFACES

• Example

public interface Comparable { int compareTo(Object other); }

public class Student implements Comparable { // this class MUST define compareTo() }

Automatically public

Is there one correctway to write this?

Page 15: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3.

INTERFACES

public class Car implements Comparable { // this class MUST define compareTo() }

Page 16: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3.

INTERFACES

• Now, suppose we have a function sort

void sort(Comparable x[]) {

}

• Can we pass an array of Student objects to this function? • How about an array of Car?

Page 17: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3.

INTERFACES

void sort(Comparable x[]) {

}

• Can we pass an array of Student objects to this function?

Only if Student implements Comparable.

• How about an array of Car?

Only if Car implements Comparable.

Page 18: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3.

XML DOM

• The World Wide Web Consortium’s Document Object Model

•Provides a common vocabulary to use in manipulating XML documents.

• May be used from C, Java, Perl, Python, or VB

• Things may be quite different “under the hood”.

• The interface to the document will be the same.

Page 19: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3.

<?xml version = "1.0" ?><!DOCTYPE TopCat SYSTEM "cats.dtd"> <TopCat> I am The Cat in The Hat <LittleCatA> I am Little Cat A </LittleCatA> <LittleCatB> I am Little Cat B <LittleCatC> I am Little Cat C </LittleCatC> </LittleCatB> <LittleCatD/></TopCat>

The XML File “cats.xml”

Page 20: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3.

Little cat A

Little cat B

I am little cat B

topcat

I am the cat

in the hat

Little cat D

Little Cat C

I am littlecat C

I am littlecat A

document

XML doc

XML dec doctype element

text element element element

text text

text

element

DOM

Page 21: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3.

Agreement.xml

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE FixedFloatSwap SYSTEM "FixedFloatSwap.dtd"><FixedFloatSwap> <Notional>100</Notional> <Fixed_Rate>5</Fixed_Rate> <NumYears>3</NumYears> <NumPayments>6</NumPayments></FixedFloatSwap>

Page 22: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3.

document

XML doc

XML dec doctype

FixedFloatSwap

Notional

FixedRate NumYears NumPayments

All of these nodes implement the Node interface

Page 23: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3.

Some DOM Documentation from JavaSoft

Page 24: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3.

The Node Interface

• The Node interface is the primary datatype for the entire Document Object Model.

• It represents a single node in the document tree.

• While all objects implementing the Node interface expose methods for dealing with children, not all objects implementing the Node interface may have children.

• For example, Text nodes may not have children.

Page 25: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3.

Properties

• All Nodes have properties.

• Not all properties are need by all types of nodes.

• The attribute property is an important part of the Element node but is null for the Text nodes.

• We access the properties through methods…

Page 26: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3.

Some Methods of Node

Example Methods are:

String getNodeName() – depends on the Node type

if Element node return tag name

if Text node return #text

Page 27: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3.

Some Methods of Node

Example Methods are:

short getNodeType()

Might return a constant like ELEMENT_NODE or TEXT_NODE or …

Page 28: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3.

Some Methods of Node

Example Methods are:

String getNodeValue()

if the Node is an Element Node then return ‘null’

if the Node is a Text Node then return a String representing that text.

Page 29: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3.

Some Methods of Node

Example Methods are:

Node getParentNode()

returns a reference to the parent

Page 30: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3.

Some Methods of Node

Example Methods are:

public Node getFirstChild()

Returns the value of the firstChild property.

Page 31: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3.

Some Methods of Node

Example Methods are:

public NodeList getChildNodes()

returns a NodeList object

A NodeList object does not implement Node. Why not?

Page 32: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3.

The NodeList Interface

•The NodeList interface provides the abstraction of an ordered collection of nodes, without defining or constraining how this collection is implemented.

•The items in the NodeList are accessible via an integral index, starting from 0.

Page 33: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3.

There are only two methods of the NodeList Interface

public Node item(int index)

Returns the indexth item in the collection. If index is greater than or equal to the number of nodes in the list, this returns null.

Page 34: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3.

There are only two methods of the NodeList Interface

public int getLength()

Returns the value of the length property.

Page 35: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3.

The Element Interface

public interface Elementextends Node

• By far the vast majority of objects (apart from text) that authors encounter when traversing a document are Element nodes.

Inheritance

Nothing prevents us fromextending one interface inorder to create another.

Page 36: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3.

The Element Interface

public interface Elementextends Node

• Some methods in the Element interface

String getAttribute(String name)

Retrieves an attribute value by name.

Page 37: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3.

The Element Interface

public interface Elementextends Node

• Some methods in the Element interface

public String getTagName()

Returns the value of the tagName property.

Page 38: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3.

The Element Interface

public interface Elementextends Node

• Some methods in the Element interface

public NodeList getElementsByTagName(String name)

Returns a NodeList of all descendant elements with a given tag name, in the order in which they would be encountered in a preorder traversal of the Element tree..

Page 39: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3.

The CharacterData Interface

public interface CharacterDataextends Node

The CharacterData interface extends Node with a set of attributes and methods for accessing character data in the DOM. For clarity this set is defined here rather than on each object that usesthese attributes and methods. No DOM objects correspond directly to CharacterData, though Text and others do inherit the interface from it. All offsets in this interface start from 0.

Page 40: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3.

The CharacterData Interface

public interface CharacterDataextends Node

An example method:

public String getData()

Returns the value of the the character data of the node that implements this interface. The Text interface extends CharacterData.

Page 41: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3.

The Document Interface

public interface Documentextends Node

The Document interface represents the entire HTML or XML document. Conceptually, it is the root of the document tree, and provides the primary access to the document's data.

Page 42: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3.

The Document Interface

public interface Documentextends Node

Some methods:

public Element getDocumentElement()

Returns the value of the documentElement property. This is a convenience attribute that allows direct access to the child node that is the root element of the document. For HTML documents, this is the element with the tagName "HTML".

Page 43: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3.

The Document Interface

Some methods:

public NodeList getElementsByTagName(String tagname)

Returns a NodeList of all the Elements with a given tag name in the order in which the would be encountered in a preorder traversal of the Document tree. Parameters: tagname - The name of the tag to match on. The special value "*" matches all tags. Returns: A new NodeList object containing all the matched Elements.

Page 44: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3.

An Example

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;

Page 45: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3.

public class Simulator3 { public static void main(String argv[]) { Document doc; if(argv.length != 1 ) {

System.err.println("usage: java Simulator3 documentname"); System.exit(1);

} try { DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();

Page 46: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3.

doc = docBuilder.parse(new File(argv[0])); Element top = doc.getDocumentElement(); top.normalize();

NodeList elementList = top.getElementsByTagName("*"); int listLength = elementList.getLength();

for(int i = 0; i < listLength; i++) { Element e = (Element)elementList.item(i); System.out.print(e.getNodeName()); Text t = (Text)e.getFirstChild(); System.out.println(t.getNodeValue());

}

Page 47: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3.

} catch(SAXParseException err) { System.out.println("Parsing error" + ", line " + err.getLineNumber() + ", URI " + err.getSystemId()); System.out.println(" " + err.getMessage()); } catch(SAXException e) { Exception x = e.getException(); ((x == null) ? e : x).printStackTrace(); } catch (Throwable t) { t.printStackTrace(); } System.exit(0); }}

Page 48: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3.

Agreement.xml

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE FixedFloatSwap SYSTEM "FixedFloatSwap.dtd"><FixedFloatSwap> <Notional>100</Notional> <Fixed_Rate>5</Fixed_Rate> <NumYears>3</NumYears> <NumPayments>6</NumPayments></FixedFloatSwap>

Page 49: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3.

Output

Notional100Fixed_Rate5NumYears3NumPayments6

Page 50: Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3.

Homework 3

Rewrite the simulator (or modify the one that I wrote) for project 2 so that it reads the SwapAgreement.xml file using Java and the XML DOM