Question

In: Computer Science

Write a class IgnoreCaseComparator that implements Comparator<Character>. If it were used as the argument to List.sort(),...

Write a class IgnoreCaseComparator that implements Comparator<Character>. If it were used as the argument to List.sort(), it would sort the list ignoring case. This behavior is different from using the natural ordering of characters, where uppercase characters come before lowercase characters.

Note that this question is not asking you to sort a list -- just to write the comparator!

For example, if the unsorted list were [b, C, A, d], then the list would be [A, b, C, d] after sorting using this comparator. (If you had sorted using the natural ordering of characters, the result would instead have been[A, C, b, d].)

You may find a utility method of Character, such as Character.toLowerCase(), helpful.

You may assume that Comparator and Character are imported. You may not import other classes.

Solutions

Expert Solution

Ok so the required code is attached below.

If you get any doubt feel free to drop that in the comment section.

Note: Code consist of a full program with the required (IgnoreCaseComparator) class and a main class just for testing the code. I know the full program is not demanded. but just for the test . you can easily copy the required class.

Code:


import java.util.*;

class IgnoreCaseComparator implements Comparator<Character>{

   @Override
   public int compare(Character o1, Character o2) {
       // TODO It compares the character in lowercase form
       return Character.toLowerCase(o1)-Character.toLowerCase(o2);
   }
  
}


public class Main{
  
   public static void main(String[] args) {
       //For testing
       ArrayList<Character> List=new ArrayList<Character>();
       List.add('a');
       List.add('B');
       List.add('D');
       List.add('c');
       List.sort(new IgnoreCaseComparator());
       System.out.println(List);
      
   }
}


Related Solutions

In Java class PassengerComparator implements Comparator for a Passenger Implement a comparator where a FirstClassPassenger precedes...
In Java class PassengerComparator implements Comparator for a Passenger Implement a comparator where a FirstClassPassenger precedes a CoachPassenger,. If both Passengers are of the same TicketClass then the Passenger with the lower TicketNumber precedes the Passenger with the higher TicketNumber.
In Python, write a function one_bit_NOT that takes one argument, a character that is either '0'...
In Python, write a function one_bit_NOT that takes one argument, a character that is either '0' or '1'. It should perform the NOT operation and return a string with a single character as the result. I.e., if the character argument is "0", it returns a "1"'. If the character argument is "1", it returns a "0".
In this assignment, you will write a class that implements a contact book entry. For example,...
In this assignment, you will write a class that implements a contact book entry. For example, my iPhone (and pretty much any smartphone) has a contacts list app that allows you to store information about your friends, colleagues, and businesses. In later assignments, you will have to implement this type of app (as a command line program, of course.) For now, you will just have to implement the building blocks for this app, namely, the Contact class. Your Contact class...
please solve using jupyter notebook . 10.9- (Square Class) Write a class that implements a Square...
please solve using jupyter notebook . 10.9- (Square Class) Write a class that implements a Square shape. The class should contain a side property. Provide an __init__ method that takes the side length as an argument. Also, provide the following read-only properties: a) perimeter returns 4 × side. b) area returns side × side. c) diagonal returns the square root of the expression (2 × side2). The perimeter, area and diagonal should not have corresponding data attributes; rather, they should...
Write a class "LinkedBag" which implements this interface. The LinkedBag class has two instance variables firstNode...
Write a class "LinkedBag" which implements this interface. The LinkedBag class has two instance variables firstNode and numberOfEntries. It has an inner class Node. BagInterface.java /** An interface that describes the operations of a bag of objects. @author Frank M. Carrano @author Timothy M. Henry @version 4.1*/public interface BagInterface<T>{ /** Gets the current number of entries in this bag. @return The integer number of entries currently in the bag. */ public int getCurrentSize(); /** Sees whether this bag is empty....
Solve this Write a C++ class that implements a stack using a linked list. The type...
Solve this Write a C++ class that implements a stack using a linked list. The type of data contained in the stack should be double. The maximum size of the stack is 30. Implement the following methods: . · Constructor and destructor; // 5 pts · void push (double value); // pushes an element with the value into the stack. 5 pts. · double pop (); // pops an element from the stack and returns its value. 5 pts. ·...
Write a complete C++ program that defines, implements, and utilizes a Lion class and a Pine...
Write a complete C++ program that defines, implements, and utilizes a Lion class and a Pine class. Definition of classes from the implementation of the classes should be split. The program is made of five files: Lion.h, Lion.cpp, Pine.h, Pine.cpp, and TestLionPine.cpp. The components of Lion class are defined in the Lion.h file; however, all constructors and methods should not have any implementation code in this header file. All implementation code, i.e. constructor body and method body, should be written...
Program Specifications: Write a program that defines a class HumanBMI, implements it as required, and tests...
Program Specifications: Write a program that defines a class HumanBMI, implements it as required, and tests the class implementation. The class definition and implementation should be separated into HumanBMI.h and HumanBMI.cpp files. A. The class HumanBMI consists of three private member variables: name of type string, height of type int in inches, and weight of type int in pounds. The class HumanBMI also includes the following public member functions: 1) setName to set the name member variable with a string...
JAVA * create Q3 class to implement comparator for a TreeSet. The purpose of       ...
JAVA * create Q3 class to implement comparator for a TreeSet. The purpose of        * the comparator is to compare the alphabetic order of integers. With Q3,        * the order of Integer elements will be sort according to the order of        * digit Unicode.        * For example, the value of {11,3,31,2} will return {11,2,3,31}        * NOTE: You don't need to compare each digit one by one. Just compare them System.out.println("Q3...
In C: Write a function definition called PhoneType that takes one character argument/parameter called phone and...
In C: Write a function definition called PhoneType that takes one character argument/parameter called phone and returns a double. When the variable argument phone contains the character a or A print the word Apple and return 1099.99 When phone contains anything else return a 0.0
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT