Another interface: Comparable

Similar documents
== isn t always equal?

Last time s big ideas

Why do we have to write a type for every variable in Java?

The class Object. Lecture CS1122 Summer 2008

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

CS 2230 CS II: Data structures. Meeting 26: the Set ADT Brandon Myers University of Iowa

CSE 331 Summer 2017 Final Exam. The exam is closed book and closed electronics. One page of notes is allowed.

MIT AITI Lecture 18 Collections - Part 1

CS 2230 CS II: Data structures. Meeting 20: generic types, exceptions, higher order functions Brandon Myers University of Iowa

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

Prelim 1 SOLUTION. CS 2110, September 29, 2016, 7:30 PM Total Question Name Loop invariants. Recursion OO Short answer

Outline for Today CSE 142. CSE142 Wi03 G-1. withdraw Method for BankAccount. Class Invariants

Introduction to Programming Using Java (98-388)

CSE 331 Midterm Exam Sample Solution 2/18/15

Program development plan

Comparing Objects 3/28/16

Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II

Due: 9 February 2017 at 1159pm (2359, Pacific Standard Time)

Sorting race.

Expressions and Casting

CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch

CS 315 Software Design Homework 3 Preconditions, Postconditions, Invariants Due: Sept. 29, 11:30 PM

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

3 ADT Implementation in Java

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

+ Abstract Data Types

CS1004: Intro to CS in Java, Spring 2005

Solutions to Quiz 1 (March 14, 2016)

CSCI 200 Lab 3 Using and implementing sets

Comparing Objects 3/14/16

Prelim 1. CS 2110, September 29, 2016, 7:30 PM Total Question Name Loop invariants

Homework #10 due Monday, April 16, 10:00 PM

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

Super-Classes and sub-classes

CISC-124. Casting. // this would fail because we can t assign a double value to an int // variable

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

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

Lab 5 Pointers and Arrays

Type Hierarchy. Comp-303 : Programming Techniques Lecture 9. Alexandre Denault Computer Science McGill University Winter 2004

CS/ENGRD 2110 SPRING 2018

Where we are going (today)

CS-140 Fall 2017 Test 1 Version Practice Practice for Nov. 20, Name:

Collections, Maps and Generics

Lecture 5: Implementing Lists, Version 1

The Java Collections Framework and Lists in Java Parts 1 & 2

CS-201 Introduction to Programming with Java

Software Testing Prof. Meenakshi D Souza Department of Computer Science and Engineering International Institute of Information Technology, Bangalore

CS 2230 CS II: Data structures. Meeting 21: trees Brandon Myers University of Iowa

QUEEN MARY, UNIVERSITY OF LONDON DCS128 ALGORITHMS AND DATA STRUCTURES Class Test Monday 27 th March

Equality for Abstract Data Types

CS/ENGRD 2110 FALL Lecture 6: Consequence of type, casting; function equals

Introduction to Object-Oriented Programming

CSE 331 Final Exam 3/16/15 Sample Solution

Test 1: CPS 100. Owen Astrachan. October 1, 2004

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

JAVA REVIEW cs2420 Introduction to Algorithms and Data Structures Spring 2015

CSE 331 Midterm Exam Sample Solution 2/13/12

Object-Oriented Design Lecture 11 CS 3500 Spring 2010 (Pucella) Tuesday, Feb 16, 2010

Software Development (cs2500)

CS 2113 Software Engineering

Classes Classes 2 / 35

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented

6.005 Elements of Software Construction Fall 2008

Quiz 1 (March 14, 2016)

Public-Service Announcement

UNIT TESTING. Krysta Yousoufian CSE 331 Section April 19, With material from Marty Stepp, David Notkin, and The Pragmatic Programmer

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

CS/ENGRD 2110 SPRING 2018

CS61C Machine Structures. Lecture 3 Introduction to the C Programming Language. 1/23/2006 John Wawrzynek. www-inst.eecs.berkeley.

CS61B Lecture #3. Homework: Please see Homework #1 on the lab page. Last modified: Wed Sep 10 20:34: CS61B: Lecture #3 1

Today. Reading. Homework. Lecture Notes CPSC 224 (Spring 2012) hashcode() method. Collections class. Ch 9: hw 7 out (due in a week)


Lab 4: Super Sudoku Solver CSCI 2101 Fall 2017

Compsci 201 Binary Trees Recurrence Relations

CS 170 Java Programming 1. Week 13: Classes, Testing, Debugging

Advanced Java Concepts Unit 2: Linked Lists.

CS211 Computers and Programming Matthew Harris and Alexa Sharp July 9, Boggle

CS 215 Software Design Homework 3 Due: February 28, 11:30 PM

Last time. Reasoning about programs. Coming up. Project Final Presentations. This Thursday, Nov 30: 4 th in-class exercise

Reasoning about programs

Object-Oriented Design Lecture 3 CSU 370 Fall 2007 (Pucella) Friday, Sep 14, 2007

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15

Where we are going (today)

CS-202 Introduction to Object Oriented Programming

Invariants What is an invariant?

Maps and Binary Search

COLLECTIONS & ITERATORS cs2420 Introduction to Algorithms and Data Structures Spring 2015

I have neither given nor received any assistance in the taking of this exam.

Introduction to Computation for the Humanities and Social Sciences. CS 3 Chris Tanner

COP 3330 Final Exam Review

Announcements. Lecture 15 Generics 2. Announcements. Big picture. CSE 331 Software Design and Implementation

CSE 331 Software Design and Implementation. Lecture 15 Generics 2

Announcements/Follow-ups

Processor. Lecture #2 Number Rep & Intro to C classic components of all computers Control Datapath Memory Input Output

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

Types, Expressions, and States

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

Lab 9: More Sorting Algorithms 12:00 PM, Mar 21, 2018

Java Comparable interface

CSE331 Autumn 2011 Midterm Examination October 28, 2011

Transcription:

Another interface: Comparable Comparing things is certainly useful, e.g. for sorting Show me cats ordered by cuteness" Show shapes ordered by number of sides"

An example interface: Comparable Since sorting things is so useful, we might want to write code that knows how to sort anything (or, at least...anything Comparable)

public static void insertsorted(comparable[] sorted, int currentsize, Comparable newelement) recall the algorithm for inserting into a sorted array: insert new element at the the end, then swap until it is in the right place Harry Ron Snape registernewpatient( Hermione ) Harry Ron Snape Hermione Harry Ron Hermione Snape Harry Hermione Ron Snape

public static void insertsorted(comparable[] sorted, int currentsize, Comparable newelement) { } sorted[currentsize] = newelement; int i = currentsize; while (i > 0 && sorted[i-1].compareto(sorted[i]) > 0) { // swap... } initially myarray: [1, 4, 7] What is the sequence of compareto return values when we call insertsorted(myarray, 3, 5) a) 1 1-1 b) 1 0 c) 1 0-1 d) -1-1 1 e) 1-1 https://b.socrative.com/login/student/ room CS2230X ids 1000-2999 room CS2230Y ids 3000+

Today s big ideas An abstract data type (ADT) is a model of data from the perspective of the user A Java interface defines only the signatures of methods. Interfaces are useful for defining ADTs List is an ADT that models a mutable list of elements; LinkedList and ArrayList are concrete implementations of List Interfaces can be used to write code once that works for objects of different classes (called reuse)

Administrivia HW 3 / Quiz 3 available by tomorrow Application: browser tabs stored in a linked list practice using a testing tool (JUnit) Section today fixing bugs in the SLinkedList from pre-lab learn to use JUnit to debug a program remember: prelab question #1 must be turned in on paper beginning of lab

HW3: linked lists applied to browser tabs You will write methods for a LinkedList class and debug with test cases. Each method will provide new functionality for tabs (reorder, open, close, display,...). \

Today s big ideas 2 Testing you should write tests JUnit helps you organize and run your tests a look at test driven development Different kinds of equality in Java

How do you know your program works? What evidence do you have that your programs worked in HW1 and HW2? https://b.socrative.com/login/student/ room CS2230X ids 1000-2999 room CS2230Y ids 3000+

What do we mean by your program works? Usually, we mean that the implementation obeys the specification // add the integer d to the end of the list // (also...please don't crash!) public void append(int d) { } the specification for LinkedList.append() your implementation of LinkedList.append() is correct if it obeys the above specification

Many ways that people gather evidence that their program works testing: run it, automatically check the output static analysis: automatically check properties without actually running your program handwritten proof: write a mathematical proof that the program works machine-checked proof: automatically verify your proof that the program works is correct model checking: with help, the computer comes up with a proof for you, but only for finite number of cases

Evidence in CS2230 you will mostly rely on tests that you write compiler will check for simple errors for you (e.g., types and syntax) before you write code for an algorithm you want at least an informal proof that it is correct testing: run it, automatically check the output static analysis: automatically check properties without actually running your program handwritten proof: write a mathematical proof that the program works

Approach in CS 2230 you will learn to write and use tests you will learn to debug programs systematically you will learn to write assertions that check the invariants of your data structures

What should you test? (be brief but specific) https://b.socrative.com/login/student/ room CS2230X ids 1000-2999 room CS2230Y ids 3000+

In many software projects, the tests get the final say There is a golden copy of the code that passes all the tests work-in-progress on new features in a separate copy. (might break things) not allowed to merge the new feature into the golden copy unless all the tests pass https://guides.github.com/introduction/flow/

Goal for today create a class with this specification and test it // insert the String s at the front of the list void insertfirst(string s) { } // Remove the String at the front of the list // and return it. Returns null if the list is empty. String removefirst() { } // Print out the elements of the list in order void printlist() { }

Approach for today We ll use a methodology called test-driven development not the only choice but it can be very useful identify a new feature (e.g., a new method) write minimal code to make the tests pass write 1 or more failing tests for the feature

1. Write JUnit tests for FLinkedList methods

2. Implement the FLinkedList methods, fixing the tests one-by-one

What does this JUnit message tell us? A. FLinkedList passed the test case called testinsertfirst B. all assert statements in testinsertfirst passed C. we have evidence supporting that FLinkedList s insertfirst method is correct D. we have proven FLinkedList s insertfirst method is correct E. the test case called testinsertfirst took 0.002 seconds to run https://b.socrative.com/login/student/ room CS2230X ids 1000-2999 room CS2230Y ids 3000+

What does this JUnit message tell us? A. FLinkedList failed the test called testremovefromlistsize1. B. 50% of our code is correct C. There is a failed assertion at line 37 of FLinkedListTest.java D. There are 37 failed assertions in FLinkedListTest.java E. There is a bug in junit.framework F. An assertion failed because our code returned null, but it was supposed to return a G. An assertion failed because our code returned a, but it was supposed to return null https://b.socrative.com/login/student/ room CS2230X ids 1000-2999 room CS2230Y ids 3000+

FLinkedListTest.java FLinkedList.java

Write JUnit tests for FLinkedList.printList()? writing tests for methods that print to the console is possible but a bit more complicated Instead, let s write a test for a helper method that would be useful to printlist()! specifically, tostring()

== isn t always equal? In Java, == does the expected for primitives. int a = 26; int b = 26; // a == b is true int a = 13; int b = 26; // a == b is false Comparing two references checks if they are pointing to the same object Patient p1 = new Patient("Marion", 100); Patient p2 = new Patient("Marion", 100); Patient p3 = p1; // p1 == p2 is false // p1 == p3 is true Not pointing to the same object? not ==

The equals() method We decide that two Patients are equal() when they have the same name and height the code that does that... public boolean equals(object o) { if (o instanceof Patient){ Patient op = (Patient) o; return this.height==op.height && this.name.equals(op.name); } } else { return false; } Every Java class already has an invisible equals method defined. But you have to override it with your own if you want to do something smarter like compare the fields. String.equals compares the content of the String instead of references Secondary new things in this snippet of code instanceof to check if o is a Patient casting o from Object to Patient

Peer instruction boolean equals(object o) { if (o instanceof Cat) { Cat c = (Cat) o; return this.breed.equals(o.breed); } return false; } Object o1 = new Object(); Object o2 = new Cat( Siamese ); Cat o3 = new Cat( Tabby ); Cat o4 = new Cat( Siamese ); Cat o5 = o2; Which are true statements? A. o1 == o2 B. o2 == o4 C. o2 == o5 D. o4==o5 E. o2.equals(o4) F. o2.equals(o5) G. o4.equals(o5) H. o2.equals(o3) https://b.socrative.com/login/student/ room CS2230X ids 1000-2999 room CS2230Y ids 3000+

Today s big ideas 2 Testing you should write tests JUnit helps you organize and run your tests a look at test driven development Different kinds of equality in Java