Teach.NET Workshop Series Track 4: AP Computer Science with.NET and J#

Slides:



Advertisements
Similar presentations
AITI Lecture 19 Linked List Adapted from MIT Course 1.00 Spring 2003 Lecture 26 and Tutorial Note 9 (Teachers: Please do not erase the above note)
Advertisements

CS 206 Introduction to Computer Science II 09 / 05 / 2008 Instructor: Michael Eckmann.
DATA STRUCTURES Lecture: Interfaces Slides adapted from Prof. Steven Roehrig.
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
1 Class Design CS 3331 Fall Outline  Organizing classes  Design guidelines  Canonical forms of classes equals method hashCode method.
AbstractClassesInterfacesPolymorphism1 Abstract Classes, Interfaces, Polymorphism Barb Ericson Georgia Tech April 2010.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Interfaces Reference: Joe Hummel. 2 UCN Technology: Computer Science 2012 Objectives “Good class design starts with good application design — how many.
Using interfaces Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling How would you find the maximum.
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
Lecture 27 Exam outline Boxing of primitive types in Java 1.5 Generic types in Java 1.5.
1 Lecture 23 Searching Overview  What is Searching?  Linear Search and its Implementation.  Brief Analysis of Linear Search.  Binary Search and its.
Searching. 2 Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an element in it static.
Searching and Sorting I 1 Searching and Sorting 1.
Unit 261 Introduction to Searching and Sorting Comparable Interface Comparator Interface Algorithm Complexity Classes Exercises.
CS 106 Introduction to Computer Science I 11 / 20 / 2006 Instructor: Michael Eckmann.
ICS201 Lecture 20 : Searching King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
Searching. 2 Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an element in it static.
Searching. Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an element in it static.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
Iterators CS 367 – Introduction to Data Structures.
Searching Also: Logarithms. 2 Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an.
(c) University of Washingtonhashing-1 CSC 143 Java Hashing Set Implementation via Hashing.
Teach.NET Workshop Series Track 4: AP Computer Science with.NET and J#
CS 46B: Introduction to Data Structures July 7 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Topic 3 The Stack ADT.
Chapter 3 Introduction to Collections – Stacks Modified
Teach.NET Workshop Series Track 4: AP Computer Science with.NET and J#
Searching Also: Logarithms. 2 Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an.
Teach.NET Workshop Series Track 4: AP Computer Science with.NET and J#
Information and Computer Sciences University of Hawaii, Manoa
Chapter 14 Searching and Sorting Section 1 - Sequential or Linear Search Section 2 - Binary Search Section 3 - Selection Sort Section 4 - Insertion Sort.
Data structures and algorithms in the collection framework 1 Part 2.
Lecture 8: Object-Oriented Design. 8-2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET Objectives “Good object-oriented programming is really.
Chapter 9 Searching and Sorting
1 Java: AP Curriculum Focus and Java Subset Alyce Brady.
Generics and Collections. Introduction Generics New feature of J2SE 5.0 Provide compile-time type safety Catch invalid types at compile time Generic methods.
CS 206 Introduction to Computer Science II 09 / 10 / 2009 Instructor: Michael Eckmann.
1/20/03A2-1 CS494 Interfaces and Collection in Java.
15440 Distributed Systems Recitation 1 Objected-Oriented Java Programming.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Teach.NET Workshop Series Track 4: AP Computer Science with.NET and J#
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
Types in programming languages1 What are types, and why do we need them?
Today’s Agenda  Generic  Iterators CS2336: Computer Science II.
Joe Hummel, PhD Dept of Mathematics and Computer Science Lake Forest College Lecture 2: Working with Visual.
Teach.NET Workshop Series Track 4: AP Computer Science with.NET and J#
Parametric Polymorphism and Java Generics. Announcements One day extension on HW5 Because of an error in my HW5 config HW6 out, due November 10 Grades.
Teach.NET Workshop Series Track 4: AP Computer Science with.NET and J#
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
1 Class Design CS 3331 Sec 6.1 & 6.3 of [Jia03]. 2 Outline  Organizing classes  Design guidelines  Canonical forms of classes equals method hashCode.
Searching and Sorting 14ACEHRPT Copyright © 2011 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. Java Methods Object-Oriented.
CS2 Module 26 Category: OO Concepts Topic: Interfaces Objectives –Interfaces.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Data Structures in Java: From Abstract Data Types to the Java Collections.
CS 146: Data Structures and Algorithms June 11 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
CSC 243 – Java Programming, Spring, 2014 Week 4, Interfaces, Derived Classes, and Abstract Classes.
1 Stacks Abstract Data Types (ADTs) Stacks Application to the analysis of a time series Java implementation of a stack Interfaces and exceptions.
CS 151: Object-Oriented Design December 3 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
Teach.NET Workshop Series Track 4: AP Computer Science with.NET and J#
Searching.
Binary Search one reason that we care about sorting is that it is much faster to search a sorted list compared to sorting an unsorted list the classic.
Lecture 3: The J# Language
Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of ,
Collections Framework
Searching.
5. OOP OOP © 2003 Microsoft.
Presentation transcript:

Teach.NET Workshop Series Track 4: AP Computer Science with.NET and J#

“AP Computer Science with.NET and J#” Lecture 13: Interfaces and Interface-based Programming Microsoft.NET Workshops for Teachers

13-3 MicrosoftAP Computer Science with.NET and J# Workshop Track LectureTopic 7Algorithms and Algorithm Analysis 8Debugging and Exception Handling 9Applying Object-oriented Design Principles in Your Classes 10Using Inheritance to Design a Set of Classes 11MBS CS: the Marine Biology Simulation Case Study 12Extending the MBS CS 13Interfaces and Interface-based Programming 14Extending the MBS CS, Part 2 15Stacks, Queues and Invariants 16Linked Data Structures and the Linked-list 17Trees and Recursion 18Collection Classes and Iteration 19Additional Resources and Ideas

13-4 MicrosoftAP Computer Science with.NET and J# Lecture — Objectives “Good object-oriented programming is really about good object-oriented design. And good OOD is all about making things easier for ourselves, as well as for our class users…” Topics: –Design by contract –Interfaces [ Joe Hummel, Lake Forest College ]

13-5 MicrosoftAP Computer Science with.NET and J# Part 1 Design by Contract

13-6 MicrosoftAP Computer Science with.NET and J# Motivation We program by relying on "contracts" –i.e. agreements between objects –"Object X agrees to do Y, so I'll call X when I need to do Y" java.io.FileReader file = new java.io.FileReader("…"); java.io.BufferedReader reader = new java.io.BufferedReader(file); s = reader.readLine(); java.io.FileReader file = new java.io.FileReader("…"); java.io.BufferedReader reader = new java.io.BufferedReader(file); s = reader.readLine(); public class BufferedReader {. public String readLine() {. } public class BufferedReader {. public String readLine() {. }

13-7 MicrosoftAP Computer Science with.NET and J# Design by Contract Design by contract formalizes this process –design first –implement second Advantages? –encourages "think before you code“ for better designs –classes that implement the same contract are interchangeable — i.e. “plug-compatible” — yielding a more flexible, easier to evolve system –enforces a consistent design across a set of classes (much like inheritance), making it easier to program against these classes — can you say polymorphism !

13-8 MicrosoftAP Computer Science with.NET and J# Part 2 Interfaces

13-9 MicrosoftAP Computer Science with.NET and J# Interfaces Interfaces are a formal way of specifying contracts An interface = a set of method signatures –representing the contract between two or more parties –pure design, no data / code public interface SomeInterfaceName { public void Method1(); public int Method2(int x, String y);. } public interface SomeInterfaceName { public void Method1(); public int Method2(int x, String y);. }

13-10 MicrosoftAP Computer Science with.NET and J# Conceptual view Here's how to visualize what's going on: "server" (object performing service according to interface contract) "client" (object requesting service) Interface –"client" uses interface to communicate with server –"server" implements interface to fulfill contract

13-11 MicrosoftAP Computer Science with.NET and J# Example Sorting and binary search These algorithms are a part of the Java Class Library –great examples of the power of polymorphism — one sort and one binary search for all objects in Java! –use an interface to communicate with objects… sort( ) object binarySearch( ) object

13-12 MicrosoftAP Computer Science with.NET and J# Working with Interfaces Working with interfaces involves 3 steps: 1.Define the interface 2.Implement the interface 3.Program against the interface Let’s work through the sorting example…

13-13 MicrosoftAP Computer Science with.NET and J# (1) Comparable Interface Java’s Comparable interface formalizes what it means to compare 2 objects Defines a compare method that returns an integer: –< 0 means this object < parameter obj –= 0 means this object = parameter obj –> 0 means this object > parameter obj public interface Comparable { public int compareTo(Object obj); } public interface Comparable { public int compareTo(Object obj); } if (s1.compareTo(s2) < 0)...

13-14 MicrosoftAP Computer Science with.NET and J# (2) Sortable Objects Implement Comparable To fulfill contract, objects must implement Comparable: public class BankCustomer implements Comparable { private String firstName, lastName;. public int compareTo(Object obj) { // make sure we are comparing apples to apples… BankCustomer other = (BankCustomer) obj; int diff; diff = this.lastName.compareTo(other.lastName); if (diff == 0) // last names are the same, so look at first names: diff = this.firstName.compareTo(other.firstName); return diff; } public class BankCustomer implements Comparable { private String firstName, lastName;. public int compareTo(Object obj) { // make sure we are comparing apples to apples… BankCustomer other = (BankCustomer) obj; int diff; diff = this.lastName.compareTo(other.lastName); if (diff == 0) // last names are the same, so look at first names: diff = this.firstName.compareTo(other.firstName); return diff; } student Bank Customer

13-15 MicrosoftAP Computer Science with.NET and J# (3) Writing a Generic Sort() Now we can sort any list of Comparable objects! –For simplicity, here’s Selection sort: public static void sort(ArrayList list) { for (int i = 0; i < list.size(); i++) { for (int j = i+1; j < list.size(); j++) { Comparable obj1; Object obj2; obj1 = (Comparable) list.get(i); obj2 = list.get(j); if (obj1.compareTo(obj2) > 0) // swap! { list.set(i, obj2); list.set(j, obj1); } } //inner } //outer } public static void sort(ArrayList list) { for (int i = 0; i < list.size(); i++) { for (int j = i+1; j < list.size(); j++) { Comparable obj1; Object obj2; obj1 = (Comparable) list.get(i); obj2 = list.get(j); if (obj1.compareTo(obj2) > 0) // swap! { list.set(i, obj2); list.set(j, obj1); } } //inner } //outer }

13-16 MicrosoftAP Computer Science with.NET and J# Demo! Sorting bank customers…

13-17 MicrosoftAP Computer Science with.NET and J# Java’s Predefined Sort and Search Methods As long as our objects implement Comparable, we can take advantage of Java’s sort and binarySearch methods: ArrayList bank = new ArrayList(); bank.add(... );. java.util.Collections.sort(bank); int i; BankCustomer c; // we need a temporary object holding search criteria to pass to binary search: c = new BankCustomer(firstname, lastname, 0, null, null); i = java.util.Collections.binarySearch(bank, c); if (i >= 0) System.out.println("Found in arraylist at index " + i); else System.out.println("Not found!"); ArrayList bank = new ArrayList(); bank.add(... );. java.util.Collections.sort(bank); int i; BankCustomer c; // we need a temporary object holding search criteria to pass to binary search: c = new BankCustomer(firstname, lastname, 0, null, null); i = java.util.Collections.binarySearch(bank, c); if (i >= 0) System.out.println("Found in arraylist at index " + i); else System.out.println("Not found!");

13-18 MicrosoftAP Computer Science with.NET and J# Demo! Java’s sort and search methods…

13-19 MicrosoftAP Computer Science with.NET and J# Hey, what about Linear Search? Note that binary search only works if objects are sorted –Binary search is fast (O(lgn)), but sorting is slow (O(nlgn)) Suppose we only need to do 1 search, and data isn’t sorted? –Then a linear search (O(n)) makes more sense ArrayList bank;. int i; BankCustomer c; // we need a temporary object holding search criteria to pass to linear search: c = new BankCustomer(firstname, lastname, 0, null, null); i = bank.indexOf(c); // performs a linear search if (i >= 0) System.out.println("Found in arraylist at index " + i); else System.out.println("Not found!"); ArrayList bank;. int i; BankCustomer c; // we need a temporary object holding search criteria to pass to linear search: c = new BankCustomer(firstname, lastname, 0, null, null); i = bank.indexOf(c); // performs a linear search if (i >= 0) System.out.println("Found in arraylist at index " + i); else System.out.println("Not found!"); will *not* be found…

13-20 MicrosoftAP Computer Science with.NET and J# Overriding Equals Linear search is based on finding an object that is equal –And equality is defined by equals method, not compareTo... –… so override default version inherited from Object class public class BankCustomer implements Comparable { private String firstName, lastName;. public boolean equals(Object obj) { // make sure we are comparing apples to apples… if (obj == null || !(obj.getClass().equals(this.getClass()))) return false; BankCustomer other = (BankCustomer)obj; return this.lastName.equals(other.lastName) && this.firstName.equals(other.firstName); } public class BankCustomer implements Comparable { private String firstName, lastName;. public boolean equals(Object obj) { // make sure we are comparing apples to apples… if (obj == null || !(obj.getClass().equals(this.getClass()))) return false; BankCustomer other = (BankCustomer)obj; return this.lastName.equals(other.lastName) && this.firstName.equals(other.firstName); }

13-21 MicrosoftAP Computer Science with.NET and J# Demo! Overriding equals…

13-22 MicrosoftAP Computer Science with.NET and J# Summary Interfaces are another powerful tool for OO design Interfaces are used throughout Java –effective use requires a solid understanding of OOD –e.g. linear search is built-in, but you need to override equals() –e.g. sorting & binary search are built-in, but you need to implement Comparable and compareTo() Use interfaces when: –You are designing a set of classes –Classes adhere to the “acts-like” rule — a BankCustomer acts-like a sortable and searchable object

13-23 MicrosoftAP Computer Science with.NET and J# Resources Web site for slides, demos, associated lab exercises: – – – Supplemental Reading: –M. Weisfeld, “ The Object-Oriented Thought Process ” (language-neutral) –D. West, “ Object Thinking ” (language-neutral) –Sierra & Bates, “ Head First Java ” Your students may really like this one, a very hip approach to learning Java

13-24 MicrosoftAP Computer Science with.NET and J# That’s it! Next up: LectureTopic.. 14Extending the MBS CS, Part