1 |
Handout A for e02 |
CS56 M16 |
Handout A, p. 1: Useful Reference Items
You may find it useful to know that the code String.format("%.2f", gpa)
will format a double
variable gpa
with exactly two places
after the decimal point.
In addition, here are a few reminders of things we discussed in class, but that you might reasonably need a “reference” for if you were using them in the real world.
The interface java.util.Comparator<T>
includes the following
method signature:
int |
compare(T o1, T o2) |
Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object. |
The interface java.lang.Comparable<T>
includes the following
method signature:
int |
compareTo(T o) |
Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second. |
The class java.util.ArrayList<E>
includes this method:
void |
sort(Comparator<? super E> c) |
Sorts this list according to the order induced by the specified Comparator . |
The class java.lang.Math
has the following method for converting the
double values into -1.0, 0.0 and 1.0 based on their sign. Type casting could be used to convert the
returned double
value to an int
value.
static double |
signum(double d) |
Returns the signum function of the argument; zero if the argument is zero, 1.0 if the argument is greater than zero, -1.0 if the argument is less than zero. |
The class java.util.Collections
contains the following static method:
static <T extends Comparable<? super T>> void | sort(List |
Sorts the specified list into ascending order, according to the natural ordering of its elements. |
And finally, java.lang.String
implements Comparable<String>
in the
way that you would expect.
Handout A, p. 2: Sample Output
$ java StudentMain
Student(6234567,"Bob",3.25,CMPSC)
Student(7152353,"Charlie",3.96,CMPSC)
Student(2352353,"Alice",3.97,CMPEN)
Student(2888888,"Danielle",4.00,CMPSC)
Student(1152353,"Ed",2.90,CMPEN)
$ java StudentMain name
Student(2352353,"Alice",3.97,CMPEN)
Student(6234567,"Bob",3.25,CMPSC)
Student(7152353,"Charlie",3.96,CMPSC)
Student(2888888,"Danielle",4.00,CMPSC)
Student(1152353,"Ed",2.90,CMPEN)
$ java StudentMain majorName
Student(2352353,"Alice",3.97,CMPEN)
Student(1152353,"Ed",2.90,CMPEN)
Student(6234567,"Bob",3.25,CMPSC)
Student(7152353,"Charlie",3.96,CMPSC)
Student(2888888,"Danielle",4.00,CMPSC)
$ java StudentMain gpaDesc
Student(2888888,"Danielle",4.00,CMPSC)
Student(2352353,"Alice",3.97,CMPEN)
Student(7152353,"Charlie",3.96,CMPSC)
Student(6234567,"Bob",3.25,CMPSC)
Student(1152353,"Ed",2.90,CMPEN)
$ java StudentMain perm
Student(1152353,"Ed",2.90,CMPEN)
Student(2352353,"Alice",3.97,CMPEN)
Student(2888888,"Danielle",4.00,CMPSC)
Student(6234567,"Bob",3.25,CMPSC)
Student(7152353,"Charlie",3.96,CMPSC)
$