Abstract Data Types II.

Slides:



Advertisements
Similar presentations
Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
Advertisements

Java Programming Strings Chapter 7.
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
Mutability SWE 332 Fall 2011 Paul Ammann. SWE 3322 Data Abstraction Operation Categories Creators Create objects of a data abstraction Producers Create.
Programming 2 CS112- Lab 2 Java
Abstract Data Types II. 2 Sufficient operations Operations on an ADT are sufficient if they meet all the requirements They must be able to create all.
23-Jun-15 Strings, Etc. Part I: String s. 2 About Strings There is a special syntax for constructing strings: "Hello" Strings, unlike most other objects,
Abstract Data Types II. 2 Sufficient operations Operations on an ADT are sufficient if they meet all the requirements They must be able to create all.
Strings, Etc. Part I: Strings. About Strings There is a special syntax for constructing strings: "Hello" Strings, unlike most other objects, have a defined.
28-Jun-15 String and StringBuilder Part I: String.
Abstract Data Types II. Sufficient operations Operations on an ADT are sufficient if they meet all the requirements –They must be able to create all the.
Java Methods A & AB Chapter 10 - Strings. Ch 10 Goals Understand Strings indepth Learn strategies to deal with the immutability of Strings Learn how to.
23-Oct-15 Abstract Data Types. 2 Data types A data type is characterized by: a set of values a data representation, which is common to all these values,
Strings Carol Yarbrough AP Computer Science Instructor Alabama School of Fine Arts.
String Handling StringBuffer class character class StringTokenizer class.
Inheritance (Part 5) Odds and ends 1. Static Methods and Inheritance  there is a significant difference between calling a static method and calling a.
Strings Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and.
Object-Oriented Programming Chapter Chapter
Chapter 3 Introduction to Classes and Objects Definitions Examples.
ITIP © Ron Poet Lecture 14 1 Responsibilities. ITIP © Ron Poet Lecture 14 2 Two Sections of Code  If we ask two people to work together to do a job,
17-Feb-16 String and StringBuilder Part I: String.
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
Arrays. 2 The array data structure An array is an indexed sequence of components Typically, the array occupies sequential storage locations The length.
CPSC 252 ADTs and C++ Classes Page 1 Abstract data types (ADTs) An abstract data type is a user-defined data type that has: private data hidden inside.
David Evans CS201J: Engineering Software University of Virginia Computer Science Lecture 5: Implementing Data Abstractions.
Object Oriented Programming Some Interesting Genes.
Inner Classes.
Inner Classes 27-Dec-17.
Chapter 10 Thinking in Objects
Classes (Part 1) Lecture 3
Chapter 10 Thinking in Objects
Chapter 10 Thinking in Objects
Array, Strings and Vectors
EKT 472: Object Oriented Programming
Chapter 3: Using Methods, Classes, and Objects
String and String Buffers
String Handling in JAVA
Namespaces, Scopes, Access privileges
Graphs and Hypergraphs
Polymorphism 11-Nov-18.
Object Based Programming
String and StringBuilder
Polymorphism 15-Nov-18.
Chapter 7: Strings and Characters
Object Oriented Programming
Chapter 9 Thinking in Objects
Classes and Objects Encapsulation
String and StringBuilder
Polymorphism 28-Nov-18.
String and StringBuilder
Graph API (How I Did It) 1-Jan-19.
Java – String Handling.
Chapter 9 Thinking in Objects
Lecture 4: Data Abstraction CS201j: Engineering Software
16 Strings.
Abstract Data Types 24-Feb-19.
String and StringBuilder
Fundaments of Game Design
String.
Polymorphism 15-Apr-19.
Arrays.
Polymorphism 21-Apr-19.
CS2013 Lecture 7 John Hurley Cal State LA.
Abstract Data Types II.
Object Oriented Programming
CMPE212 – Reminders Assignment 2 due next Friday.
Object-Oriented Design AND CLASS PROPERTIES
Object-Oriented Design AND CLASS PROPERTIES
Inner Classes 25-Oct-19.
Presentation transcript:

Abstract Data Types II

Sufficient operations A set of operations on an ADT is sufficient if, together, they meet all the requirements They must be able to create all the values and perform all the operations required by the application Remember that the application cannot directly access the internal values They should be able to create all the values and perform all the operations required by any application in a given class of applications

Necessary operations An operation on an ADT is necessary if omitting it would fail to meet the requirements If the application can implement an operation easily and efficiently by combining other operations, that operation is unnecessary It’s OK to have unnecessary operations if they add significantly to the convenience of using the ADT

Necessary and sufficient operations Notice that “sufficient” applies to a set of operations, while “necessary” applies to individual operations in that set “Necessary” is relative to the set; an operation that is necessary in one set may not be necessary in another A necessary and sufficient set of operations is a set which is sufficient (meets all the requirements), but would not be sufficient if any one operation were omitted

Convenience operations An operation is a convenience operation if it could be accomplished by some overly complex combination of other operations Convenience operations should be justified Will it be used often? Does it really simplify the user’s task? Would the user expect this operation to be provided? Is it significantly more efficient?

Necessary and sufficient operations A class should define a necessary and sufficient set of operations Convenience operations should be justified Similarly, a class should have a necessary and sufficient data representation In general, a class should not contain data that can be easily computed from other data in the class

Example: Strings Necessary and sufficient operators: A constructor: public String(char[] chs) Ways to access data: public int length() public charAt(int index) Would you be happy with just these? If you invented the String class, could you justify operations such as equals and string concatenation? Convenience operators aren’t all bad!

Types of operations A constructor creates a legal value of the ADT (typically from input values) An accessor uses a value of the ADT to compute a value of some other type A transformer uses a value of the ADT to compute another value of the same ADT A mutative transformer changes the value of the ADT it is given An applicative transformer takes one value of an ADT and, without changing it, returns a new value of the same ADT

Requirements The constructors and transformers must together be able to create all legal values of the ADT A constructor or transformer should never create an illegal value It’s nice if the constructors alone can create all legal values, but sometimes this results in constructors with too many parameters for reasonable convenience The accessors must be able to extract any data needed by the application

Operations in Java Constructors can be implemented with Java constructors A constructor’s job is to construct an object of a class in a valid state That should be a constructor’s only job Accessors and transformers can be implemented with Java methods Mutative transformers are typically (but not always) implemented as void methods Sometimes they both modify an object and return it

Factory methods The problem with a constructor is that it will always construct an object of a given type This isn’t always what you want The constructor might be called with illegal values of the parameters You may wish to create unique instances of objects A common solution to these problems is to embed the call or calls to a private constructor inside a static “factory” method Example: public static Animal create(String voice) { if (voice.equals("woof")) return new Dog(); if (voice.equals("meow")) return new Cat(); if (voice.equals("moo")) return new Cow(); throw new IllegalArgumentException(voice); }

Factory methods II With a normal constructor, public class Add { public Add( ) { } } each call to new Add() creates another Add object These objects are not equal unless you define your own equals(Object o) method But with a factory method, public class Add { static Add add = new Add(); // done once when class is loaded private Add( ) { } // hide this constructor from the world! public static Add getAdd( ) { return add; } } You can only ever have one Add object Hence, the default equals method (same as ==) always works

Example: String Constructors: Accessors: "This is syntactic sugar for a constructor" public String(char[] chs) Accessors: public int length() public char charAt() Transformers (applicative only): public String substring(int i, int j) public String concat(String that) (also +) Etc.

Immutable objects A String is immutable: it cannot be changed The String class has no mutative transformers Operations such as string concatenation create new Strings Advantages: Efficient (for most uses) Easy to use and simple to understand (changing a String in one object doesn’t change it in other objects) Disadvantage: Every call to a transformer creates a new String

Example: StringBuffer Constructors: public StringBuffer(String s) Accessors: public int length() public char charAt() Transformers (applicative): none Transformers (mutative): public StringBuffer append(Object obj) Etc.

Mutable objects A StringBuffer is mutable: it can be changed The StringBuffer class has both applicative and mutative transformers Advantage: Efficient (for doing a lot of string manipulation) Disadvantage: Can be confusing (example coming up shortly) Operations on Strings are done by converting to StringBuffers, doing the work, and converting back

Safe use of Strings public class Person { private String name; Person(String name) { this.name = name; } } String jan = "Jan"; Person doctor = new Person(jan); String dan = "D" + jan.substring(1, 2); Person secretary = new Person(dan);

Unsafe use of StringBuffers public class Person { private StringBuffer name; Person(StringBuffer name) { this.name = name; } } StringBuffer buffer = new StringBuffer("Jan"); Person doctor = new Person(buffer); buffer.setCharAt(0, 'D'); Person secretary = new Person(buffer);

Summary A class should define a necessary and sufficient set of operations Convenience operations should be justified Operations can be classified as: Constructors Accessors Transformers (applicative or mutative) Immutable objects are often preferable to mutable objects

The End