05-exceptions-testing

05-exceptions-testing - CSE 143 Lecture 5 More ArrayIntList;

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
CSE 143 Lecture 5 More ArrayIntList ; Pre/postconditions; exceptions; testing slides created by Marty Stepp http://www.cs.washington.edu/143/
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Convenience methods • Implement the following methods: – indexOf - returns the first index an element is found, or -1 if not – isEmpty - returns true if list has no elements – contains - returns true if the list contains the given int value • Why do we need isEmpty and contains when we already have indexOf and size ? – These methods provide convenience to the client of our class. if (myList.size() == 0) { if (myList.isEmpty()) { if (myList.indexOf(42) >= 0) { if (myList.contains(42)) {
Background image of page 2
3 More ArrayIntList • Let's add some new features to our ArrayIntList class: 1. A method that allows client programs to print a list's elements 2. A constructor that accepts an initial capacity (By writing these we will recall some features of objects in Java.) • Printing lists: You may be tempted to write a print method: // client code ArrayIntList list = new ArrayIntList(); ... list.print(); – Why is this a bad idea? What would be better?
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 The toString method • Tells Java how to convert an object into a String ArrayIntList list = new ArrayIntList(); System.out.println("list is " + list ); // ("list is " + list.toString()); •Syntax: public String toString() { code that returns a suitable String ; } • Every class has a toString , even if it isn't in your code. – The default is the class's name and a hex (base-16) number: ArrayIntList@9e8c34
Background image of page 4
5 toString solution // Returns a String representation of the list. public String toString() { if (size == 0) { return "[]"; } else { String result = "[" + elementData[0]; for (int i = 1; i < size; i++) { result += ", " + elementData[i]; } result += "]"; return result; } }
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 Multiple constructors • existing constructor:
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/04/2011 for the course CSE 143 taught by Professor Sr during the Spring '08 term at University of Washington.

Page1 / 18

05-exceptions-testing - CSE 143 Lecture 5 More ArrayIntList;

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online