Question

In: Computer Science

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 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.

Solutions

Expert Solution

Program Code Screenshot

Sample Output

Program Code to Copy

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

class CountLetters{

    static Map countLetters(String str[]){
        //Create a new map
        Map<String,Integer> map = new HashMap();
        //Loop through all strings in the array
        for(String s : str){
            //add to map
            map.put(s.substring(0,1),map.getOrDefault(s.substring(0,1),0)+1);
        }
        return map;
    }

    public static void main(String[] args) {
        Map<String,Integer> map = countLetters(new String[]{"test","me","testing"});
        System.out.println(map);
    }
}

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...
Let's get some practice implementing a known interface. Create a public class named MyString. MyString should...
Let's get some practice implementing a known interface. Create a public class named MyString. MyString should provide a public constructor that accepts a single String argument. You should reject null Strings in your constructor using assert. MyString should also implement the Java Comparable interface, returning 1 for a positive result and -1 for a negative result. Normally Strings are compared lexicographically: "aaa" comes before "z". MyString should compare instances based on the length of its stored String. So MyString("aaa") should...
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,...
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...
Class, Let's create a menu in Java. A menu is a presentation of options for you...
Class, Let's create a menu in Java. A menu is a presentation of options for you to select. You can do this in the main() method. 1. Create a String variable named menuChoice. 2. Display 3 options for your program. I recommend using 3 System.out.println statements and a 4th System.out.print to show "Enter your Selection:". This needs to be inside the do -- while loop. A. Deposit Cash. B. Withdraw Cash X. Exit Enter your Selection: 3. Prompt for the...
Create an ID table class that maps identifiers to memory addresses for a Parser java program....
Create an ID table class that maps identifiers to memory addresses for a Parser java program. The first identifier will be at address 0, the second at address 1, and so on. I am working on a Lexer/Parser project for java. I've completed the lexer and parser portion of the program but I'm looking for some assistance in creating a ID table class. That has specific methods that add token identifiers into a hashmap. Here are the specifications that I'm...
create a public class and a main class to deletebyStudentID number with get ID and getStudentByName...
create a public class and a main class to deletebyStudentID number with get ID and getStudentByName in public class. when the user is prompted to search for the student and delete them by their. ID has seven digits and 6 students each. Ex. Delete student by their ID         Enter students name and their ID         Vanessa,         1111-111         Vanessa is now deleted
In the following class public class Single { private float unique; ... } Create constructor, setter...
In the following class public class Single { private float unique; ... } Create constructor, setter and getter methods, and toString method. Write a program that request a time interval in seconds and display it in hours, minutes, second format. NEED THESE ASAP
JAVA Assignement In the same file, create two classes: a public class Module1 and a non-public...
JAVA Assignement In the same file, create two classes: a public class Module1 and a non-public (i.e. default visibility) class Module1Data. These classes should have the following data fields and methods: 1) Module1 data fields: a. an object of type Module1Data named m1d b. an object of type Scanner named scanner c. a double named input 2) Module1Data methods: a. a method named square that returns an int, accepts an int parameter named number, and returns the value of number...
COMPLETE JAVA CODE: public final class Vector2 {       // NOTE:    Before you get...
COMPLETE JAVA CODE: public final class Vector2 {       // NOTE:    Before you get started with the constructors, implement the class variables and    // =====   the accessor methods (getX,getY). The tester for the constructors relies on these    //           methods being implemented. After this, move ahead with the constructors          // class variables here       COMPLETE JAVA CODE: /** * Creates the vector <code>(0.0, 0.0)</code>. */ public Vector2() {       COMPLETE...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT