Cs2220: Engineering Software Class 10: Generic Datatypes Fall 2010 University of Virginia David Evans.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

Sets and Maps Part of the Collections Framework. 2 The Set interface A Set is unordered and has no duplicates Operations are exactly those for Collection.
DATA STRUCTURES Lecture: Interfaces Slides adapted from Prof. Steven Roehrig.
Java Programming Abstract classes and Interfaces.
Interfaces CSC 171 FALL 2004 LECTURE 14. Project 1 review public class Rational { private int numerator, denominator; public Rational(int numerator, int.
If (justMetYou) { crazy = true; cout
Slides adapted from Alex Mariakakis, with material from Krysta Yousoufian, Mike Ernst, and Kellen Donohue Section 4: Graphs and Testing.
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
David Evans CS201J: Engineering Software University of Virginia Computer Science Lecture 6: Reasoning about Data Abstractions.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
CSE 115 Week 4 February 4 - 8, Monday Announcements Software installation fest Tuesday & Wednesday 4-7 in Baldy 21. Software installation fest Tuesday.
Building Java Programs Inner classes, generics, abstract classes reading: 9.6, 15.4,
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Cs205: engineering software university of virginia fall 2006 Semantics and Specifying Procedures David Evans
Sets and Maps Part of the Collections Framework. The Set interface A Set is unordered and has no duplicates Operations are exactly those for Collection.
Cs2220: Engineering Software Class 11: Subtyping and Inheritance Fall 2010 University of Virginia David Evans.
Cs2220: Engineering Software Class 8: Implementing Data Abstractions Fall 2010 University of Virginia David Evans.
Cs205: engineering software university of virginia fall 2006 Data Abstraction David Evans
CSE 143 Lecture 20 Binary Search Trees continued; Tree Sets read slides created by Marty Stepp and Hélène Martin
CS 106 Introduction to Computer Science I 04 / 20 / 2007 Instructor: Michael Eckmann.
Class 14: Object-Oriented Programming Fall 2010 University of Virginia David Evans cs2220: Engineering Software.
Some Other Collections: Bags, Sets, Queues and Maps COMP T2 Lecture 4 School of Engineering and Computer Science, Victoria University of Wellington.
CPSC 102: Computer Science II Dr. Roy P. Pargas 408 Edwards Hall Office Hours 10:00-11:00 am MWF 2:00-3:00 pm TTh.
CS 206 Introduction to Computer Science II 09 / 10 / 2009 Instructor: Michael Eckmann.
Cs2220: Engineering Software Class 6: Defensive Programming Fall 2010 University of Virginia David Evans.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 12: Subtyping Rules What’s the.
CS 61B Data Structures and Programming Methodology July 3, 2008 David Sun.
Cs205: engineering software university of virginia fall 2006 David Evans Substitution Principle.
Data Abstractions EECE 310: Software Engineering.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 14: Substitution Principle.
Polymorphism Liskov 8. Outline equals() Revisiting Liskov’s mutable vs. not rule Polymorphism Uniform methods for different types “easy” polymorphism.
Cs205: engineering software university of virginia fall 2006 Subtyping and Inheritance David Evans Quiz Friday: classes through.
Sets and Maps Sets Maps The Comparator Interface Sets and Maps in Java Collections API – TreeSet – TreeMap Review for Exam Reading:
Polymorphism (generics) CSE 331 University of Washington.
COMP 121 Week 8: Generic Collections. Objectives To understand type variables and how they are used in generic programming To be able to implement and.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
PROGRAMMING PRE- AND POSTCONDITIONS, INVARIANTS AND METHOD CONTRACTS B MODULE 2: SOFTWARE SYSTEMS 13 NOVEMBER 2013.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 9: Designing Exceptionally.
Polymorphism SWE 619. Outline equals() Revisiting Liskov’s mutable vs. not rule Polymorphism Uniform methods for different types “easy” polymorphism Element.
Sets and Maps Part of the Collections Framework. 2 The Set interface A Set is unordered and has no duplicates Operations are exactly those for Collection.
Written by: Dr. JJ Shepherd
Iteration Abstraction SWE Software Construction Fall 2009.
Cs2220: Engineering Software Class 12: Substitution Principle Fall 2010 University of Virginia David Evans.
It is better to have 100 functions operate on one data structure than 10 functions on 10 data structures. Alan Perlis Lecture 15: Data Abstraction.
Zach Tatlock / Winter 2016 CSE 331 Software Design and Implementation Lecture 13 Generics 1.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
CS 367 Introduction to Data Structures Charles N. Fischer Fall s367.html.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 10: Programming Exceptionally.
Collections Dwight Deugo Nesa Matic
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
Unified Modeling Language (UML)
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 11: Subtyping and Inheritance.
Cs205: engineering software university of virginia fall 2006 Programming Exceptionally David Evans
Winter 2006CISC121 - Prof. McLeod1 Stuff We had better discuss a midterm date… –27 Feb. to 3 March or –6 to 10 March.
David Evans CS201J: Engineering Software University of Virginia Computer Science Lecture 7: A Tale of Two Graphs (and.
David Evans CS201J: Engineering Software University of Virginia Computer Science Lecture 5: Implementing Data Abstractions.
Zach Tatlock / Winter 2016 CSE 331 Software Design and Implementation Lecture 6 Representation Invariants.
Exceptions throw new RuntimeException(“bad things happened”); The above line is the simplest possible way of throwing an exception. In Java, exceptions.
EECE 310: Software Engineering
EECE 310: Software Engineering
Subtyping Rules David Evans cs205: engineering software BlackBear
Lecture 9: Exceptions in Java CS201j: Engineering Software
Lecture 5: Code Red, Ariane 5, and Gambling
Data Abstraction David Evans cs205: engineering software
Lecture 7: A Tale of Two Graphs CS201j: Engineering Software
Lecture 4: Data Abstraction CS201j: Engineering Software
Lecture 13: Subtyping Rules Killer Bear Climber
Reasoning about Data Abstractions
Presentation transcript:

cs2220: Engineering Software Class 10: Generic Datatypes Fall 2010 University of Virginia David Evans

Exam 1 Out at end of class today, due Tuesday at beginning of class – I will answer questions about PS3 (including anything in PS3 comments) while the exam is out (including Monday’s office hours) Work alone Open resources: use any resources you want, but cite anything that is not part of the course materials PS3: Available for pick-up by Monday.

Reality Check Writing abstraction functions, rep invariants, testing code thoroughly, reasoning about correctness, etc. for a big program is a ridiculous amount of work! Does anyone really do this? – Yes (and a lot more), but usually only when its really important to get things right: Cost per line of code: – Small, unimportant projects: $1-5/line – WindowsNT: about $100/line – FAA’s Automation System ( ): $900/line

StringGraph, IntegerGraph, etc. public class StringGraph OVERVIEW: A StringGraph is a directed graph where V is a set of Strings, and E is a set of edges. Each edge is a pair (v1, v2), representing an edge from v1 to v2 in G. A typical StringGraph is where each ai and bi is in [1, n]. public void addNode(String s) throws DuplicateException; public void addEdge(String s, String t) throws NoNodeException, DuplicateException … public class StringGraph OVERVIEW: A StringGraph is a directed graph where V is a set of Strings, and E is a set of edges. Each edge is a pair (v1, v2), representing an edge from v1 to v2 in G. A typical StringGraph is where each ai and bi is in [1, n]. public void addNode(String s) throws DuplicateException; public void addEdge(String s, String t) throws NoNodeException, DuplicateException … public class IntegerGraph OVERVIEW: A IntegerGraph is a directed graph where V is a set of ints, and E is a set of edges. Each edge is a pair (v1, v2), representing an edge from v1 to v2 in G. A typical IntegerGraph is where each ai and bi is in [1, n]. public void addNode(int s) throws DuplicateException; public void addEdge(int s, int t) throws NoNodeException, DuplicateException … public class IntegerGraph OVERVIEW: A IntegerGraph is a directed graph where V is a set of ints, and E is a set of edges. Each edge is a pair (v1, v2), representing an edge from v1 to v2 in G. A typical IntegerGraph is where each ai and bi is in [1, n]. public void addNode(int s) throws DuplicateException; public void addEdge(int s, int t) throws NoNodeException, DuplicateException … public class CityGraph OVERVIEW: A CityGraph is a directed graph where V is a set of City objects, and E is a set of edges. Each edge is a pair (v1, v2), representing an edge from v1 to v2 in G. A typical IntegerGraph is where each ai and bi is in [1, n]. public void addNode(City s) throws DuplicateException; public void addEdge(City s, City t) throws NoNodeException, DuplicateException … public class CityGraph OVERVIEW: A CityGraph is a directed graph where V is a set of City objects, and E is a set of edges. Each edge is a pair (v1, v2), representing an edge from v1 to v2 in G. A typical IntegerGraph is where each ai and bi is in [1, n]. public void addNode(City s) throws DuplicateException; public void addEdge(City s, City t) throws NoNodeException, DuplicateException … public class URLGraph OVERVIEW: A URLGraph is a directed graph where V is a set of URL objects, and E is a set of edges. Each edge is a pair (v1, v2), representing an edge from v1 to v2 in G. A typical IntegerGraph is where each ai and bi is in [1, n]. public void addNode(URL s) throws DuplicateException; public void addEdge(URL s, URL t) throws NoNodeException, DuplicateException … public class URLGraph OVERVIEW: A URLGraph is a directed graph where V is a set of URL objects, and E is a set of edges. Each edge is a pair (v1, v2), representing an edge from v1 to v2 in G. A typical IntegerGraph is where each ai and bi is in [1, n]. public void addNode(URL s) throws DuplicateException; public void addEdge(URL s, URL t) throws NoNodeException, DuplicateException …

Type Parameters We want to have Graphs with many different types of nodes How should we declare the Graph methods? public void addNode(? s) public void addEdge(? s, ? t) public Set getAdjacent() We don’t want just one Graph datatype. We want different Graphs for different node types. We don’t want just one Graph datatype. We want different Graphs for different node types.

Generic Datatype public class Graph {... public void addNode(T s) … public void addEdge(T s, T t) … public Set getAdjacent() …... } Note: Java did not support generic datatypes until version 1.5 (this is why the book doesn’t use them) public class StringGraph {... public void addNode(String s) … public void addEdge(String s, String t) … public Set getAdjacent() …... }

Specifying the Generic Graph public class Graph OVERVIEW: A Graph is a directed graph where V is a set of T objects, and E is a set of edges. Each edge is a pair (v1, v2), representing an edge from v1 to v2 in G. A typical Graph is where each ai and bi is in [1, n]. public void addNode(T s) throws DuplicateException // MODIFIES: this // EFFECTS: public class StringGraph public void addNode(String s) throws DuplicateException MODIFIES: this EFFECTS: If s is the name of a node in this, throws DuplicateException. Otherwise, adds s to the nodes in this, with no adjacent nodes: G post = public class StringGraph public void addNode(String s) throws DuplicateException MODIFIES: this EFFECTS: If s is the name of a node in this, throws DuplicateException. Otherwise, adds s to the nodes in this, with no adjacent nodes: G post =

Implementing a Generic Graph // Rep C public class StringGraph { class NodeRecord implements Comparable { String key; Set values; … } private Set rep; // Rep C public class StringGraph { class NodeRecord implements Comparable { String key; Set values; … } private Set rep; public class Graph > { class NodeRecord implements Comparable { T key; Set values; … } private Set rep; public class Graph > { class NodeRecord implements Comparable { T key; Set values; … } private Set rep;

NodeRecord (for StringGraph) class NodeRecord implements Comparable { String key; Set values; NodeRecord (String p_key) { key = p_key; values = new TreeSet (); } public int compareTo(NodeRecord n) { // This assertion should be guaranteed by the rep invariant assert (!key.equals(n.key) || (n.values == values)); return key.compareTo(n.key); } public boolean matches(String p_key) { return key.equals(p_key); } public void addEdge(String p_edge) throws DuplicateException { … } } Its okay that the rep isn’t private, since this is an inner class: defined inside StringGraph implementation, not visible to clients. Its okay that the rep isn’t private, since this is an inner class: defined inside StringGraph implementation, not visible to clients. We need to implement compareTo to satisfy the Comparable interface, required by TreeSet.

Generic NodeRecord class NodeRecord implements Comparable { T key; Set values; NodeRecord (T p_key) { key = p_key; values = new TreeSet (); } public int compareTo(NodeRecord n) { // This assertion should be guaranteed by the rep invariant assert (!key.equals(n.key) || (n.values == values)); return key.compareTo(n.key); } public boolean matches(T p_key) { return key.equals(p_key); } public void addEdge(T p_edge) throws DuplicateException { if (values.contains(p_edge)) throw new DuplicateException(" "); values.add(p_edge); } Abstraction Function: Nodes = { el.key | el is an element in rep } Edges = { ( el.key, value ) | el is an element in rep and value is an element in el.values } Rep Invariant: rep != null elements of rep are not null forall elements e in rep: forall elements f in e.values: f = el.key for some element el in rep Abstraction Function: Nodes = { el.key | el is an element in rep } Edges = { ( el.key, value ) | el is an element in rep and value is an element in el.values } Rep Invariant: rep != null elements of rep are not null forall elements e in rep: forall elements f in e.values: f = el.key for some element el in rep

public class Graph > { class NodeRecord implements Comparable { … } private Set rep; public Graph() { rep = new TreeSet (); } private NodeRecord getNode(T s) throws NoNodeException { // EFFECTS: If s is a node in this returns the corresponding NodeRecord. If not, throws NoNodeException. for (NodeRecord r : rep) { if (r.matches(s)) { return r; } throw new NoNodeException(s.toString()); } public void addNode(T s) throws DuplicateException { try { getNode(s); throw new DuplicateException(s.toString()); } catch (NoNodeException e) { // No exception means its not a duplicate rep.add(new NodeRecord(s)); }

public void addEdge(T s, T t) throws NoNodeException, DuplicateException { NodeRecord srec = getNode(s); getNode(t); // only to get exception if t doesn't exist srec.addEdge(t); // DuplicateException will be rethrown } public Set getAdjacent(T s) throws NoNodeException { return new TreeSet (getNode(s).values); } Why do we need to copy values?

public String toString() { String res = "<"; boolean firstone = true; for (NodeRecord r : rep) { if (firstone) { firstone = false; } else { res += ", “; } res += r.key; } res += "; { "; firstone = true; for (NodeRecord r : rep) { for (T e : r.values) { if (firstone) { firstone = false; } else { res += ", “; } res += "(" + r.key + ", " + e + ")"; } res += " } >"; return res; } Abstraction Function: Nodes = { el.key | el is an element in rep } Edges = { ( el.key, value ) | el is an element in rep and value is an element in el.values } Abstraction Function: Nodes = { el.key | el is an element in rep } Edges = { ( el.key, value ) | el is an element in rep and value is an element in el.values }

Charge Exam 1 is due Tuesday Next week: Subtyping PS4: Designing with Data Abstractions and Subtyping