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.

Slides:



Advertisements
Similar presentations
Intro to Scala Lists. Scala Lists are always immutable. This means that a list in Scala, once created, will remain the same.
Advertisements

The ArrayList Class and the enum Keyword
Purpose : To convert this string to a new character array. Return Type : char[ ] Parameters : none Declaration : public char[ ] toCharArray() Returns.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
Java Programming Strings Chapter 7.
Strings An extension of types A class that encompasses a character array and provides many useful behaviors Chapter 9 Strings are IMMUTABLE.
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.
Lecture 3: Topics If-then-else Operator precedence While loops Static methods Recursion.
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.
Chapter 9 Characters and Strings. Topics Character primitives Character Wrapper class More String Methods String Comparison String Buffer String Tokenizer.
Mutable, Immutable, and Cloneable Objects Chapter 15.
CSM-Java Programming-I Spring,2005 String Handling Lesson - 6.
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,
26-Jun-15 Abstract Data Types. 2 Data types I We type data--classify it into various categories-- such as int, boolean, String, Applet A data type represents.
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.
Arrays. 2 The array data structure An array is an indexed sequence of components Typically, the array occupies sequential storage locations The length.
13-Jul-15 Abstract Data Types. 2 Data types I We type data--classify it into various categories-- such as int, boolean, String, Applet A data type represents.
MIT AITI 2003 Lecture 7 Class and Object - Part I.
18-Aug-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming in Java Topic : Objects and Classes (cont)
5-1 Data types: values, operations, and data representation. Abstract data type: values and operations only. Requirements, contract, implementation(s).
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.
Primitive Types Java offers a number of primitive types eg.) int, short, long double, float char A variable which is declared as a primitive type stores.
1 Object Oriented Programming Lecture II Classes, Objects, Abstract Data Types, Representation Invariants, Mutables and Immutables.
Data Structures and Algorithms -- Chapter 3 Abstract Data Types Mohamed Mustaq.
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.
Design Patterns Gang Qian Department of Computer Science University of Central Oklahoma.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
String Handling StringBuffer class character class StringTokenizer class.
Strings JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin, and Skylight.
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.
Vladimir Misic: Characters and Strings1Tuesday, 9:39 AM Characters and Strings.
G51PR1 Introduction to Programming I University of Nottingham Unit 8 : Strings.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
Strings and Related Classes String and character processing Class java.lang.String Class java.lang.StringBuffer Class java.lang.Character Class java.util.StringTokenizer.
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
17-Feb-16 String and StringBuilder Part I: String.
Introduction to Objects and Encapsulation Computer Science 4 Mr. Gerb Reference: Objective: Understand Encapsulation and abstract data types.
Individual Testing, Big-O, C++ Bryce Boe 2013/07/16 CS24, Summer 2013 C.
Module 13: Properties and Indexers. Overview Using Properties Using Indexers.
Mutability SWE 332 Fall 2015 Paul Ammann. SWE 3322 Data Abstraction Operation Categories Creators Create objects of a data abstraction Producers Create.
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.
2 Arrays Array is a data structure that represents a collection of the same type of data. A "fixed length list".
Strings Chapter 7 CSCI CSCI 1302 – Strings2 Outline Introduction The String class –Constructing a String –Immutable and Canonical Strings –String.
Array, Strings and Vectors
Abstract Data Types II.
String and String Buffers
String and StringBuilder
Chapter 7: Strings and Characters
Object Oriented Programming
MSIS 655 Advanced Business Applications Programming
String and StringBuilder
Graph API (How I Did It) 1-Jan-19.
Java – String Handling.
Lecture 4: Data Abstraction CS201j: Engineering Software
Abstract Data Types 24-Feb-19.
String and StringBuilder
Arrays.
CS2013 Lecture 7 John Hurley Cal State LA.
Abstract Data Types II.
Object Oriented Programming
In Java, strings are objects that belong to class java.lang.String .
Basic Mutability Paul Ammann.
Presentation transcript:

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 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

3 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

4 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?

5 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

6 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!

7 Types of operations A constructor is creates a value of the ADT 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 computer another value of the ADT A mutative transformer changes the value of the ADT it is given An applicative transformer takes one ADT and, without changing it, returns a new ADT

8 Requirements The constructors and transformers must together be able to create all legal values of the ADT The accessors must be able to extract any data needed by the application

9 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

10 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 Another approach is to embed the call or calls to a constructor inside a “factory” method Example: public 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); }

11 Example: String Constructors: "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.

12 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

13 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.

14 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

15 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);

16 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);

17 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

18 The End