Object Oriented Programming in Java Habib Rostami Lecture 7.

Slides:



Advertisements
Similar presentations
Chapter 6 The Collections API. Simple Container/ Iterator Simple Container Shape [] v = new Shape[10]; Simple Iterator For( int i=0 ; i< v.length ; i++)
Advertisements

0 of 37 Stacks and Queues Lecture of 37 Abstract Data Types To use a method, need to know its essentials: signature and return type o additionally,
CSC 205 – Java Programming II Lecture 25 March 8, 2002.
5-May-15 ArrayLists. 2 ArrayList s and arrays A ArrayList is like an array of Object s Differences between arrays and ArrayList s: Arrays have special.
Interfaces.
CS 106 Introduction to Computer Science I 04 / 30 / 2007 Last lecture :( Instructor: Michael Eckmann.
CS 307 Fundamentals of Computer Science 1 Abstract Data Types many slides taken from Mike Scott, UT Austin.
CS 106 Introduction to Computer Science I 04 / 27 / 2007 Instructor: Michael Eckmann.
Java Collections Framework COMP53 Oct 24, Collections Framework A unified architecture for representing and manipulating collections Allows collections.
CS 106 Introduction to Computer Science I 05 / 03 / 2010 Instructor: Michael Eckmann.
15-Jun-15 Lists in Java Part of the Collections Framework.
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
24-Jun-15 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Unit 291 Java Collections Framework: Interfaces Introduction to the Java Collections Framework (JCF) The Comparator Interface Revisited The Collection.
Lists in Java Part of the Collections Framework. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection.
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 13: Queues and Vectors.
12-Jul-15 Lists in Java Part of the Collections Framework.
Stacks, Queues, and Deques
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
Stacks, Queues, and Deques
1 Collection, Iterable, and Iterator Interfaces The Collection Interface and its Hierarchy The Iterable and Iterator Interfaces For-each Loops with Iterable.
SEG4110 – Advanced Software Design and Reengineering TOPIC G Java Collections Framework.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Chapter 3 List Stacks and Queues. Data Structures Data structure is a representation of data and the operations allowed on that data. Data structure is.
Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.
Collections in Java. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection with no duplicates SortedSet.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Generalized Containers CSIS 3701: Advanced Object Oriented Programming.
1 Java: AP Curriculum Focus and Java Subset Alyce Brady.
Object Oriented Programming in Java Habib Rostami Lecture 6.
Geoff Holmes Palindrome solutions Overview Arrays Collections Enumerators Vector BitSet Stack Dictionary Hashtable Collection Classes (Chapter 19) import.
Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.
Data structures Abstract data types Java classes for Data structures and ADTs.
1/20/03A2-1 CS494 Interfaces and Collection in Java.
Collections in Java. 2 Collections Hierarchy > ArrayListVector Stack LinkedList > Arrays Collections.
CS 106 Introduction to Computer Science I 04 / 25 / 2008 Instructor: Michael Eckmann.
CSE 143 Lecture 20 Abstract classes. 2 Circle public class Circle { private double radius; public Circle(double radius) { this.radius = radius; } public.
Recitation 5 Enums and The Java Collections classes/interfaces 1.
Computer Science 209 Software Development Inheritance and Composition.
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
IMPLEMENTING ARRAYLIST COMP 103. RECAP  Comparator and Comparable  Brief look at Exceptions TODAY  Abstract Classes - but note that the details are.
CS-2852 Data Structures LECTURE 2 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
1/20/05A-1 © 2001 T. Horton CS 494 Adv. SW Design and Development A Tasting…. Course 1: Design patterns: Intro, example Course 2: Inheritance, Interfaces,
CS2852 Week 3, Class 2 Today Big-O runtime analysis Linked Lists Muddiest Point Lab Quiz Includes writing a method from ArrayList class (See next slide)
Data Structures Lakshmish Ramaswamy. Removing Element public Object remove(int index){ Object retobj; if(index size){ throw IndexOutOfBoundsException.
C19: Collection Classes (don’t forget to look at all the online code examples)
1 The copy constructor in the BankAccounts class. Two alternatives here: /** copy constructor */ public BankAccounts(BankAccounts L){ theAccounts = L.theAccounts.clone();
Implementing ArrayList Part T2 Lecture 6 School of Engineering and Computer Science, Victoria University of Wellington  Thomas Kuehne, Marcus Frean,
19-Mar-16 Collections and ArrayLists.. 2 Collections Why use Collections. Collections and Object-Orientation. ArrayLists. Special Features. Creating ArrayLists.
CS-2852 Data Structures Week 4, Class 1 - Review Review! Thursday Exam I - Review Implementing ArrayList Big-O Iterators – High-level description Linked.
 2016, Marcus Biel, ArrayList Marcus Biel, Software Craftsman
Catalog of Refactoring
Data Structures Lakshmish Ramaswamy.
Stacks.
Software Development Inheritance and Composition
COP 3503 FALL 2012 Shayan Javed Lecture 8
Implementing ArrayList Part 1
TCSS 143, Autumn 2004 Lecture Notes
Programming in Java Lecture 11: ArrayList
(Java Collections Framework) AbstractSequentialList
ArrayLists 22-Feb-19.
Based on slides by Alyssa Harding & Marty Stepp
JCF Collection classes and interfaces
Programming II (CS300) Chapter 02: Using Objects Java ArrayList Class
slides created by Alyssa Harding
Web Design & Development Lecture 6
Lecture 16 Stacks and Queues CSE /26/2018.
Java Generics & Iterators
Presentation transcript:

Object Oriented Programming in Java Habib Rostami Lecture 7

Today’s Presentation  Abstract classes ( Some examples )  Interfaces

Abstract class Shape abstract class Shape { abstract double area ();} class Triangle extends Shape { double b, h; Triangle (double b, double h) { this.b=b; this.h=h;} double area () {return 0.5*b*h;} } class Square extends Shape { double a; Square (double a) { this.a=a;} double area () {return 0.5*a*a;} }

Casting to super class public class Test { Shape s = new Triangle(1,3); Shape s1 = new Square(2); }

Casting to super class public class Test { Shape[] ar = new Shape[2]; public Test(){ ar[0] = new Triangle(1,3); ar[1] = new Square(2); } public double getArea(int i){ return ar[i].area(); }

Determining the interface –before writing a class definition, determine the interface the set of services we offer to clients –similarly, if defining data structures, we should first determine the interface stacks support a constructor, push, pop, size, isEmpty, and top queues offer a constructor, enqueue, dequeue, size, isEmpty and front

Data Abstraction –if the interface remains the same, clients don't need to be changed, even if the implementation behind the interface changes public class Time { private int timeInSecs; //public methods } public class Time { private int hours; private int minutes private int secs; //same public methods //but with different //bodies }

Java Interfaces –Java allows us to take this one stage further, by formally recording the interface as a Java interface –a java interface is just a collection of abstract methods (i.e. we state the signatures, but not the bodies) public interface MyStack { public int size(); public boolean isEmpty(); public Object top(); public void push(Object elt); public Object pop(); } states that this is an interface, not a class states that this is an interface, not a class note no bodies for the methods note no bodies for the methods

interfaces vs classes –a class definition can contain instance/class variables and instance/class methods, including both the signature and the body –a java interface contains only the signatures of public instance methods (and named constants) –a java interface acts like a specification it says what it means to be e.g. a stack to be a stack, an object must offer at least those methods in its public interface

using Java interfaces –Java allows us to tell the compiler that a class will implement an interface –regard it as a contract stating that you will meet the specification –any class that implements an interface must provide implementations of the public methods (or it must be abstract, and its subclasses provide them) –the compiler will check, and if the bodies are not provided, it won't compile

Example import java.util.ArrayList; public class ALStack implements MyStack { private ArrayList al; public ALStack() { al = new ArrayList (); } public int size() { return al.size(); } //and versions of isEmpty, push, pop and top, as //in the previous lecture } promises that we will provide bodies for all the MyStack methods promises that we will provide bodies for all the MyStack methods

Example (cont) public class ArrayStack implements MyStack { private int capacity; private Object[] s; private int top = -1; public ArrayStack(int reqdCapacity) { capacity = reqdCapacity; s = new Object[capacity]; } public int size() { return top+1; } //and versions of isEmpty, push, pop and top... }

Polymorphism public class Test{ private MyStack s = new new ArrayStack(20); public doSomeThing(){ s.put(new Point(10,10)); }

Some java examples java.util.List Interface

1.0t15 Collection (interface) A group of objects Major methods: –int size(); –boolean isEmpty(); –boolean contains(Object); –Iterator iterator(); –Object[] toArray(); –boolean add(Object); –boolean remove(Object); –void clear();

1.0t16 List interface List extends Collection An ordered collection of objects Duplicates allowed

1.0t17 List Details Major additional methods: –Object get(int); –Object set(int, Object); –int indexOf(Object); –int lastIndexOf(Object); –void add(int, Object); –Object remove(int); –List subList(int, int); add() inserts remove() deletes Implemented by: –ArrayList, LinkedList, Vector