Question

In: Computer Science

Language: JavaScript Create a public class CountLetters providing a single static method countLetters. countLetters accepts an...

Language: JavaScript

Create a public class CountLetters providing a single static method countLetters. countLetters accepts an array of Strings and returns a Map from Strings to Integers. (You can reject null arguments using assert.)

The map should contain counts of the passed Strings based on their first letter. For example, provided the array {"test", "me", "testing"} your Map should be {"t": 2, "m": 1}. You should ignore empty Strings and not include any zero counts.

As a reminder, you can retrieve the first character of a String as a char using charAt. You may find substring more helpful. You may also want to examine the Map getOrDefault method. You can use any Map implementation in java.util.

Below is currently what I've done.

import java.util.Map;
import java.util.HashMap;

public class CountLetters {
public static countletters(String[] data) {
assert(data != null);
  
}
}

Solutions

Expert Solution

In case of any query do comment. Please rate answer as well. Thanks

Code:

import java.util.HashMap;

public class Countletters
{
  public static void main (String[]args)
  {
        String[] data = {"test", "me", "testing"};
        HashMap<String,Integer> letterCountMap = countletters(data);
        System.out.println(letterCountMap);
  }
  
  //count leter method which will return Map string to intgers which will count of starting letter to count
  public static HashMap <String,Integer> countletters (String[]data)
  {

    assert (data != null);
    HashMap <String,Integer> letterCountMap =
      new HashMap <String,Integer> ();

    for (int i = 0; i < data.length; i++)
      {

        if (data[i] != null && !data[i].isEmpty ())
          {
            String str = data[i].substring (0,1);
            if (letterCountMap.containsKey (str))
              {
    
                        //if String is already in the map then increase the count by 1
                        int count = letterCountMap.get (str) + 1;
                        letterCountMap.put (str, count);
              }
            else
              {
                        // if String is not present then add it with count 1 
                        letterCountMap.put (str, 1);  
              }
          }

      }
      //return the map
      return letterCountMap;

  }
}

=========Output and screen shot of the code=====


Related Solutions

Let's get some practice with maps! Create a public class CountLetters providing a single static method...
Let's get some practice with maps! Create a public class CountLetters providing a single static method countLetters. countLetters accepts an array of Strings and returns a Map from Strings to Integers. (You can reject null arguments using assert.) The map should contain counts of the passed Strings based on their first letter. For example, provided the array {"test", "me", "testing"} your Map should be {"t": 2, "m": 1}. You should ignore empty Strings and not include any zero counts. As...
Make a public class Creater that provides a single class (static) method named subtractor. subtractor takes...
Make a public class Creater that provides a single class (static) method named subtractor. subtractor takes a single int argument and returns a method that implements the following Create interface: public interface Create { int change(int something); --------------------------------------------------------------------- The returned “function” should implement Create so that it subtracts the value passed to subtractor. For example Create one = Creater.substractor(5); Create two = Creater.subtractor(-3); System.out.println(one.change(5)); // should print 0 System.out.println(second.change(3); // should print -6 The answer should be is a single...
Design a Geometry class with the following methods: * A static method that accepts the radius...
Design a Geometry class with the following methods: * A static method that accepts the radius of a circle and returns the area of the circle. Use the following formula: Area=?r^2 * A static method that accepts the length and width of a rectangle and returns the area of the rectangle. Use the folliwng formula: Area = Length x Width * A static method that accepts the length of a triangle's base and the triangle's hight. The method should return...
For Java Let's get some practice with maps! Create a public class CountLetters providing a single...
For Java Let's get some practice with maps! Create a public class CountLetters providing a single static method countLetters. countLetters accepts an array of Strings and returns a Map from Strings to Integers. (You can reject null arguments using assert.) The map should contain counts of the passed Strings based on their first letter. For example, provided the array {"test", "me", "testing"} your Map should be {"t": 2, "m": 1}. You should ignore empty Strings and not include any zero...
public class GreeterTest {    public static void main(String[] args)    { // create an object...
public class GreeterTest {    public static void main(String[] args)    { // create an object for Greeter class Greeter greeter = new Greeter("Jack"); // create two variables Greeter var1 = greeter; Greeter var2 = greeter; // call the sayHello method on the first Greeter variable String res1 = var1.sayHello(); System.out.println("The first reference " + res1); // Call the setName method on the secod Grreter variable var2.setName("Mike"); String res2 = var2.sayHello(); System.out.println("The second reference " + res2);    } }...
Error: Main method is not static in class ArrayReview, please define the main method as: public...
Error: Main method is not static in class ArrayReview, please define the main method as: public static void main(String[] args) please help me fast: import java.util. Random; import java.util.Scanner; //ArrayReview class class ArrayReview { int array[];    //constructor ArrayReview (int n) { array = new int[n]; //populating array Random r = new Random(); for (int i=0;i<n; i++) array[i] = r.nextInt (); } //getter method return integer at given index int getElement (int i) { return array[i]; }    //method to...
Using maps in Java. Make a public class called ExampleOne that provides a single class (static)...
Using maps in Java. Make a public class called ExampleOne that provides a single class (static) method named firstOne. firstOne accepts a String array and returns a map from Strings to Integer. The map must count the number of passed Strings based on the FIRST letter. For example, with the String array {“banana”, “apples”, “blueberry”, “orange”}, the map should return {“b”:2, “a”:1, “o”:1}. Disregard empty Strings and zero counts. Retrieve first character of String as a char using charAt, or,...
Create a new Java file, containing this code public class DataStatsUser { public static void main...
Create a new Java file, containing this code public class DataStatsUser { public static void main (String[] args) { DataStats d = new DataStats(6); d.append(1.1); d.append(2.1); d.append(3.1); System.out.println("final so far is: " + d.mean()); d.append(4.1); d.append(5.1); d.append(6.1); System.out.println("final mean is: " + d.mean()); } } This code depends on a class called DataStats, with the following API: public class DataStats { public DataStats(int N) { } // set up an array (to accept up to N doubles) and other member...
PLEASE CODE THIS IN JAVA Create a driver class Playground that contains the function, public static...
PLEASE CODE THIS IN JAVA Create a driver class Playground that contains the function, public static void main(String[] args) {}. Create 2 SportsCar and 2 Airplane instances using their constructors. (SPORTSCAR AND AIRPLANE CLASSES LISTED BELOW THIS QUESTION. Add all 4 instances into a single array called, “elements.” Create a loop that examines each element in the array, “elements.” If the elements item is a SportsCar, run the sound method and if the item is an Aeroplane, run it’s ChangeSpeed...
Create a method:         Public Static boolean isSubArray489(int[] intArray) This method has an array of integers as its...
Create a method:         Public Static boolean isSubArray489(int[] intArray) This method has an array of integers as its input parameter, we'll say that a SubArray 9,8,7is that three integers, 9,8,7values appearing decreasing order one by one in the array. Return true if the array does contain any SubArray “987” Notice that any 3 integers that presenting a decreasing by one order must return True. (987, 876,765,654, 543, 432,321, 210 are all True) Call this method in your main function, and pass in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT