Void checkindexint index int min int max if index min

Info icon This preview shows pages 5–12. Sign up to view the full content.

View Full Document Right Arrow Icon
void checkIndex(int index, int min, int max) { if (index < min || index > max) { throw new IndexOutOfBoundsException(index); } }
Image of page 5

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

View Full Document Right Arrow Icon
6 Printing an ArrayIntList • Currently our list class has a print method: // client code ArrayIntList list = new ArrayIntList(); ... list.print(); Why is this a bad idea? What would be better?
Image of page 6
7 The toString method • Tells Java how to convert an object into a String ArrayIntList list = new ArrayIntList(); System.out.println("list is " + list ); • 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: [email protected]
Image of page 7

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

View Full Document Right Arrow Icon
8 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; } }
Image of page 8
9 Exercise • Write a class called StutterIntList . Its constructor accepts an integer stretch parameter. Every time an integer is added, the list will actually add stretch number of copies of that integer. • Example usage: StutterIntList list = new StutterIntList(3); list.add(7); // [7, 7, 7] list.add(-1); // [7, 7, 7, -1, -1, -1] list.add(2, 5); // [7, 7, 5, 5, 5, 7, -1, -1, -1] list.remove(4); // [7, 7, 5, 5, 7, -1, -1, -1] System.out.println(list.getStretch()); // 3
Image of page 9

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

View Full Document Right Arrow Icon
10 Inheritance inheritance : Forming new classes based on existing ones. a way to share/ reuse code between two or more classes superclass : Parent class being extended. subclass : Child class that inherits behavior from superclass. • gets a copy of every field and method from superclass
Image of page 10
11 An Employee class public class Employee { ...
Image of page 11

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

View Full Document Right Arrow Icon
Image of page 12
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern