Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

this should take you no more than 15 mins and Junit test provided. We often need

ID: 3605457 • Letter: T

Question

this should take you no more than 15 mins and Junit test provided.

We often need to order lists of individuals. When this is done in countries using the Latin alphabet, this ordering is usually alphabetically. But there is no requirement that it be done this way and this practice differs in countries whose language does not use an ordered alphabet (such as Japanese, Korean, and Chinese). This suggests that we could make a Name class Comparable, but that we look for a more universal approach to its ordering. To start this problem, complete the Name class so that instances will be ordered by their name's LENGTH (e.g., the sum of the length of their first name and the length of their last name).

Even still, there are situations where we might want to order names in the traditional Western manner. Since it is very commonly used, this other ordering is perfect for a Comparator. Write a NameComparator class that can be used to order Nameinstances by their LAST name.

------------------------------------------------------

Name Class

-----------------------------------

test

package edu.buffalo.cse116;

import static org.junit.Assert.assertTrue;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

import org.junit.Before;
import org.junit.Test;

/**
* JUnit test cases for your class implementing the insertion sort.
*
* @author Matthew Hertz
*/
public class CompTest {
/** Values which we will be adding to the list. */
private static final String[] data = { "Alpha", "Bravo", "Charlie", "Delta", "Echo", "Foxtrot", "Golf", "Hotel" };

/** How to order the Strings in data if we just look at their length. */
private static final int[] lengthOrder = { 12, 12, 9, 11, 11, 9, 10, 2 };

/** Instance of list which holds the names your comparators will check. */
private List<Name> arrayList;

/**
* Test that your sort code works when it gets a list of interesting first names.
*/
@Test
public void testLastNameComparison() {
Comparator<Name> comp = new NameComparator();
// Generate the list to sort.
generateOrderedLastNameList(arrayList, data.length);
// Loop through each Name in the list
for (int i = 0; i < arrayList.size(); i++ ) {
// Loop through each Name in the list
for (int j = 0; j < arrayList.size(); j++ ) {
if (i < j) {
assertTrue("Comparator incorrectly orders " + arrayList.get(i) + " and " + arrayList.get(j),
comp.compare(arrayList.get(i), arrayList.get(j)) < 0);
} else if (i == j) {
assertTrue("Comparator incorrectly orders " + arrayList.get(i) + " and " + arrayList.get(j),
comp.compare(arrayList.get(i), arrayList.get(j)) == 0);
} else {
assertTrue("Comparator incorrectly orders " + arrayList.get(i) + " and " + arrayList.get(j),
comp.compare(arrayList.get(i), arrayList.get(j)) > 0);
}
}
}
}

/**
* Test that your sort code works when it gets a list of interesting first names.
*/
@Test
public void testNameComparable() {
// Generate the list to sort.
generateOrderedFirstNameList(arrayList, data.length);
// Loop through each Name in the list
for (int i = 0; i < arrayList.size(); i++ ) {
// Loop through each Name in the list
for (int j = 0; j < arrayList.size(); j++ ) {
if (lengthOrder[i] < lengthOrder[j]) {
assertTrue("compareTo() incorrectly orders " + arrayList.get(i).getLastName() + " and " +
arrayList.get(j).getLastName(), arrayList.get(i).compareTo(arrayList.get(j)) < 0);
} else if (lengthOrder[i] == lengthOrder[j]) {
assertTrue("compareTo() incorrectly orders " + arrayList.get(i).getLastName() + " and " +
arrayList.get(j).getLastName(), arrayList.get(i).compareTo(arrayList.get(j)) == 0);
} else {
assertTrue("compareTo() incorrectly orders " + arrayList.get(i).getLastName() + " and " +
arrayList.get(j).getLastName(), arrayList.get(i).compareTo(arrayList.get(j)) > 0);
}
}
}
}

/**
* Initialize theList so that it contains exactly n items in sorted order.
*
* @param list List of Names to which we are adding data.
* @param n Number of elements to add to the list.
*/
private void generateOrderedFirstNameList(List<Name> list, int n) {
// Make sure the list is empty.
list.clear();
// Now add the proper amount of data to the list.
for (int i = 1; i < n; i++ ) {
list.add(new Name(data[i], data[(i + 1) % data.length]));
}
}

/**
* Initialize theList so that it contains exactly n items in reverse order.
*
* @param list List of Names to which we are adding data.
* @param n Number of elements to add to the list.
*/
private void generateOrderedLastNameList(List<Name> list, int n) {
// Make sure the list is empty.
list.clear();
// Now add the proper amount of data to the list.
for (int i = 0; i < n; i++ ) {
list.add(new Name(data[n - (i + 1)], data[i]));
}
}

/**
* Create the List instance which your insertion sort will sort.
*/
@Before
public void setUp() {
arrayList = new ArrayList<>();
}
}

Explanation / Answer

/*

Created the NameComparator class to compare two Name objects based on the lastName field

*/

package edu.buffalo.cse116;

class NameComparator implements Comparator<Name>{

private int priority;

private String desciption;

@Override

public int compare(Name n1, Name n2) {

return n1.getLastName().compareTo(n2.getLastName());

}

}

/*

And here is the Name class with mising compareTo function implemented based on firstName and lastName combined length

*/

package edu.buffalo.cse116;

class Name implements Comparable<Name> {

/**

* First, often referred to as &quot;Christian&quot;, name of the person.

*/

private String lastName;

/**

* Last or family name of the person.

*/

private String firstName;

/**

* Create a new instance of Name with the given first and last names.

*

* @param f First name of the person.

* @param l Last name of the person.

*/

public Name(String f, String l) {

firstName = f;

lastName = l;

}

/**

* Get the first name for this person.

*

* @return Person's Christian name.

*/

public String getFirstName() {

return firstName;

}

/**

* Get the last name for this person.

*

* @return Person's family name.

*/

public String getLastName() {

return lastName;

}

@Override

public String toString() {

StringBuilder sb = new StringBuilder(lastName);

sb.append(firstName);

sb.setLength(20);

return sb.toString();

}

@Override

public int compareTo(Name n){

return (this.firstName + this.lastName).length() - (n.firstName + n.lastName).length();

}

}

/**

Rest of the code as it is

*/

//test

package edu.buffalo.cse116;

import static org.junit.Assert.assertTrue;

import java.util.ArrayList;

import java.util.Comparator;

import java.util.List;

import org.junit.Before;

import org.junit.Test;

/**

* JUnit test cases for your class implementing the insertion sort.

*

* @author Matthew Hertz

*/

class CompTest {

/** Values which we will be adding to the list. */

private static final String[] data = { "Alpha", "Bravo", "Charlie", "Delta", "Echo", "Foxtrot", "Golf", "Hotel" };

/** How to order the Strings in data if we just look at their length. */

private static final int[] lengthOrder = { 12, 12, 9, 11, 11, 9, 10, 2 };

/** Instance of list which holds the names your comparators will check. */

private List<Name> arrayList;

/**

* Test that your sort code works when it gets a list of interesting first names.

*/

@Test

public void testLastNameComparison() {

Comparator<Name> comp = new NameComparator();

// Generate the list to sort.

generateOrderedLastNameList(arrayList, data.length);

// Loop through each Name in the list

for (int i = 0; i < arrayList.size(); i++ ) {

// Loop through each Name in the list

for (int j = 0; j < arrayList.size(); j++ ) {

if (i < j) {

assertTrue("Comparator incorrectly orders " + arrayList.get(i) + " and " + arrayList.get(j),

comp.compare(arrayList.get(i), arrayList.get(j)) < 0);

} else if (i == j) {

assertTrue("Comparator incorrectly orders " + arrayList.get(i) + " and " + arrayList.get(j),

comp.compare(arrayList.get(i), arrayList.get(j)) == 0);

} else {

assertTrue("Comparator incorrectly orders " + arrayList.get(i) + " and " + arrayList.get(j),

comp.compare(arrayList.get(i), arrayList.get(j)) > 0);

}

}

}

}

/**

* Test that your sort code works when it gets a list of interesting first names.

*/

@Test

public void testNameComparable() {

// Generate the list to sort.

generateOrderedFirstNameList(arrayList, data.length);

// Loop through each Name in the list

for (int i = 0; i < arrayList.size(); i++ ) {

// Loop through each Name in the list

for (int j = 0; j < arrayList.size(); j++ ) {

if (lengthOrder[i] < lengthOrder[j]) {

assertTrue("compareTo() incorrectly orders " + arrayList.get(i).getLastName() + " and " +

arrayList.get(j).getLastName(), arrayList.get(i).compareTo(arrayList.get(j)) < 0);

} else if (lengthOrder[i] == lengthOrder[j]) {

assertTrue("compareTo() incorrectly orders " + arrayList.get(i).getLastName() + " and " +

arrayList.get(j).getLastName(), arrayList.get(i).compareTo(arrayList.get(j)) == 0);

} else {

assertTrue("compareTo() incorrectly orders " + arrayList.get(i).getLastName() + " and " +

arrayList.get(j).getLastName(), arrayList.get(i).compareTo(arrayList.get(j)) > 0);

}

}

}

}

/**

* Initialize theList so that it contains exactly n items in sorted order.

*

* @param list List of Names to which we are adding data.

* @param n Number of elements to add to the list.

*/

private void generateOrderedFirstNameList(List<Name> list, int n) {

// Make sure the list is empty.

list.clear();

// Now add the proper amount of data to the list.

for (int i = 1; i < n; i++ ) {

list.add(new Name(data[i], data[(i + 1) % data.length]));

}

}

/**

* Initialize theList so that it contains exactly n items in reverse order.

*

* @param list List of Names to which we are adding data.

* @param n Number of elements to add to the list.

*/

private void generateOrderedLastNameList(List<Name> list, int n) {

// Make sure the list is empty.

list.clear();

// Now add the proper amount of data to the list.

for (int i = 0; i < n; i++ ) {

list.add(new Name(data[n - (i + 1)], data[i]));

}

}

/**

* Create the List instance which your insertion sort will sort.

*/

@Before

public void setUp() {

arrayList = new ArrayList<>();

}

}