Example code to look at:
- Date and Calendar example code
- Compare using comparator class
- Compare using comparable interface on the things you are comparing
- Example of secondary sort on an object using compartor
For this part please use the Date Format mask:
"yyyy-MM-dd HH:mm:ss Z"
- Print the current date (just do
new Date()
) using the above format mask (useSimpleDateFormat
). - Use the
GregorianCalender
to set the date to one month, one day, and one hour ago. Print this date out. - Read in a timestamp from the user (use Scanner, it will be a
long
, so use.nextLong()
). Create aDate
Object from this, and print it out formatted, with the above format mask.
Note: Date class has a constructor that takes a timestamp. So you can do a thing like this:
long timestamp = scanner.nextLong();
Date usersDate = new Date(timestamp);
- Create a class called
Lizard
. If should have 2 attributes:
public String name;
public float age; // in years.
- Implement Comparable on the Lizard. You must compare it to another Lizard (see
Cat.java
for clues). - In the
compareTo()
method you implement on lizard do the following: - Primary Sort by name (Ascending)
- Secondary Sort by age (Ascending)
- Make another class, and use it to create 10 lizards. Some of them must have the SAME NAME, but DIFFERENT AGES. We are going to use this learn how to do a secondary sort. Add all of the Lizards to a single
List<Lizard>
. - Print out the unsorted list. The
toString()
on Lizard MUST display the name and age. - Call
Collections.sort()
on the list of Lizards. - Print out the sorted list. Your lizards must be sorted in Ascending order by name. Your lizards that had the same name must be secondarily sorted by AGE!
Part 1 is worth 30 points. Part 2 is worth 70 points. Please see the example code, there will be examples of how to do everything you need to do. Your job is to understand it and put it all together.