CMPSCI 187: Programming With Data Structures. Lecture 6: The StringLog ADT David Mix Barrington 17 September 2012

Similar documents
CMPSCI 187: Programming With Data Structures. Lecture #24: Files and a Case Study David Mix Barrington 2 November 2012

CMPSCI 187: Programming With Data Structures. Lecture #23: Linked Lists David Mix Barrington 31 October 2012

CMPSCI 187: Programming With Data Structures. Lecture #11: Implementing Stacks With Arrays David Mix Barrington 28 September 2012

CMPSCI 187: Programming With Data Structures. Lecture #21: Array-Based Lists David Mix Barrington 26 October 2012

CMPSCI 187: Programming With Data Structures. Lecture #15: Thinking Recursively David Mix Barrington 10 October 2012

CS-202 Introduction to Object Oriented Programming

CMPSCI 187: Programming With Data Structures. Lecture #31: Graphs David Mix Barrington 26 November 2012

Chapter 4 Defining Classes I

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed

CS112 Lecture: Defining Instantiable Classes

Java Object Oriented Design. CSC207 Fall 2014

COMP 110/L Lecture 20. Kyle Dewey

Basic Object-Oriented Concepts. 5-Oct-17

Lab 4 Due April 18 th

1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides

CMPSCI 187: Programming With Data Structures. Lecture #16: Thinking About Recursion David Mix Barrington 12 October 2012

CMPSCI 250: Introduction to Computation. Lecture 20: Deterministic and Nondeterministic Finite Automata David Mix Barrington 16 April 2013

CISC-124. Dog.java looks like this. I have added some explanatory comments in the code, and more explanation after the code listing.

Week 3 Classes and Objects

Inheritance. Transitivity

Data Structures and Algorithms Design Goals Implementation Goals Design Principles Design Techniques. Version 03.s 2-1

16 Multiple Inheritance and Extending ADTs

ITI Introduction to Computing II

CMPSCI 187: Programming With Data Structures. Review for First Midterm 9 October 2011

Programs as Models. Procedural Paradigm. Class Methods. CS256 Computer Science I Kevin Sahr, PhD. Lecture 11: Objects

CS1004: Intro to CS in Java, Spring 2005

CS201 - Assignment 3, Part 2 Due: Wednesday March 5, at the beginning of class

Lecture 21: The Many Hats of Scala: OOP 10:00 AM, Mar 14, 2018

ITI Introduction to Computing II

CMPSCI 187: Programming With Data Structures. Lecture 12: Implementing Stacks With Linked Lists 5 October 2011

CmpSci 187: Programming with Data Structures Spring 2015

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

Java Review. Fundamentals of Computer Science

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

CS/ENGRD 2110 SPRING Lecture 7: Interfaces and Abstract Classes

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

Programming in the large

CMPSCI 187: Programming With Data Structures. Lecture #20: Concurrency and a Case Study David Mix Barrington 24 October 2012

Recommended Group Brainstorm (NO computers during this time)

Array Based Lists. Collections

Ticket Machine Project(s)

Download FirstOODesignPractice from SVN. A Software Engineering Technique: (Class Diagrams)

CSE 131 Introduction to Computer Science Fall Exam II

CS112 Lecture: Defining Classes. 1. To describe the process of defining an instantiable class

ITI Introduction to Computing II

Object Oriented Programming. Java-Lecture 11 Polymorphism

Chapter 7 Classes & Objects, Part B

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

Notes on Chapter Three

Lecture 8: Iterators and More Mutation

Lecture 7 Objects and Classes

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

Adam Blank Lecture 3 Autumn 2016 CSE 143. Computer Programming II

CSE 143. More ArrayIntList; pre/post; exceptions; debugging. Computer Programming II. CSE 143: Computer Programming II

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

CSE 331 Software Design & Implementation

CMPSCI 187: Programming With Data Structures. Lecture #34: Efficient Sorting Algorithms David Mix Barrington 3 December 2012

CS/ENGRD 2110 FALL Lecture 7: Interfaces and Abstract Classes

Lecture 36: Cloning. Last time: Today: 1. Object 2. Polymorphism and abstract methods 3. Upcasting / downcasting

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2)

CmpSci 187: Programming with Data Structures Spring 2015

CMPSCI 187: Programming With Data Structures. Review for Final Exam David Mix Barrington 10 December 2012

1: Introduction to Object (1)

Recall. Key terms. Review. Encapsulation (by getters, setters, properties) OOP Features. CSC148 Intro. to Computer Science

Principles of Object Oriented Programming. Lecture 4

CmpSci 187: Programming with Data Structures Spring 2015

CMPSCI 250: Introduction to Computation. Lecture #28: Regular Expressions and Languages David Mix Barrington 2 April 2014

Solutions to Quiz 1 (March 14, 2016)

Chapter 10 Classes Continued. Fundamentals of Java

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE. What is Inheritance?

CS-XXX: Graduate Programming Languages. Lecture 22 Class-Based Object-Oriented Programming. Dan Grossman 2012

Enums. In this article from my free Java 8 course, I will talk about the enum. Enums are constant values that can never be changed.

Classes, interfaces, & documentation. Review of basic building blocks

More on inheritance CSCI 136: Fundamentals of Computer Science II Keith Vertanen Copyright 2014

Inheritance, Polymorphism, and Interfaces

PRINCIPLES OF SOFTWARE BIM209DESIGN AND DEVELOPMENT 10. PUTTING IT ALL TOGETHER. Are we there yet?

EINDHOVEN UNIVERSITY OF TECHNOLOGY

Inheritance, and Polymorphism.

Encapsulation. Administrative Stuff. September 12, Writing Classes. Quick review of last lecture. Classes. Classes and Objects

CS313D: ADVANCED PROGRAMMING LANGUAGE

Inheritance and Polymorphism

Instance Members and Static Members

CS103 Handout 29 Winter 2018 February 9, 2018 Inductive Proofwriting Checklist

CS/ENGRD 2110 SPRING Lecture 5: Local vars; Inside-out rule; constructors

2. [20] Suppose we start declaring a Rectangle class as follows:

CS11 Introduction to C++ Fall Lecture 7

Chapter 4: Writing Classes

Software Design and Analysis for Engineers

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

Finally, all fields are public by default, so you can easily write simple functions, such as this one, without writing getters:

Outline. Computer Science 331. Information Hiding. What This Lecture is About. Data Structures, Abstract Data Types, and Their Implementations

COMP 250 Winter 2011 Reading: Java background January 5, 2011

CMPSCI 187: Programming With Data Structures. Lecture 5: Analysis of Algorithms Overview 16 September 2011

CISC 1600 Lecture 3.1 Introduction to Processing

CMPSCI 187 / Spring 2015 Hangman

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

Java Libraries. Lecture 6 CGS 3416 Fall September 21, 2015

Transcription:

CMPSCI 187: Programming With Data Structures Lecture 6: The StringLog ADT David Mix Barrington 17 September 2012

The StringLog ADT Data Abstraction Three Views of Data Java Interfaces Defining the StringLog ADT StringLog s methods: Constructors, Transformers, and Observers Code for the StringLog interface

Data Abstraction What is a piece of data, really? Computer scientists are practical people. For us, the essence of a piece of data is what you can do with it. Different actors should be authorized to do different things with a given piece of data. We want to hide information about the data from actors that don t need it. This isn t necessarily a matter of security -- in order for an object to do its job in the best way, we want a description of that job that is as simple as possible. Then we can make use of any flexibility that is available. An important general principle of design here is modularity. Different elements of our software should have clearly defined responsibilities, and the interactions among those elements need to be clearly specified. The start of a class definition is the definition of an abstract data type or ADT, which is a list and specifation of the desired operations for the objects.

Three Views of Data DJW talk about three separate levels at which we view an ADT (and thus view any class created to implement the ADT): At the Application Level, we view the ADT as a client what services does it offer us, and how to we access those services. Every predefined class in Java is described in the application programming interface or API, which is your primary source of information about it as a client. At the Abstract Level, we describe what data the object holds, and what can be done with it, less specifically than in an API, but still independently of any implementation. Finally, at the Implementation Level we consider how lower-level programming constructs are actually used to make the object do what it does.

Java Interfaces We often want to assume that an object can be used in a certain way, without having to know exactly what class that object is from. We ve seen how we might, for example, put a Terrier object into a Dog variable, and use it as a Dog. Here Dog is a superclass of Terrier. An interface in Java is in effect a superclass that is not an actual class for which you may create objects. DJW s example on pages 69-72 is of a FigureGeometry interface for geometrical figures. It defines operations that we might want to carry out for circles or squares or triangles. (They give a Circle class that implements this interface.) If a variable s type is FigureGeometry, an object of any implementing type may go in that variable. But there is no such thing as a FigureGeometry object.

Defining the StringLog ADT So at the abstract level, what is a StringLog? We want it to be a collection of strings, with a single string that is the name of the collection. We want to be able to add new strings to the collection, determine whether a particular string is already there, and print out the entire collection of strings. We ll use this class later in implementing a trivia game. There are a few questions that are not answered by this simple description. Does a StringLog have a maximum capacity? (Any data structure on any fixed computer has a maximum capacity because the computer s memory is finite, but are there to be practical limits on the size?) Some concrete data structures can resize themselves as needed, and linked structures have no fixed size. This description tells us what methods we will need. Note that it does not tell us what data fields we need, only generally what we need to store. For example, we might or might not have an instance variable for the size.

Constructors, Transformers, and Observers In general, we can group the instance methods of a class into three categories. We need one or more constructors to create new objects, transformers to alter the data fields of an object, and observers to read, or otherwise get information about, the data fields. The simplest transformers are setters and the simplest observers are getters. StringLog has two constructors, one with a size field and one without. We have two transformers, one to insert a new string and one to clear the collection of all its strings while retaining the StringLog s name. We have five observers: three simple (get the name, ask the size, or find out whether we are full) and two more complicated (is a given string in the log, return a formatted list of all the strings in the log).

Code for the StringLog Interface Here is the code, without comments, to declare an interface defining the StringLog ADT. Later classes will implement this interface. Note that there are no constructors and no code for the methods -- they are abstract methods that will be overridden later by the real classes. The comments would say what each method does and give the precondition for insert (that the StringLog is not currently full). public interface StringLogInterface { void insert(string element); boolean isfull( ); int size( ); boolean contains(string element); void clear( ); String getname( ); String tostring( );}