Question

Write code that maps Strings to the Integers which represent the lengths of the Strings. Create...

Write code that maps Strings to the Integers which represent the lengths of the Strings.

Create a class with a Map <String, Integer>.

Write one method called addStringToMap() that takes a String parameter and adds an entry to the Map with the String as the key and the length of the String as the value. Note that, if there is already an entry with that String as the key, nothing will happen when you add the entry.

Write a method called printOutMap() that prints out all the entries from the Map.

Main should call addStringToMap() repeatedly to add at least ten Strings, including at least one pair of duplicate Strings (like "John" and "John") and at least one other pair that are not duplicates but have the same length (like "Bob" and "Sue".) The Strings may be hard-coded; you do not need to take user input. Main should then call the printOutMap method.

Homework Answers

Answer #1

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

public class Mapp {

   Map<String, Integer> hm = new HashMap<String, Integer>();
   public static void main(String[] args) {
       Mapp mp=new Mapp();
// calling the method addStringToMap
       mp.addStringToMap("hello");
       mp.addStringToMap("John");
       mp.addStringToMap("Congratulation");
       mp.addStringToMap("John");
       mp.addStringToMap("Bob");
       mp.addStringToMap("Rohit");
       mp.addStringToMap("Sue");
       mp.addStringToMap("Constructor");
       mp.addStringToMap("Friends");
       mp.addStringToMap("Helmet");
       mp.addStringToMap("Mango");
       mp.addStringToMap("MAcculam");
       mp.addStringToMap("Salman");
      
       mp.printOutMap();
   }
  
   // below method used to print the Map value kye and string
   private void printOutMap() {
       for (Map.Entry<String, Integer> me : hm.entrySet()) {
   System.out.print(me.getKey() + ":");
   System.out.println(me.getValue());
   }
      
   }
   // below mthod uses to calculate the length of the string
   //and then store the corresponding string
   private void addStringToMap(String name) {
       // find the length of the string
       int l=name.length();
       hm.put(name, l);
      
   }

}
----------------------------------------------

SAMPLE OUTPUT:

Rohit:5
Sue:3
Helmet:6
Bob:3
Constructor:11
Friends:7
Salman:6
John:4
Mango:5
hello:5
MAcculam:8
Congratulation:14
------------------------------

SUMMARY:

I have provided the solution as per your requirement, i hope you're satisfied with the way i have approached. please dont hesitate to give me a Like if you like it, i appreciate your like. If you have any queries you can shoot them any time in comments section. I will be glad to help you.
Thank you :)

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
We encourage you to work in pairs for this challenge to create a Student class with...
We encourage you to work in pairs for this challenge to create a Student class with constructors. First, brainstorm in pairs to do the Object-Oriented Design for a Student class. What data should we store about Students? Come up with at least 4 different instance variables. What are the data types for the instance variables? Write a Student class below that has your 4 instance variables and write at least 3 different constructors: one that has no parameters and initializes...
Strings The example program below, with a few notes following, shows how strings work in C++....
Strings The example program below, with a few notes following, shows how strings work in C++. Example 1: #include <iostream> using namespace std; int main() { string s="eggplant"; string t="okra"; cout<<s[2]<<endl; cout<< s.length()<<endl; ​//prints 8 cout<<s.substr(1,4)<<endl; ​//prints ggpl...kind of like a slice, but the second num is the length of the piece cout<<s+t<<endl; //concatenates: prints eggplantokra cout<<s+"a"<<endl; cout<<s.append("a")<<endl; ​//prints eggplanta: see Note 1 below //cout<<s.append(t[1])<<endl; ​//an error; see Note 1 cout<<s.append(t.substr(1,1))<<endl; ​//prints eggplantak; see Note 1 cout<<s.find("gg")<<endl; if (s.find("gg")!=-1) cout<<"found...
This laboratory assignment involves implementing a data structure called a map. A map associates objects called...
This laboratory assignment involves implementing a data structure called a map. A map associates objects called keys with other objects called values. It is implemented as a Java class that uses arrays internally. 1. Theory. A map is a set of key-value pairs. Each key is said to be associated with its corresponding value, so there is at most one pair in the set with a given key. You can perform the following operations on maps. You can test if...
You need to write a permute class that will take first and second strings to rearrange...
You need to write a permute class that will take first and second strings to rearrange letters in first, followed by second. For example, if the first is “CAT” string and second is “MAN” string, then the program would print the strings TACMAN, ATCMAN, CTAMAN, TCAMAN, ACTMAN, and CATMAN. The first and second strings can be any length of string or a null. The permute class uses a Node class as link list node to link all letters arrangement. The...
Java Code import java.util.Scanner; /** Create two methods as instructed below and then call them appropriately....
Java Code import java.util.Scanner; /** Create two methods as instructed below and then call them appropriately. Hint: Only one of the methods you create needs to be called from the main method. */ public class LandCalculation { public static void main(String[] args) { final int FEET_PER_ACRE = 43560; // Number of feet per acre double tract = 0.0, acres = 0.0; Scanner keyboard = new Scanner(System.in); System.out.print("Enter the tract size: "); tract = keyboard.nextDouble(); // Validate the user's input. while(tract...
In c++ create a class to maintain a GradeBook. The class should allow information on up...
In c++ create a class to maintain a GradeBook. The class should allow information on up to 3 students to be stored. The information for each student should be encapsulated in a Student class and should include the student's last name and up to 5 grades for the student. Note that less than 5 grades may sometimes be stored. Your GradeBook class should at least support operations to add a student record to the end of the book (i.e., the...
Problem Definition: Problem: Given an array of integers find all pairs of integers, a and b,...
Problem Definition: Problem: Given an array of integers find all pairs of integers, a and b, where a – b is equal to a given number. For example, consider the following array and suppose we want to find all pairs of integers a and b where a – b = 3 A = [10, 4, 6, 16, 1, 6, 12, 13] Then your method should return the following pairs: 4, 1 15, 12 13, 10 A poor solution: There are...
PLEASE USING C# TO SOLVE THIS PROBLEM. THANK YOU SO MUCH! a. Create a project with...
PLEASE USING C# TO SOLVE THIS PROBLEM. THANK YOU SO MUCH! a. Create a project with a Program class and write the following two methods (headers provided) as described below: - A Method, public static int InputValue(int min, int max), to input an integer number that is between (inclusive) the range of a lower bound and an upper bound. The method should accept the lower bound and the upper bound as two parameters and allow users to re-enter the number...
Modify the following code by JAVA language Write a static method totalDurations that is passed the...
Modify the following code by JAVA language Write a static method totalDurations that is passed the parallel arrays from the main program. It creates a new pair of parallel arrays (phone numbers and durations, again) where each different incoming phone number is stored exactly once, and the duration is the total duration of all the calls from that phone number. It then prints these arrays. Nothing is returned. For example, if the arrays contain 555-1234 (duration 10), 555-4321 (duration 20),...
Objectives:The focus of this assignment is to create and use a recursive method given a moderately...
Objectives:The focus of this assignment is to create and use a recursive method given a moderately difficult problem. Program Description: This project will alter the EmployeeManager to add a search feature, allowing the user to find an Employee by a substring of their name. This will be done by implementing the Rabin-Karp algorithm. A total of seven classes are required. Employee (From previous assignment) HourlyEmployee (From previous assignment) SalaryEmployee (From previous assignment) CommissionEmployee (From previous assignment) EmployeeManager (Altered from previous...