this keyword this : A reference to the implicit parameter Syntax:

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.
Lecture 23 Input and output with files –(Sections 2.13, 8.7, 8.8) Exceptions and exception handling –(Chapter 17)
Building Java Programs Appendix B
Building Java Programs Chapter 15 Implementing a Collection Class: ArrayIntList Copyright (c) Pearson All rights reserved.
CSE 143 Lecture 4 ArrayList Reading: 10.1 slides created by Marty Stepp
CHAPTER 5 ArrayLists.
Computer Science 209 Software Development Handing Errors and Creating Documentation.
CSE 143 Lecture 4 Exceptions and ArrayList slides created by Marty Stepp
Recitation 3 2D Arrays, Exceptions. 2D arrays 2D Arrays Many applications have multidimensional structures: ●Matrix operations ●Collection of lists ●Board.
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:
MIT AITI 2004 – Lecture 14 Exceptions Handling Errors with Exceptions.
Today’s lecture Review of chapter 8 Go over examples.
Building Java Programs Chapter 13 Lecture 13-1: binary search and complexity reading:
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
CSE 143 Lecture 1 Arrays (review) slides created by Marty Stepp
CSE 143 Lecture 14: testing.
EECE 310: Software Engineering
CSCI 162 – Introduction to Programming II William Killian
The Class ArrayLinearList
Introduction Exception handling Exception Handles errors
Introduction to Exceptions in Java
Dr. Kyung Eun Park Summer 2017
List Representation - Array
Building Java Programs
Building Java Programs
Preconditions precondition: Something your method assumes is true at the start of its execution. Often documented as a comment on the method's header:
SWE 332 Last Modified Spring 2010 Paul Ammann
this keyword this : A reference to the implicit parameter Syntax:
Programming in Java Lecture 11: ArrayList
CSE 143 Lecture 4 ArrayList Reading: 10.1.
Defining New Types of Objects, part 3
Building Java Programs
CSE 143 Lecture 1 Review: Arrays and objects
slides created by Ethan Apter
Object initialization: constructors
CSE 143 Lecture 1 Arrays (review); ArrayList reading: 10.1
Chapter 14 A List ADT.
Building Java Programs
Building Java Programs
Building Java Programs
slides created by Ethan Apter
Can we solve this problem?
CSE 143 Java Exceptions 1/18/2019.
CSE 143 Lecture 4 More ArrayIntList:
slides created by Marty Stepp
Java Exceptions Dan Fleck CS211.
Go to pollev.com/cse143.
Can we solve this problem?
CSE 143 Lecture 5 More ArrayIntList:
CSE 373 Implementing a Stack Reading: Weiss Ch. 3; 3.6; 1.5
Building Java Programs
slides created by Marty Stepp
CSE 143 Lecture 2 ArrayIntList, binary search
Building Java Programs
Building Java Programs
Building Java Programs
File output; Arrays reading: 6.4 – 6.5, 7.1
Why are arrays useful? We can use arrays to store a large amount of data without declaring many variables. Example: Read in a file of 1000 numbers, then.
Exceptions 10-May-19.
Can we solve this problem?
Building Java Programs
CSE 143 Lecture 6 interfaces; eclipse; testing
CSE 143 Lecture 4 Implementing ArrayIntList reading:
slides created by Ethan Apter and Marty Stepp
Building Java Programs Appendix B
CSE 143 Lecture 1 Arrays (review); ArrayList reading: 10.1
Implementing ArrayIntList
Presentation transcript:

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:

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?

The Arrays class The Arrays class in java.util has many useful methods: Syntax: Arrays.methodName(parameters) Method name Description binarySearch(array, value) returns the index of the given value in a sorted array (or < 0 if not found) binarySearch(array, minIndex, maxIndex, value) returns index of given value in a sorted array between indexes min /max - 1 (< 0 if not found) copyOf(array, length) returns a new resized copy of an array equals(array1, array2) returns true if the two arrays contain same elements in the same order fill(array, value) sets every element to the given value sort(array) arranges the elements into sorted order toString(array) returns a string representing the array, such as "[10, 30, -25, 17]"

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: // Makes sure that this list's internal array is large // enough to store the given number of elements. // Postcondition: elementData.length >= capacity public void ensureCapacity(int capacity) { // double in size until large enough int newCapacity = elementData.length; while (capacity > newCapacity) { newCapacity = newCapacity * 2; } elementData = Arrays.copyOf(elementData, newCapacity); If your method states a postcondition, clients should be able to rely on that statement being true after they call the method.

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