Shapes Consider the task of writing classes to represent 2D shapes such as Circle, Rectangle, and Triangle. Certain operations are common to all shapes:

Slides:



Advertisements
Similar presentations
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
Advertisements

© 2006 Pearson Addison-Wesley. All rights reserved16-1 Methods in the List Interface (Part 1 of 16)
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 24 : Collections King Fahd University of Petroleum & Minerals College of Computer.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L15 (Chapter 22) Java Collections.
Collections The objectives of this chapter are: To outline the Collections infrastructure in Java To describe the various collection classes To discuss.
Building Java Programs Inner classes, generics, abstract classes reading: 9.6, 15.4,
CSE 143 Lecture 14 Interfaces; Abstract Data Types (ADTs) reading: 9.5, 11.1; 16.4 slides created by Marty Stepp
Building Java Programs Interfaces, Comparable reading: , 16.4, 10.2.
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-4: Interfaces reading: self-check: exercises: #11.
Perimeter and Area o Perimeter is the distance around the outside of a flat object. o Area is the amount of surface space that a flat object has.
CSE 143 Lecture 9 Interfaces; Stacks and Queues slides created by Marty Stepp
CSS446 Spring 2014 Nan Wang.  Java Collection Framework ◦ Set ◦ Map 2.
Building Java Programs Bonus Slides Hashing. 2 Recall: ADTs (11.1) abstract data type (ADT): A specification of a collection of data and the operations.
CSE 143 Lecture 6 Interfaces; Complexity (Big-Oh) reading: 9.5, 11.1, slides created by Marty Stepp
EE 422C Interfaces Day 5. 2 Announcements SVN has Project 2. –Partly due next week. –SVN update to get it in your repository. See Piazza for Grocery List.
1 Java's Collection Framework Map and Sets. 2 Collection Framework  A collections framework is a unified architecture for representing and manipulating.
University of Limerick1 Collections The Collection Framework.
CSE 373 Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
Building Java Programs Chapter 13 Sorting reading: 13.3, 13.4.
Calculating the Perimeter & Area of a Rectangle For more maths help & free games related to this, visit:
Building Java Programs Interfaces reading: , 16.4.
Lecture 21: Interfaces and Abstract classes
CSE 143 Lecture 14 Interfaces; Abstract Data Types (ADTs)
Lecture 23:More on Interfaces
Lecture 13: Interfaces, Comparable reading: , 16.4, 10.2
CSE 143 Lecture 20 Binary Search Trees continued; Tree Sets
Chapter 19 Java Data Structures
Building Java Programs
Interfaces.
A tree set Our SearchTree class is essentially a set.
CSE 373: Data Structures and Algorithms
Wednesday Questions How do I debug? We love to teach this at IPL/OH!
Wednesday Notecards What’s your credit card number?
Lecture 1: Welcome to CSE 373
slides created by Marty Stepp and Hélène Martin
Interfaces EE 422C.
Linked node problem 3 What set of statements turns this picture:
Collections class Method name Description binarySearch(list, value)
Building Java Programs
slides adapted from Marty Stepp and Hélène Martin
Building Java Programs
Interfaces CS163 Fall 2018.
CSE 143 Lecture 6 Interfaces; Complexity (Big-Oh)
Building Java Programs
A tree set Our SearchTree class is essentially a set.
CSE 154 Sorting reading: 13.3, 13.4
Collections in Java The objectives of this lecture are:
Review: Classes, Inheritance, and Collections
slides created by Marty Stepp
Collections class Method name Description binarySearch(list, value)
slides created by Marty Stepp and Ethan Apter
slides created by Marty Stepp
Binary Search Trees continued; Tree Sets
CSE 143 Sorting reading: 13.3, 13.4.
slides created by Marty Stepp
Based on slides by Alyssa Harding & Marty Stepp
Building Java Programs Chapter 13
Binary search (13.1) binary search: Locates a target value in a sorted array/list by successively eliminating half of the array from consideration. How.
slides created by Marty Stepp and Hélène Martin
CSE 373 Implementing a Stack Reading: Weiss Ch. 3; 3.6; 1.5
CSE 373 Sorting 1: Bogo Sort, Stooge Sort, Bubble Sort
Area & Perimeter.
slides created by Marty Stepp
CSE 143 Lecture 8 Iterators; Comparable reading: 11.2; 10.2
Collections class Method name Description binarySearch(list, value)
Lecture 7: Linked List Basics reading: 16.2
CSE 143 Lecture 6 interfaces; eclipse; testing
CSE 373 Java Collection Framework reading: Weiss Ch. 3, 4.8
Wednesday Questions How do I debug? We love to teach this at IPL/OH!
slides created by Marty Stepp and Hélène Martin
Presentation transcript:

Shapes Consider the task of writing classes to represent 2D shapes such as Circle, Rectangle, and Triangle. Certain operations are common to all shapes: perimeter: distance around the outside of the shape area: amount of 2D space occupied by the shape Every shape has these, but each computes them differently.

Shape area and perimeter Circle (as defined by radius r ): area = ½ π r 2 perimeter = 2 π r Rectangle (as defined by width w and height h ): area = w h perimeter = 2w + 2h Triangle (as defined by side lengths a, b, and c) area = √(s (s - a) (s - b) (s - c)) where s = ½ (a + b + c) perimeter = a + b + c r w h a b c

Interfaces (9.5) interface: A list of methods that a class can promise to implement. Inheritance gives you an is-a relationship and code sharing. A Lawyer can be treated as an Employee and inherits its code. Interfaces give you an is-a relationship without code sharing. A Rectangle object can be treated as a Shape but inherits no code. Analogous to non-programming idea of roles or certifications: "I'm certified as a CPA accountant. This assures you I know how to do taxes, audits, and consulting." "I'm 'certified' as a Shape, because I implement the Shape interface. This assures you I know how to compute my area and perimeter."

Interface syntax public interface name { public type name(type name, ..., type name); ... } Example: // Describes features common to all shapes. public interface Shape { public double area(); public double perimeter();

ADTs as interfaces (11.1) abstract data type (ADT): A specification of a collection of data and the operations that can be performed on it. Describes what a collection does, not how it does it. Java's collection framework uses interfaces to describe ADTs: Collection, Deque, List, Map, Queue, Set An ADT can be implemented in multiple ways by classes: ArrayList and LinkedList implement List HashSet and TreeSet implement Set LinkedList , ArrayDeque, etc. implement Queue They messed up on Stack; there's no Stack interface, just a class.

Why use ADTs? Why would we want more than one kind of list, queue, etc.? Answer: Each implementation is more efficient at certain tasks. ArrayList is faster for adding/removing at the end; LinkedList is faster for adding/removing at the front/middle. Etc. You choose the optimal implementation for your task, and if the rest of your code is written to use the ADT interfaces, it will work.

Collections class Method name Description binarySearch(list, value) returns the index of the given value in a sorted list (< 0 if not found) copy(listTo, listFrom) copies listFrom's elements to listTo emptyList(), emptyMap(), emptySet() returns a read-only collection of the given type that has no elements fill(list, value) sets every element in the list to have the given value max(collection), min(collection) returns largest/smallest element replaceAll(list, old, new) replaces an element value with another reverse(list) reverses the order of a list's elements shuffle(list) arranges elements into a random order sort(list) arranges elements into ascending order

The compareTo method (10.2) The standard way for a Java class to define a comparison function for its objects is to define a compareTo method. Example: in the String class, there is a method: public int compareTo(String other) A call of A.compareTo(B) will return: a value < 0 if A comes "before" B in the ordering, a value > 0 if A comes "after" B in the ordering, 0 if A and B are considered "equal" in the ordering.

Interfaces (9.5) interface: A list of methods that a class can promise to implement. Inheritance gives you an is-a relationship and code sharing. A Lawyer can be treated as an Employee and inherits its code. Interfaces give you an is-a relationship without code sharing. A Rectangle object can be treated as a Shape but inherits no code. Analogous to non-programming idea of roles or certifications: "I'm certified as a CPA accountant. This assures you I know how to do taxes, audits, and consulting." "I'm 'certified' as a Shape, because I implement the Shape interface. This assures you I know how to compute my area and perimeter."

Comparable (10.2) public interface Comparable<E> { public int compareTo(E other); } A class can implement the Comparable interface to define a natural ordering function for its objects. A call to your compareTo method should return: a value < 0 if the this object comes "before" other one, a value > 0 if the this object comes "after" other one, 0 if the this object is considered "equal" to other.