Preconditions precondition: Something your method assumes is true at the start of its execution. Often documented as a comment on the method's header:

Slides:



Advertisements
Similar presentations
Chapter 17 Failures and exceptions. This chapter discusses n Failure. n The meaning of system failure. n Causes of failure. n Handling failure. n Exception.
Advertisements

Exceptions CSE301 University of Sunderland Harry Erwin, PhD.
CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
Lecture 23 Input and output with files –(Sections 2.13, 8.7, 8.8) Exceptions and exception handling –(Chapter 17)
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
16-Jun-15 Exceptions. Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a null.
Exceptions. Errors and Exceptions An error is a bug in your program –dividing by zero –going outside the bounds of an array –trying to use a null reference.
Java Review 2 – Errors, Exceptions, Debugging Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Building Java Programs
Building Java Programs
Building Java Programs Chapter 15 Implementing a Collection Class: ArrayIntList Copyright (c) Pearson All rights reserved.
CSE 143 Lecture 2 More ArrayList ; classes and objects reading: 10.1; slides created by Marty Stepp and Hélène Martin
CSE 143 Lecture 4 ArrayList Reading: 10.1 slides created by Marty Stepp
Copyright 2008 by Pearson Education Building Java Programs Chapter 10 Lecture 10-1: ArrayList reading: 10.1.
CHAPTER 5 ArrayLists.
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
Stacks and Queues. 2 3 Runtime Efficiency efficiency: measure of computing resources used by code. can be relative to speed (time), memory (space), etc.
CSE 143 Lecture 2 ArrayList reading: 10.1 slides created by Marty Stepp
Copyright 2010 by Pearson Education Building Java Programs Chapter 10 Lecture 21: ArrayList reading: 10.1.
Computer Science 209 Software Development Handing Errors and Creating Documentation.
CSE 143 Lecture 2 More ArrayList ; classes and objects reading: 10.1; slides created by Marty Stepp and Hélène Martin
CSE 143 Lecture 4 Exceptions and ArrayList slides created by Marty Stepp
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:
Today’s lecture Review of chapter 8 Go over examples.
Exceptions an unusual condition – e.g. division by zero – e.g. file doesn't exist – e.g. illegal type – etc. etc… typically a run-time error – i.e. during.
Stacks and Queues. 2 Abstract Data Types (ADTs) abstract data type (ADT): A specification of a collection of data and the operations that can be performed.
slides created by Marty Stepp
Collections.
The Class ArrayLinearList
Introduction to Exceptions in Java
Building Java Programs
Week 2: 10/1-10/5 Monday Tuesday Wednesday Thursday Friday
Welcome to CSE 143!.
this keyword this : A reference to the implicit parameter Syntax:
this keyword this : A reference to the implicit parameter Syntax:
Lecture 2: Implementing ArrayIntList reading:
Programming in Java Lecture 11: ArrayList
CSE 143 Lecture 4 ArrayList Reading: 10.1.
Defining New Types of Objects, part 3
ArrayLists.
slides created by Ethan Apter
Object initialization: constructors
Words exercise Write code to read a file and display its words in reverse order. A solution that uses an array: String[] allWords = new String[1000]; int.
CSE 143 Lecture 1 Arrays (review); ArrayList reading: 10.1
Chapter 14 A List ADT.
Chapter 10 ArrayList reading: 10.1
Welcome to CSE 143!.
Lecture 1: ArrayList reading: 10.1
slides created by Ethan Apter
Grouped Data Arrays, and Array Lists.
CSE 143 Java Exceptions 1/18/2019.
CSE 143 Lecture 4 More ArrayIntList:
Lecture 13: ArrayLists AP Computer Science Principles
ArrayLists 22-Feb-19.
CSE 143 Lecture 2 More ArrayList; classes and objects
Go to pollev.com/cse143.
CSE 143 Lecture 5 More ArrayIntList:
CSE 373 Implementing a Stack Reading: Weiss Ch. 3; 3.6; 1.5
slides created by Marty Stepp
Building Java Programs
slides created by Alyssa Harding
Introduction to Programming
Building Java Programs
Exceptions 10-May-19.
slides created by Marty Stepp
CSE 143 Lecture 6 interfaces; eclipse; testing
slides created by Ethan Apter and Marty Stepp
CSE 143 Lecture 21 Advanced List Implementation
Presentation transcript:

Preconditions precondition: Something your method assumes is true at the start of its execution. Often documented as a comment on the method's header: // Returns the element at the given index. // Precondition: 0 <= index < size public int get(int index) { return elementData[index]; } Stating a precondition doesn't really "solve" the problem, but it at least documents our decision and warns the client what not to do. What if we want to actually enforce the precondition?

Throwing exceptions (4.4) throw new ExceptionType(); throw new ExceptionType("message"); Generates an exception that will crash the program, unless it has code to handle ("catch") the exception. Common exception types: ArithmeticException, ArrayIndexOutOfBoundsException, FileNotFoundException, IllegalArgumentException, IllegalStateException, IOException, NoSuchElementException, NullPointerException, RuntimeException, UnsupportedOperationException Why would anyone ever want a program to crash?

Postconditions postcondition: Something your method promises will be true at the end of its execution. Often documented as a comment on the method's header: // Precondition : size() < capacity // Postcondition: value is added at the end of the list public void add(int value) { elementData[size] = value; size++; } If your method states a postcondition, clients should be able to rely on that statement being true after they call the method.

this keyword this : A reference to the implicit parameter Syntax: (the object on which a method/constructor is called) Syntax: To refer to a field: this.field To call a method: this.method(parameters); To call a constructor this(parameters); from another constructor:

ArrayList of primitives? The type you specify when creating an ArrayList must be an object type; it cannot be a primitive type. // illegal -- int cannot be a type parameter ArrayList<int> list = new ArrayList<int>(); But we can still use ArrayList with primitive types by using special classes called wrapper classes in their place. // creates a list of ints ArrayList<Integer> list = new ArrayList<Integer>();

Wrapper classes Primitive Type Wrapper Type int Integer double Double char Character boolean Boolean A wrapper is an object whose sole purpose is to hold a primitive value. Once you construct the list, use it with primitives as normal: ArrayList<Double> grades = new ArrayList<Double>(); grades.add(3.2); grades.add(2.7); ... double myGrade = grades.get(0);

Tips for testing You cannot test every possible input, parameter value, etc. 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 test behavior in combination Maybe add usually works, but fails after you call remove Make multiple calls; maybe size fails the second time only