How Many Sorts? if( list[j] < list[small]) small = j; double temp = list[i]; list[i] = list[small]; list[small] = temp; } // end of i loop

Similar documents
Aggregation and Composition. [notes Chapter 4]

EECS2030 Day 8 worksheet Tue Oct 3, // the easy way return Integer.compare(this.size, other.size);

Topic 5 Polymorphism. " Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code.

Review Questions II KEY

Use of the ArrayList class

Advanced Placement Computer Science. Inheritance and Polymorphism

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

CS 113 MIDTERM EXAM 2 SPRING 2013

CS/ENGRD 2110 SPRING 2018

Objects and Classes Continued. Engineering 1D04, Teaching Session 10

Chapter 13: Arrays of Objects

CS 307 Midterm 2 Spring 2011

Introduction to Computing II (ITI 1121) Final Examination

Topic 10. Abstract Classes. I prefer Agassiz in the abstract, rather than in the concrete.

Classes Classes 2 / 35

University Interscholastic League. Computer Science Competition

Refresher: Interface Specifications. ADT Documentation. Set Represented as an Array. Representation Invariant, Abstraction Function

Representation Invariant, Abstraction Function

COMP200 GENERICS. OOP using Java, from slides by Shayan Javed

Classes Classes 2 / 36

COP 3330 Final Exam Review

Collections class Comparable and Comparator. Slides by Mark Hancock (adapted from notes by Craig Schock)

Lecture 15 Summary. Collections Framework. Collections class Comparable and Comparator. Iterable, Collections List, Set Map

Classes and Methods לאוניד ברנבוים המחלקה למדעי המחשב אוניברסיטת בן-גוריון

CS 170 Java Programming 1. Week 15: Interfaces and Exceptions

Arrays Classes & Methods, Inheritance

Topic 3 Encapsulation - Implementing Classes

CS 307 Midterm 2 Fall 2009

CS 151. Linked Lists, Recursively Implemented. Wednesday, October 3, 12

Comparing Objects 3/14/16

Lecture 15 Summary 3/11/2009. By the end of this lecture, you will be able to use different types of Collections and Maps in your Java code.

Classes and Methods גרא וייס המחלקה למדעי המחשב אוניברסיטת בן-גוריון

Polymorphism and Interfaces. Corky Cartwright Department of Computer Science Rice University

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

Data abstractions: ADTs Invariants, Abstraction function. Lecture 4: OOP, autumn 2003

Topic 11 Linked Lists

CMSC 132: Object-Oriented Programming II. Interface

Prelim 1. CS 2110, October 1, 2015, 5:30 PM Total Question Name True Short Testing Strings Recursion

CSCI 136 Data Structures & Advanced Programming. Lecture 12 Fall 2018 Profs Bill & Jon

public Student(int theage, String thename, char thegrade) { age = theage; name = thename; grade = thegrade; } public int getage() { return age;}

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

Generic types. Announcements. Raw ArrayLists. Generic types (cont.) Creating a raw ArrayList: Accessing a raw ArrayList:

Closed book but one sheet, both sides, of A4 paper is allowed. Section 2.5 of the text Generics in the Java Programming Languages by Gilad Bracha

Alex Mariakakis CSE 331, Autumn 2013 With material from Krysta Yousoufian, Marty Stepp, Mike Ernst, and others

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

CS1004: Intro to CS in Java, Spring 2005

CS231 - Spring 2017 Linked Lists. ArrayList is an implementation of List based on arrays. LinkedList is an implementation of List based on nodes.

Comparing Objects 3/28/16

CS 312 Exam 2 Fall KG Kris Megan Roman Sonika

Polymorphism and Interfaces. CGS 3416 Spring 2018

G51PRG: Introduction to Programming Second semester

Basic Class Diagrams. Class Diagrams, cont d. Class Diagrams, cont d. Car. Car. Car. What does the minus sign here mean?

Java Collection Framework

CSC 1052 Algorithms & Data Structures II: Lists

Slides are adapted from the originals available at

More on collec)ons and sor)ng

Classes and Methods עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון מבוסס על השקפים של אותו קורס שניתן בשנים הקודמות

Java-Array. This tutorial introduces how to declare array variables, create arrays, and process arrays using indexed variables.

CS 61B Midterm 1 Guerrilla Section Spring 2018 February 10, 2018

CS 307 Midterm 2 Fall 2010

CS 251 Intermediate Programming Inheritance

Chapter 6 Single-dimensional Arrays

Who we are: Lecture 1: Overview & Java. Why CS 62? Sample Problems. CS 62 Fall 2016 Kim Bruce & Peter Mawhorter

COMP-202 Unit 7: More Advanced OOP. CONTENTS: ArrayList HashSet (Optional) HashMap (Optional)

Introduction to OCaml

CSE 143 Lecture 20. Circle

COMP 110/L Lecture 20. Kyle Dewey

//main.cpp. using namespace std;

Pointers and Strings Chapters 10, Pointers and Arrays (10.3) 3.2 Pointers and Arrays (10.3) An array of ints can be declared as

COMP200 INTERFACES. OOP using Java, from slides by Shayan Javed

CS 11 Ocaml track: lecture 2

ComS 207: Programming I Homework 7 Out: Wed. Oct 11, 2006 Due: Fri. Oct 20, 2006 (Online submission *ONLY*) Student Name: Recitation Section:

CP222 Computer Science II. Linked Lists

Module 7: Arrays (Single Dimensional)

Binghamton University. CS-140 Fall Interfaces

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

CSCI 261 Computer Science II

Question 2. [5 points] State a big-o upper bound on the worst case running time of the given

COMP1008 An overview of Polymorphism, Types, Interfaces and Generics

Creating an object Instance variables

Java Object Oriented Design. CSC207 Fall 2014

Sample file. Practice Exam One COMPUTER SCIENCE A SECTION I. Directions: Determine the answer to each of the following questions or incomplete

The ArrayList class CSC 123 Fall 2018 Howard Rosenthal

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

Inheritance and Interfaces

Review: Object Diagrams for Inheritance. Type Conformance. Inheritance Structures. Car. Vehicle. Truck. Vehicle. conforms to Object

CS111: PROGRAMMING LANGUAGE II

CSE115 Introduction to Computer Science for Majors I Spring 2008 Final Exam. First/Given Name (PRINT) May 6, 2008

Administration. Objects and Arrays. Objects. Agenda. What is an Object? What is a Class?

COMP 250. Lecture 29. interfaces. Nov. 18, 2016

Introduction to Object-Oriented Programming

CS-140 Fall 2018 Test 2 Version A Nov. 12, Name:

Lesson 2.4 Arraylist

Family Name:... Other Names:... ID Number:... Signature... Model Solutions. COMP 103: Test 1. 9th August, 2013

Points off Total off Net Score. CS 314 Final Exam Spring 2016

Building Java Programs Sample Final Exam #1. Name of Student. Section (e.g., AA)

Topic 5: Enumerated Types and Switch Statements

CSE 143. Lecture 13: Interfaces, Comparable reading: , 16.4, 10.2

1. An object of a class can be treated as an object of its corresponding class.

Outline. applications of hashing equality and comparisons in Java sorting selection sort bubble sort insertion sort

Transcription:

Topic 7 Interfaces I once attended a Java user group meeting where James Gosling (one of Java's creators) was the featured speaker. During the memorable Q&A session, someone asked him: "If you could do Java over again, what would you change?" "I'd leave out classes," he replied. After the laughter died down, he explained that the real problem wasn't classes per se, but rather implementation inheritance (the extends relationship). Interface inheritance (the implements relationship) is preferable. - Allen Holub

How Many Sorts? How many sorts do you want to have to write? public static void SelSort(double[] list) { for(int i = 0; i < list.length - 1; i++) { int small = i; for(int j = i + 1; j < list.length; j++) { } if( list[j] < list[small]) small = j; } double temp = list[i]; list[i] = list[small]; list[small] = temp; } // end of i loop CS314 Interfaces 2

Why interfaces? Interfaces allow the creation of abstract data types "A set of data values and associated operations that are precisely specified independent of any particular implementation. " multiple implementations allowed Interfaces allow a data type to be specified without worrying about the implementation do design first What will this data type do? Don t worry about implementation until design is done. separation of concerns allow us to create generic algorithms CS314 Interfaces 3

Interfaces public interface List<E> { No constructors No instance variables abstract methods default methods static methods class constants (prefer enums) public static final int DEFAULT_SIDES = 6 CS314 Interfaces 4

Implementing Interfaces A class inherits (extends) exactly one other class, but A class can implement as many interfaces as it likes public class ArrayList implements List A class that implements an interface must provide implementations of all method declared in the interface or the class must be abstract interfaces can extend other interfaces CS314 Interfaces 5

The Comparable Interface The Java Standard Library contains a number of interfaces names are italicized in the class listing One of the most important interfaces is the Comparable interface CS314 Interfaces 6

Comparable Interface package java.lang; public interface Comparable<T> { public int compareto(t other); } compareto must return an int <0 if the calling object is less than the parameter, 0 if they are equal an int >0 if the calling object is greater than the parameter compareto should be consistent with equals but this isn't required. CS314 Interfaces 7

Interfaces "Use interfaces to ensure a class has methods that other classes or methods will use." Anthony, Spring 2013 The other classes or methods are already done. The other methods or classes call interface type methods POLYMORPHISM old code using new code CS314 Interfaces 8

Clicker What is output by the following code? Comparable c1 = new Comparable(); Comparable c2 = new Comparable(); System.out.println(c1.compareTo(c2)); A. A value < 0 B. 0 C. A value > 0 D. Unknown until program run E. Other CS314 Interfaces 9

Example compareto Suppose we have a class to model playing cards Ace of Spades, King of Hearts, Two of Clubs each card has a suit and a value, represented by ints this version of compareto will compare values first and then break ties with suits CS314 Interfaces 10

compareto in a Card class public class Card implements Comparable<Card> { } public int compareto(card othercard) { return this.rank - other.rank; } // other methods not shown Assume ints for ranks (2, 3, 4, 5, 6,...) and suits (0 is clubs, 1 is diamonds, 2 is hearts, 3 is spades). CS314 Interfaces 11

Interfaces and Polymorphism Interfaces may be used as the data type for object variables Can t simply create objects of that type Can refer to any objects that implement the interface or descendants Assume Card implements Comparable Card c = new Card(); Comparable comp1 = new Card(); Comparable comp2 = c; CS314 Interfaces 12

Clicker Question 1 Which of the following lines of code causes a syntax error? Comparable c1; // A c1 = new Comparable(); // B Comparable c2 = "Kelly"; // C int x = c2.compareto("ann"); // D // E more than one of A - D // what is x after statement? CS314 Interfaces 13

Why Make More Work? Why bother implementing an interface such as Comparable objects can use method that expect an interface type Example if I implement Comparable: Arrays.sort(Object[] a) public static void sort(object[] a) All elements in the array must implement the Comparable interface. Furthermore, all elements in the array must be mutually comparable objects of my type can be stored in data structures that accept Comparables CS314 Interfaces 14

A List Interface What if we wanted to specify the operations for a List, but no implementation? Allow for multiple, different implementations. Provides a way of creating abstractions. a central idea of computer science and programming. specify "what" without specifying "how" "Abstraction is a mechanism and practice to reduce and factor out details so that one can focus on a few concepts at a time. " CS314 Interfaces 15

List Interface public interface List<E> { } public void add(e val); public int size(); public E get(int location); public void insert(int location, E val); public E remove(int location); CS314 Interfaces 16