For this homework, you will explore using the Comparable
and Comparator
interfaces, and anonymous inner classes, non-static inner classes, and static nested classes to sort files by different properties.
Your search engine project will need to rank search results such that the most relevant results are listed first.
There are many ways to enable sorting of custom objects. This homework helps you understand these different approaches, and hopefully decide on an approach appropriate for your project.
<aside> <img src="/icons/git_gray.svg" alt="/icons/git_gray.svg" width="40px" /> This homework assignment provides an example of how a custom object may be sorted. You only need to choose ONE approach for your project... choose the simplest one to start!
</aside>
Below are some hints that may help with this homework assignment:
Focus first on implementing the Comparable
interface for the FileMetadata
static nested class. You will need to modify the declaration of this class and add method(s) for this part.
<aside>
<img src="/icons/warning_gray.svg" alt="/icons/warning_gray.svg" width="40px" /> There will be at least one warning in the test code until you properly implement the Comparable
interface!
</aside>
You may add additional classes, methods, members, and/or annotations to the FileSorter
class as needed.
The Comparator
members require you to complete 2 steps:
Create either an anonymous inner class, non-static inner class, or static nested class. For example:
public class ExampleClass ...
Initialize an instance of that class and assign it to the required variable. For example:
public static final Object YOUR_VARIABLE = new ExampleClass();
Remember, to initialize an instance of a non-static inner class, you need an instance of the outer class first. See the Java Tutorials: Nested Classes tutorial for examples. One example they provide is:
OuterClass outerObject = new OuterClass();
OuterClass.InnerClass innerObject = outerObject.new InnerClass();
This can be combined into a single line as follows:
OuterClass.InnerClass example = new OuterClass().new InnerClass();
For the anonymous inner class, make sure to declare, define, and instantiate the class within the return
statement (otherwise you may get Javadoc warnings). For example:
return new ExampleClass(...) { ... };
These hints are optional. There may be multiple approaches to solving this homework.