Computer Science 15-100, Fall 2008
Class Notes: Changes to Course Notes (Additions, Deletions, Errata)
Note: These changes either have been made or will soon be made to
the course notes for this semester. Most have already been discussed in
class and/or in email.
- Do not throw exceptions in equals methods
Wrong:
class Foo
{
public boolean equals(Object object) {
Foo that = (Foo) object;
// crashes if object is not of type
Foo
... compare "this" and "that" ...
}
}
Right:
class Foo
{
public boolean equals(Object object) {
if
((object == null) || (! (object instanceof Foo)))
return false;
Foo that = (Foo) object;
// can safely cast to Foo now!
... compare "this" and "that" ...
}
}
- Using statically-allocated arrays in expressions
The problem:
int[] a =
{ 2, 3, 4 }; // ok
a = { 1, 2, 3 }; // not ok --
will not compile!
The solution:
int[] a =
{ 2, 3, 4 };
a = new int[]{ 1, 2,
3 }; // works!
- Null arrays
Small point, but the notes do not indicate that you can assign "null" to
an array reference, and use equality tests for null, just as you can to any
other object reference:
int[] a =
null;
if (a == null)
System.out.println("yes!");
- Proper use of paint
- Using helper methods in JComponentWithEvents
The key is to pass the Graphics2D parameter. Nearly all of you
did this in your Tetris hw (which is good). But the notes should
show an example of it.
- Improper use of paint
- Do not call paint methods (Swing calls paint for you)
- Do not store the Graphics2D parameter in an instance variable
- Do not modify any instance variables inside any paint methods
- Proper use of Random
- Place the singleton Random instance in a static variable
- Use SecureRandom (slower but "more random")
public
static final Random random = new java.security.SecureRandom();
- Simple accessors and mutators
You all know that for every private instance variable "foo", you should
have a public accessor "getFoo" and, when appropriate, a public mutator "setFoo", but the notes
did not explicitly spell this out. [ As an aside, this is a somewhat
simplistic treatment of private/public, but it is the standard for intro CS
courses. ]
- Instance methods accessing private instance variables of other
instances (that.foo)
Methods in a class have access to all fields (private or otherwise) of
ANY INSTANCE of that class. So this works:
class Foo {
private int x;
public int compareTo(Object object) {
Foo that = (Foo) object;
return this.x -
that.x;
}
}
- insertionsort with a while loop
We presented insertionsort with a for loop, but given that the inner
loop counts down rather than up and also that it has a mildly complicated
termination test, some people prefer to write that inner loop using a while
loop:
public static
void insertionsort(int[] a) {
int n = a.length;
for (int i=0; i<n; i++) {
int j=i;
while (j>0 && a[j] < a[j-1]) {
swap(a, j, j-1);
j--;
}
}
}
We'll include both approaches in the notes.
- Useful web sites
- Flag design
This site is so useful for flag design hw's -- it gives detailed
scale and layout information and specific RGB values for the flags.
For example, check out:
http://www.vexilla-mundi.com/bahamas_flag.html
We'll include this reference in future flag-based hw problems.
- Animated sorting algorithms:
http://vision.bc.edu/~dmartin/teaching/sorting/anim-html/all.html
- Others?
If you have other suggestions, we'd love to hear them! Thanks!
- Pending Additions:
- Using Monte Carlo Methods
Rather than explain this in hw4, make an explicit note that explains
it, including how to count the # of trials, the # of successes, and
compute the expected value as the ratio of these.
- Writing 2D Board Games
Prior to the Connect4 (or similar) assignment, present a framework
for a 2D board game, showing how to store the board in a 2d array, how
to do the traditional nested for loops to traverse the board, how to
display a board using
- Using Swing
Perhaps add a note describing some basics of Swing (buttons,
menus, textfields, layouts, etc).
- Deployment (jars, resources, applications, applets)
This has been much-requested. We will add a note that
demonstrates a simple program that reads in a text file, an image file
(a jpeg), and music files (midi and wav files), running in 3 modes:
"dev" mode (where it is unpackaged, but still has access to its
resources), packaged as an application jar file, and packaged as a
web-based applet jar file. This note may note be completed until
after the final exam this semester, but we will post it to the course
mailing list when it is available.
- Pending Deletions:
- Reduce/eliminate chapter 1 topics
- Under Review
- JCF with or without generics (ArrayList vs
ArrayList<String>)?
- Term project?
- Pending Update to JComponentWithEvents
We will release the next version of JComponentWithEvents within a week
or two. We will send an email to the class list at that time (that
will be the final email to the class list, in fact). The changes will
include:
- javadoc html documentation
- version checking in background (for fast launching)
- no version or field-modification-in-paint checking in applet mode
- more key constants
- improved exception handling
(better handling of file+reflection security exceptions in applet
mode)
- modified launch method (less "magical")
- resources (images and music files)
- drawing images
- adding buttons, textFields, and other Swing objects
- 2d board game support?
- Other changes?
Are there other features you would like to see in
JComponentWithEvents? Please let us know! Thanks!
- Other changes?
We are always seeking ways to improve this course -- both in terms of
how effectively we meet the learning objectives of the course, as well as
how well we meet your individual needs and interests. To that end, we
very much welcome any suggestions you might have on how to modify the
course, from smaller changes to individual notes to larger changes regarding
our entire approach. Please let us know your thoughts! Thanks!!!
carpe diem -
carpe diem - carpe diem - carpe diem
- carpe diem - carpe diem -
carpe diem - carpe diem - carpe
diem