The rest of the problem is here: https://www.cs.princeton.edu/courses/archive/fa
ID: 3573591 • Letter: T
Question
The rest of the problem is here:
https://www.cs.princeton.edu/courses/archive/fall16/cos226/assignments/autocomplete.html
Write an immutable data type Term. java that represents an autocomplete term: a query string and an associated integer weight. You must implement the following API, which supports comparing terms by three different orders: lexicographic order by query string (the natural order): in descending order by weight (an alternate order): and lexicographic order by query string but using only the first r characters (a family of alternate orderings). The last order may seem a bit odd, but you will use it in Part 3 to find all query strings that start with a given prefix (of length r). public class Tern lmplements Comparable {//Initializes a term with the given query string and weight. public Term(String Query, long weight)//Compares two terms in descending order by weight public static Comparator byReverseweightOrder()//compares the two terms in lexicographic order hut using only the first r characters of each query. public static Comparator byprefixorder (int r)//Compares the two terms in lexicographic order by query public int compareto (Term that)//Returns a string representation of this tern in the following format://the weight, followed by a tab, followed by the query, public String toString()//unit testing (required) public static void main(String[] arga)} The string comparison functions should take time proportional to the number of characters needed to resolve the comparison.Explanation / Answer
import java.util.*;
import java.lang.*;
import java.io.*;
class Term implements Comparable<Term> {
private String query;
private long weight;
// Initializes a term with the given query string and weight.
public Term(String query, long weight) {
if(query == null)
throw new NullPointerException("Query string cannot be null.");
if(weight < 0)
throw new IllegalArgumentException("Weight cannot be negative.");
this.query = query;
this.weight = weight;
}
// Compares the two terms in descending order by weight.
public static Comparator<Term> byReverseWeightOrder() {
return new Comparator<Term>() {
@Override
public int compare(Term o1, Term o2) {
return (int) (o2.weight - o1.weight);
}
};
}
// Compares the two terms in lexicographic order but using only the first r characters of each query.
public static Comparator<Term> byPrefixOrder(int r) {
if(r < 0)
throw new IllegalArgumentException("Prefix cannot be negative");
return new Comparator<Term>() {
@Override
public int compare(Term o1, Term o2) {
return o1.query.substring(0,r).compareTo(o2.query.substring(0,r));
}
};
}
// Compares the two terms in lexicographic order by query.
public int compareTo(Term that) {
return this.query.compareTo(that.query);
}
// Returns a string representation of this term in the following format:
// the weight, followed by a tab, followed by the query.
public String toString() {
return weight+" "+query;
}
// unit testing (required)
public static void main(String[] args) {
List<Term> termList = new ArrayList<>();
Term t1 = new Term("AA",1);
Term t2 = new Term("BAC",4);
Term t3 = new Term("WWA",2);
Term t4 = new Term("B",10);
termList.add(t1);
termList.add(t2);
termList.add(t3);
termList.add(t4);
Collections.sort(termList,byReverseWeightOrder());
for(Term term : termList)
System.out.println(term);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.