The Department of Computer Science & Engineering
cse@buffalo
STUART C. SHAPIRO: CSE 116 B

CSE 116
Introduction To Computer Science for Majors 2
Lecture B
Lecture Notes
Stuart C. Shapiro
Spring, 2003


ADTs and Coding Style

Reading
Riley, Chapters O1, O2, O3, 1

Preconditions, Errors, Assertions, Exceptions
Read and run BugDemoApp, AssertDemoApp, and ExceptionDemoApp from Demos/AssertDemo as both a developer and a user.

Moral:
To use assertions: Compare the source code with the documentation.
To create API documentation:
  1. Create a subdirectory named doc.
  2. Execute javadoc -source 1.4 -d doc -author *.java
See javadoc description.

Common Object methods
toString, equals, and clone are inherited by every Java class from Object. (We won't discuss clone.)
compareTo can be inherited from the Comparable interface.

Different kinds of equality
Try in the BeanShell:
x = "abc";
y = x;
print(x == y);

print(x == "abc");
print(x.equals("abc"));
Define your own equals in your classes.
Example: PersonName

Answer to a question asked in class:
instanceof is a boolean-valued operator, for example,
if (x instanceof String) {System.out.println("Yes");}

getClass()is a method of java.lang.Object that returns an object of class java.lang.Class, which is the class of the object, for example,
System.out.println("x is of the class " + x.getClass());

and getName() is a method of java.lang.Class, which returns a String, which represents the Class, for example,
System.out.println("x is of the class " + x.getClass().getName());

So, you can do x.getClass() == y.getClass() to see if x and y are of the same class.

Use of compareTo(Object)
Try in the BeanShell:
print(x.compareTo("bsh"));
print(x.compareTo(y));
print(x.compareTo("aaa"));
Define your own compareTo(Object) in your classes, but add implements Comparable.
Example: PersonName

First Previous Next

Copyright © 2003 by Stuart C. Shapiro. All rights reserved.

Stuart C. Shapiro <shapiro@cse.buffalo.edu>