Expression Evaluation: Expression BinaryVariableValue Never need an instance of Expression.

Slides:



Advertisements
Similar presentations
Chapter 23 Organizing list implementations. This chapter discusses n The notion of an iterator. n The standard Java library interface Collection, and.
Advertisements

Sequence of characters Generalized form Expresses Pattern of strings in a Generalized notation.
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 A data structure is a collection of data organized in some fashion that permits access to individual elements stored in the structure This.
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Singleton vs utility class  at first glance, the singleton pattern does not seem to offer any advantages to using a utility class  i.e., a utility class.
Inheritance Inheritance Reserved word protected Reserved word super
Object Oriented Programming Chapter 7 Programming Languages by Ravi Sethi.
C8: Understanding Inheritance. Intuitive description Intuitive: FLORISTS are SHOPKEEPERS, inheriting various shopkeeper behaviors Tension in OOP languages:
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Chapter 7: Semantics Fall 2009 Marco.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
1 Java Object Model Part 2: the Object class. 2 Object class Superclass for all Java classes Any class without explicit extends clause is a direct subclass.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;
CS-2851 Dr. Mark L. Hornick 1 Tree Maps and Tree Sets The JCF Binary Tree classes.
(c) University of Washingtonhashing-1 CSC 143 Java Hashing Set Implementation via Hashing.
CSC 142 C 1 CSC 142 Object based programming in Java [Reading: chapter 4]
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. C H A P T E R S E V E N Object-Oriented Programming.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
Imperative Programming
CS2110: SW Development Methods Textbook readings: MSD, Chapter 8 (Sect. 8.1 and 8.2) But we won’t implement our own, so study the section on Java’s Map.
1 Review of Java Higher Level Language Concepts –Names and Reserved Words –Expressions and Precedence of Operators –Flow of Control – Selection –Flow of.
Topic 3 The Stack ADT.
Chapter 7 Java Server Pages. Objectives Explain how the separation of concerns principle applies to JSP Describe the operation and life-cycle of a JSP.
Java Tutorial. Object-Oriented Programming Concepts Object –a representation of some item state  fields/members and should be encapsulated behavior 
Generalized Containers CSIS 3701: Advanced Object Oriented Programming.
Lecture 8: Object-Oriented Design. 8-2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET Objectives “Good object-oriented programming is really.
Copyright © 2002, Systems and Computer Engineering, Carleton University Hashtable.ppt * Object-Oriented Software Development Unit 8.
Geoff Holmes Palindrome solutions Overview Arrays Collections Enumerators Vector BitSet Stack Dictionary Hashtable Collection Classes (Chapter 19) import.
These materials where developed by Martin Schray. Please feel free to use and modify them for non-commercial purposes. If you find them useful or would.
Java 5 Part 1 CSE301 University of Sunderland Harry Erwin, PhD.
1/20/03A2-1 CS494 Interfaces and Collection in Java.
P Chapter 2 introduces Object Oriented Programming. p OOP is a relatively new approach to programming which supports the creation of new data types and.
Semantics. Semantics is a precise definition of the meaning of a syntactically and type-wise correct program. Ideas of meaning: –Operational Semantics.
1 Collections Framework A collections framework is a unified architecture for representing and manipulating collections. All collections frameworks contain:
CS 61B Data Structures and Programming Methodology July 2, 2008 David Sun.
U n i v e r s i t y o f H a i l 1 ICS 202  2011 spring  Data Structures and Algorithms 
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Inheritance Type/Subtype Relationship. Inheritance Idea: An object B of one type, termed child class, inherits from another object A of another type,
Copyright (c) Systems and Computer Engineering, Carleton University * Object-Oriented Software Development Unit 13 The Collections Framework.
Introduction to Collections. Collections Collections provide a way of organizing related data in a model Different types of collections have different.
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.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
Design Patterns. Outline Purpose Purpose Useful Definitions Useful Definitions Pattern Overview Pattern Overview.
Data Structures Arrays and Lists Part 2 More List Operations.
C19: Collection Classes (don’t forget to look at all the online code examples)
The Object class Object package java.lang Object clone equals hashCode toString aCopy toThis hash string ! yesOrNo.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
Lecture 9:FXML and Useful Java Collections Michael Hsu CSULA.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
CSC 243 – Java Programming, Fall, 2008 Tuesday, September 30, end of week 5, Interfaces, Derived Classes, and Abstract Classes.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 8 Semantic Interpretation To understand a.
Catalog of Refactoring
Using the Java Collection Libraries COMP 103 # T2
JAVA MULTIPLE CHOICE QUESTION.
7.1 What Is An Object Object-oriented program - Description or simulation of application Object-oriented programming is done by adopting or extending an.
03/10/14 Inheritance-2.
Chapter 3: Using Methods, Classes, and Objects
Object Oriented Programming in Java
Programming Languages 2nd edition Tucker and Noonan
OO Design with Inheritance
COMPUTER 2430 Object Oriented Programming and Data Structures I
Protocols CS 4311 Wirfs Brock et al., Designing Object-Oriented Software, Prentice Hall, (Chapter 8) Meyer, B., Applying design by contract, Computer,
Assertions References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 4/25/2019.
Review: libraries and packages
Software Design Lecture : 39.
Presentation transcript:

Expression Evaluation: Expression BinaryVariableValue Never need an instance of Expression

Expression Evaluation: Abstract syntax definition: Expression = Variable | Value | Binary Variable = String id Value = int intValue | boolean boolValue Binary = Operator op; Expression term1, term2 Operator = BooleanOp | RelationalOp | ArithmeticOp Italics: type name; Non-italics: member name Review Section 2.3

Abstract Syntax  Class Hierarchy Expression Variable Value Binary IntValue BoolValue UndefValue Instead of primitives

Abstract Syntax  Class Hierarchy (2) Operator ArithmeticOp RelationalOp BooleanOp AddOp SubOp MultOp DivOp LessOp EqualOp GreaterOp... AndOp OrOp

Evaluation: M: Expression x ∑  Value M(Expression e, State σ) e.M(σ) = e (this) if e is a Value σ(e) if e is a Variable ApplyBinary(e.op, M(e.term1, σ), M(e.term2, σ)) if e is a Binary ApplyUnary(e.op, M(e.term1, σ)) if e is a Unary

What “state” means: By State we mean the set of all instance variable values. By σ(e) we mean the value of an Expression, e, given its current data state as σ. Example: e is the Expression x + 4 current value of x is 3 (σ(e)) current value of e is 7 M(e,σ(e)) Meaning of e

Expression.java public abstract class Expression { public abstract Value M(State sigma); } M (meaning) == evaluate

Command Pattern: Abstract (italics)

Motivation: GUI needs behaviour without knowing what kind. Command turns a request for behaviour into an object Concrete subclasses specify receiver-action pairs. Binary encapsulates a receiver (op) and an action (apply())

Variable.java public class Variable extends Expression { // Variable = String id String id; public boolean equals (Object obj) { String s = ((Variable) obj).id; return id.equalsIgnoreCase(s); // case-insensitive identifiers } public int hashCode ( ) { return id.hashCode( ); } public Value M(State sigma) { return (Value)(sigma.get(this)); }

Value.java (1) public class Value extends Expression { // Value = int intValue | boolean boolValue | Undefined static public Value create(int i ) { return new IntValue(i); } static public Value create(boolean b ) { return new BooleanValue(b); } static public Value create( ) { return new UndefValue(); } // only UndefValue class needs to override this public boolean isUndef() { return false; } public int intVal() { return -1; } // allows override public boolean boolVal() { return false; } // allows override public Value M(State sigma) { return this; }... }

Value.java (2) public class Value extends Expression { // Value = int intValue | boolean boolValue | Undefined... class IntValue extends Value { int intVal; public IntValue(int i) { intVal = i; } public int intVal() { return intVal; } } class BooleanValue extends Value { boolean boolVal; public BooleanValue(boolean b) { boolVal = b; } public boolean boolVal() { return boolVal; } } class UndefValue extends Value { public UndefValue() { } public boolean isUndef() { return true; } } In Chapter 3, Value had a Type instance variable. This is ad hoc polymorphism. Now we create the actual subclasses

Factory Pattern: Value is an example of a Factory Pattern. Factory patterns can be used in following cases: 1.When a class does not know which class of objects it must create. 2. A class specifies its sub-classes to specify which objects to create. 3. In programmer’s language (very raw form), you can use factory pattern where you have to create an object of any one of a number of sub-classes of some class depending on the data provided. NOTE: Patterns are archetypes of typical programming constructs.There is a whole study of what these archetypes are and how they should be used. eg:

Weird syntax: The book code for Value is “weird” and invalid. But this works strangely enough public Value extends Expression { public Value(int n) { return new IntValue(n);}... } public Value extends Expression { public Value Value(int n) {return new IntValue(n);}... } // client Value x = Value(3); // the actual type of x is IntValue

Binary.java public class Binary extends Expression { // Binary = Operator op; Expression term1, term2 Operator op; Expression term1, term2; public Binary(Operator o, Expression t1, Expression t2) { op = o; term1 = t1; term2 = t2; } public Value M(State sigma) { return op.apply(term1.M(sigma), term2.M(sigma)); } Only the Operator class needs be public

Operator.java public abstract class Operator { public abstract Value apply(Value v1, Value v2); } abstract class ArithmeticOp extends Operator { } abstract class RelationalOp extends Operator { } abstract class BooleanOp extends Operator { } NOTE: In a full implementation of Jay, we need to do type checking and the above three abstract classes help in this. For our purposes they don’t hurt so leave them there.

AddOp.java class AddOp extends ArithmeticOp { public Value apply(Value v1, Value v2) { if (v1.isUndef() || v2.isUndef()) return new Value(); return new Value(v1.intVal() + v2.intVal()); }

State.java // State.java import java.util.*; public class State { private Hashtable contents = null; public State () { contents = new Hashtable (); } public Enumeration keys() { return contents.keys(); } public Value get(Variable v) { return contents.get(v); } public State (Variable key, Value value) { if ( contents == null ) contents = new Hashtable (); contents.put(key,value); } public State onion( State t) { // union for ( Enumeration e = t.keys(); e.hasMoreElements(); ) { Variable v = e.nextElement(); contents.put(v, t.get(v)); } return this; }

State.java (2) Text implementation uses inheritance after complaining about Vector  Stack Onion(): This is really just union. Any ideas? Hashtable.put(k,v) replaces any existing value if an attempt is made put a duplicate key.

Concordance: Roses are red Violets are blue Sugar is sweet So are you Are: 1, 2, 4 Blue: 2 Is: 3 Red: 1 Roses: 1 So: 4 Sugar: 3 Sweet: 3 Violets: 2 You: 4 Blue: 2 Red: 1 Roses: 1 So: 4 Sugar: 3 Sweet: 3 Violets: 2 You: 4 Without nonsense words

Concordance Uses: Searching large documents like Bible Variants include indexes, no nonsense concordance Cross-reference listing of program: what functions use what variables and call what other funtions

OOD Methodology(?): List all nouns (sounds like ER) found is problem description Concordance Words (String) Line numbers (int) Document Report

ULM Diagram:

Concordance.java public class Concordance { private Dictionary > dict = new Hashtable > (); private boolean allowDupl = true; public Concordance (boolean allowDupl ) { this.allowDupl = allowDupl; } public Concordance ( ) { this(true); } public void add (String word, Integer line) { Vector set = dict.get(word); if (set == null) // word not in Concordance { set = new Vector ( ); dict.put(word, set); } if (allowDupl || !set.contains(line)) set.addElement(line); } public Enumeration keys( ) { return dict.keys( ); } public Enumeration > elements (String word) { return (dict.get(word)).elements( ); } } Sets exist in Java but not ones that allow for duplicate entries.

Document.java public class Document { public Document (BufferedReader input, boolean sensitive, Concordance dict) throws IOExecption { String line; for (int number = 1; (line = input.readLine())!=null; number++) { if (!sensitive) line = line.toLowerCase( ); Enumeration e = new StringTokenizer(line, " \t\n.,!?;:()[]{}"); while (e.hasMoreElements( )) dict.add(e.nextElement( ), new Integer(number)); }

Report.java public class Report { public Report (Concordance dict, PrintStream out) { Enumeration e = dict.keys( ); while (e.hasMoreElements( )) { String word = e.nextElement( ); out.print(word + ":"); Enumeration > f = dict.elements(word); while (f.hasMoreElements( )) out.print(" " + f.nextElement( )); out.println( ); } Exercise: Add code to the above so that an HTML document is produced.

Things learned: A good library is a good thing. Enumerators have numerable uses