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

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...
In java: -Create a class named Animal
In java: -Create a class named Animal
In Java, create an abstract vehicle class. Create a Truck Class that Inherits from Vehicle Class....
In Java, create an abstract vehicle class. Create a Truck Class that Inherits from Vehicle Class. The vehicle class should contain at least 2 variables that could pertain to ANY vehicle and two methods. The truck class should contain 2 variables that only apply to trucks and one method. Create a console program that will instantiate a truck with provided member information then call one method from the truck and one method contained from the inherited vehicle class. Have these...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class named Book. In the Book class: Add the following private instance variables: title (String) author (String) rating (int) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. Add a second constructor that receives only 2 String parameters, inTitle and inAuthor. This constructor should only assign input parameter values to title and...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class named Employee. In the Employee class: Add the following private instance variables: name (String) job (String) salary (double) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. (Refer to the Tutorial3 program constructor if needed to remember how to do this.) Add a public String method named getName (no parameter) that...
Create a Java project called 5 and a class named 5 Create a second new class...
Create a Java project called 5 and a class named 5 Create a second new class named CoinFlipper Add 2 int instance variables named headsCount and tailsCount Add a constructor with no parameters that sets both instance variables to 0; Add a public int method named flipCoin (no parameters). It should generate a random number between 0 & 1 and return that number. (Important note: put the Random randomNumbers = new Random(); statement before all the methods, just under the...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class,...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class, you are to have the following data members: name: String (5 pts) id: String   (5 pts) hours: int   (5 pts) rate: double (5 pts) private members (5 pts) You are to create no-arg and parameterized constructors and the appropriate setters(accessors) and getters (mutators). (20 pts) The class definition should also handle the following exceptions: An employee name should not be empty, otherwise an exception...
JAVA Homework 1) Create a die class. This is similar to the coin class , but...
JAVA Homework 1) Create a die class. This is similar to the coin class , but instead of face having value 0 or 1, it now has value 1,2,3,4,5, or 6. Also instead of having a method called flip, name it roll (you flip a coin but roll a die). You will NOT have a method called isHeads, but you will need a method (call it getFace ) which returns the face value of the die. Altogether you will have...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT