CSE 143 Lecture 6 interfaces; eclipse; testing

Slides:



Advertisements
Similar presentations
CSE 143 Lecture 4: testing and complexity reading:
Advertisements

CSE 143 Lecture 3 Inheritance slides created by Marty Stepp
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.
Building Java Programs Chapter 15 Implementing a Collection Class: ArrayIntList Copyright (c) Pearson All rights reserved.
CSE 143 Lecture 9 Interfaces; Stacks and Queues slides created by Marty Stepp
CSE 143 Lecture 6 Inheritance; binary search reading: 9.1, ; 13.1 slides created by Marty Stepp
CSE 143 Lecture 10 Linked List Basics reading: slides created by Marty Stepp
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
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.
CSE 143 Lecture 20 Abstract classes. 2 Circle public class Circle { private double radius; public Circle(double radius) { this.radius = radius; } public.
CSE 143 Lecture 4 More ArrayIntList : Pre/postconditions; exceptions; testing reading: slides created by Marty Stepp and Hélène Martin
Building Java Programs Chapter 15 Lecture 15-2: testing ArrayIntList; pre/post conditions and exceptions reading:
slides adapted from Marty Stepp and Hélène Martin
Building Java Programs Chapter 13 Lecture 13-1: binary search and complexity reading:
CSE 143 Lecture 3 Implementing ArrayIntList reading: slides created by Marty Stepp and Hélène Martin
Building Java Programs Interfaces reading: , 16.4.
CSE 143 Lecture 1 Arrays (review) slides created by Marty Stepp
CSE 143 Lecture 14: testing.
Lecture 21: Interfaces and Abstract classes
CSE 143 Lecture 14 Interfaces; Abstract Data Types (ADTs)
Lecture 13: Interfaces, Comparable reading: , 16.4, 10.2
Building Java Programs
Interfaces.
slides created by Marty Stepp
Data Structures and Algorithms
Preconditions precondition: Something your method assumes is true at the start of its execution. Often documented as a comment on the method's header:
Wednesday Notecards What’s your credit card number?
Lecture 1: Welcome to CSE 373
Interfaces EE 422C.
this keyword this : A reference to the implicit parameter Syntax:
this keyword this : A reference to the implicit parameter Syntax:
slides created by Marty Stepp and Ethan Apter
Interfaces CS163 Fall 2018.
CSE 143 Lecture 6 Interfaces; Complexity (Big-Oh)
Building Java Programs
CSE 143 Lecture 2 Collections and ArrayIntList
Lecture 7: Linked List Basics reading: 16.2
Lecture 5: complexity reading:
slides created by Marty Stepp and Hélène Martin
Shapes Consider the task of writing classes to represent 2D shapes such as Circle, Rectangle, and Triangle. Certain operations are common to all shapes:
CSE 143 Lecture 4 More ArrayIntList:
slides created by Ethan Apter
CSE 143 Lecture 4 Implementing ArrayIntList
slides created by Marty Stepp
Lecture 8: Complex Linked List Code reading: 16.2 – 16.3
Based on slides by Alyssa Harding & Marty Stepp
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.
Go to pollev.com/cse143.
CSE 143 Lecture 3 Implementing ArrayIntList reading:
CSE 143 Lecture 5 More ArrayIntList:
CSE 373 Implementing a Stack Reading: Weiss Ch. 3; 3.6; 1.5
CSE 143 Lecture 4 Implementing ArrayIntList; Binary Search
Building Java Programs
Lecture 20: Interfaces and Abstract classes
CSE 143 Lecture 2 ArrayIntList, binary search
Building Java Programs
slides created by Ethan Apter
slides adapted from Marty Stepp
Building Java Programs
slides created by Marty Stepp
Lecture 7: Linked List Basics reading: 16.2
slides created by Ethan Apter and Marty Stepp
slides created by Marty Stepp
CSE 143 Lecture 21 Advanced List Implementation
slides created by Marty Stepp
Introduction to Methods and Interfaces
Presentation transcript:

CSE 143 Lecture 6 interfaces; eclipse; testing slides adapted from Marty Stepp http://www.cs.washington.edu/143/

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: public interface Vehicle { public int getSpeed(); public void setDirection(int direction);

Interface syntax public class name implements interface name{ ... } Example: public class Car implements Vehicle { … public int getSpeed() { return speed; public void setDirection(int direction) { this.direction = direction;

Writing testing programs Some programs are written specifically to test other programs. If we wrote ArrayIntList and want to give it to others, we must make sure it works adequately well first. Write a client program with a main method that constructs several lists, adds elements to them, and calls the various other methods.

Tips for testing You cannot test every possible input, parameter value, etc. Even a single (int) method has 2^32 different possible values! So you must think of a limited set of tests likely to expose bugs. Think about boundary cases positive, zero, negative numbers right at the edge of an array or collection's size Think about empty cases and error cases 0, -1, null; an empty list or array an array or collection that contains null elements Write helping methods in your test program to shorten it.

More testing tips Focus on expected vs. actual behavior the test shouldn't just call methods and print results; it should: call the method(s) compare their results to a known correct expected value if they are the same, report that the test "passed" if they differ, report that the test "failed" along with the values test behavior in combination maybe add usually works, but fails after you call remove what happens if I call add then size? remove then toString? make multiple calls; maybe size fails the second time only

Example ArrayIntList test public static void main(String[] args) { int[] a1 = {5, 2, 7, 8, 4}; int[] a2 = {2, 7, 42, 8}; int[] a3 = {7, 42, 42}; helper(a1, a2); helper(a2, a3); helper(new int[] {1, 2, 3, 4, 5}, new int[] {2, 3, 42, 4}); } public static void helper(int[] elements, int[] expected) { ArrayIntList list = new ArrayIntList(elements); for (int i = 0; i < elements.length; i++) { list.add(elements[i]; list.remove(0); list.remove(list.size() - 1); list.add(2, 42); for (int i = 0; i < expected.length; i++) { if (list.get(i) != expected[i]) { System.out.println("fail; expect " + Arrays.toString(expected) + ", actual " + list);