Question

In: Computer Science

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 = [15 marks] =================================");
       Set set = new TreeSet<>(new Q3());
       Integer[] arr3 = {11,3,31,2};
       set.addAll(Arrays.asList(arr3));
       for (Integer element: set)
           System.out.print(element+"\t");
       System.out.println();


implement stats method in Q4 class to perform statistics for the
       * occurrence of each character in the provided sentence string.
       * Save the results into a hashmap, with key = character and value = occurrence
       * For example, "no news is good news" should return a hashmap with value of
       * { =4, s=3, d=1, e=2, w=2, g=1, i=1, n=3, o=3}
       * NOTE: the first key indicates space.

String sentence = "no news is good news";
       System.out.println("Q5 = [15 marks] =================================");
       System.out.println(Q5.stats(sentence));

Solutions

Expert Solution

import java.util.Comparator;

import java.util.TreeSet;

   class The_Comparator implements Comparator<String> {

    public int compare(String str1, String str2)

    { String first_Str;

        String second_Str;

        first_Str = str1;

        second_Str = str2;

        return second_Str.compareTo(first_Str);

    }

}

  public class Tree_Set_Demo {

    public static void main(String[] args)

    {

        TreeSet<String> tree_set = new TreeSet<String>(new

        The_Comparator()); tree_set.add("G");

        tree_set.add("E");

        tree_set.add("E");

        tree_set.add("K");

        tree_set.add("S");

        tree_set.add("4");

        System.out.println("Set before using the comparator: "+

        tree_set);

   System.out.println("The elements sorted in descending"+

        "order:");

        for (String element : tree_set)

            System.out.print(element + " ");

    }

}

output:

Set before using the comparator: [S, K, G, E, 4]
The elements sorted in descendingorder:
S K G E 4

=================================
import java.io.*;
import java.util.*;
class OccurenceOfCharInString {
   static void characterCount(String inputString)
   {
       // Creating a HashMap containing char
       // as a key and occurrences as a value
       HashMap<Character, Integer> charCountMap
           = new HashMap<Character, Integer>();

       // Converting given string to char array

       char[] strArray = inputString.toCharArray();

       // checking each char of strArray
       for (char c : strArray) {
           if (charCountMap.containsKey(c)) {

               // If char is present in charCountMap,
               // incrementing it's count by 1
               charCountMap.put(c, charCountMap.get(c) + 1);
           }
           else {

               // If char is not present in charCountMap,
               // putting this char to charCountMap with 1 as it's value
               charCountMap.put(c, 1);
           }
       }

       // Printing the charCountMap
       for (Map.Entry entry : charCountMap.entrySet()) {
           System.out.println(entry.getKey() + " " + entry.getValue());
       }
   }

   // Driver Code
   public static void main(String[] args)
   {
       String str = "Abhi";
       characterCount(str);
   }
}

output:

A 1
b 1
h 1
i 1

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.
Java the goal is to create a list class that uses an array to implement the...
Java the goal is to create a list class that uses an array to implement the interface below. I'm having trouble figuring out the remove(T element) and set(int index, T element). I haven't added any custom methods other than a simple expand method that doubles the size by 2. I would prefer it if you did not use any other custom methods. Please use Java Generics, Thank you. import java.util.*; /** * Interface for an Iterable, Indexed, Unsorted List ADT....
Create in Java Create a stack class to store integers and implement following methods: 1- void...
Create in Java Create a stack class to store integers and implement following methods: 1- void push(int num): This method will push an integer to the top of the stack. 2- int pop(): This method will return the value stored in the top of the stack. If the stack is empty this method will return -1. 3- void display(): This method will display all numbers in the stack from top to bottom (First item displayed will be the top value)....
Program in Java Create a class and name it MyArray and implement following method. * NOTE:...
Program in Java Create a class and name it MyArray and implement following method. * NOTE: if you need more methods, including insert(), display(), etc. you can also implement those. Method name: getKthMin(int k) This method receives an integer k and returns k-th minimum value stored in the array. * NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods). Example: Items...
Java: Create a class and name it MyArray and implement following method. * NOTE: if you...
Java: Create a class and name it MyArray and implement following method. * NOTE: if you need more methods, including insert(), display(), etc. you can also implement those Method name: getKthMin(int k) This method receives an integer k and returns k-th minimum value stored in the array. * NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods). Example: Items in the...
Program in Java Create a class and name it MyArray and implement following method. * NOTE:...
Program in Java Create a class and name it MyArray and implement following method. * NOTE: if you need more methods, including insert(), display(), etc. you can also implement those. Method name: getKthMin(int k) This method receives an integer k and returns k-th minimum value stored in the array. * NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods). Example: Items...
Code in Java Create a stack class to store integers and implement following methods: 1) void...
Code in Java Create a stack class to store integers and implement following methods: 1) void push(int num): This method will push an integer to the top of the stack. 2) int pop(): This method will return the value stored in the top of the stack. If the stack is empty this method will return -1. 3) void display(): This method will display all numbers in the stack from top to bottom (First item displayed will be the top value)....
Program in Java Create a stack class to store integers and implement following methods: 1- void...
Program in Java Create a stack class to store integers and implement following methods: 1- void push(int num): This method will push an integer to the top of the stack. 2- int pop(): This method will return the value stored in the top of the stack. If the stack is empty this method will return -1. 3- void display(): This method will display all numbers in the stack from top to bottom (First item displayed will be the top value)....
Program in Java Create a queue class to store integers and implement following methods: 1- void...
Program in Java Create a queue class to store integers and implement following methods: 1- void enqueue(int num): This method will add an integer to the queue (end of the queue). 2- int dequeue(): This method will return the first item in the queue (First In First Out). 3- void display(): This method will display all items in the queue (First item will be displayed first). 4- Boolean isEmpty(): This method will check the queue and if it is empty,...
Use Java programming to implement the following: Implement the following methods in the UnorderedList class for...
Use Java programming to implement the following: Implement the following methods in the UnorderedList class for managing a singly linked list that cannot contain duplicates. Default constructor Create an empty list i.e., head is null. boolean insert(int data) Insert the given data into the end of the list. If the insertion is successful, the function returns true; otherwise, returns false. boolean delete(int data) Delete the node that contains the given data from the list. If the deletion is successful, the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT