Question

In: Computer Science

CS 209 Data Structure 2. Create a method that takes a HashMap and returns the sum...

CS 209 Data Structure

2. Create a method that takes a HashMap and returns the sum of the keys of the HashMap.

3. Create a method that takes a HashMap and returns the sum of all keys and values of the HashMap.

For example, if the input is [1=9, 3=6, 4=9, 6=8, 7=6] then the method should return 59.

Solutions

Expert Solution

A method that takes a HashMap and returns the sum of the keys of the HashMap

public <K, V extends Number> double sumKeys(Map<K, V> map) {  
  // hashmap with data type of double and method written to get sum of keys 

    double sum = 0.0d;  // defination of sum
    for (Number key : map.key())  // take key in for loop
        sumkey += key.doubleValue();  // sum+ function will add up all the output given by for loop
    return sumkey; // returns sum of all key of HashMap
}

A method that takes a HashMap and returns the sum of all keys and values of the HashMap.

 Public sumKeyValue(Map<K, V> map)
 // method  to return the sum of key and values of Hashmap

 { 
  public <K, V extends Number> double sumKeys(Map<K, V> map) 
  // method that returns sum of keys
  {
    double sum = 0.0d;
    for (Number key : map.key()) // take key in for loop
        sumkey += key.doubleValue();
    return sumkey;
  }

public <K, V extends Number> double sumValues(Map<K, V> map) 
// method that returns sum of values
  {
    double sum = 0.0d;
    for (Number value : map.values()) // take value in for loop
        sumvalue += value.doubleValue();
    return sumvalue;
   }
   
   return sumkey+sumvalue; // returns total sum of keys and values
}

Related Solutions

CS 209 Data Structure 1. Create a method that takes an ArrayList of Integer and returns...
CS 209 Data Structure 1. Create a method that takes an ArrayList of Integer and returns a sorted copy of that ArrayList with no duplicates. Sample Input: {5, 7, 4, 6, 5, 6, 9, 7} Sample Output: {4, 5, 6, 7, 9}
Create a method that takes a HashMap<Integer, Integer> and returns the sum of the keys of...
Create a method that takes a HashMap<Integer, Integer> and returns the sum of the keys of the HashMap.
Java Programming CS 209 Data Structure 1. Create a method that takes an ArrayList of String...
Java Programming CS 209 Data Structure 1. Create a method that takes an ArrayList of String and returns a copy of that ArrayList with no duplicates. The relative ordering of elements in the new ArrayList should be the same. Sample Input: {"qwerty", "asdfgh", "qwer", "123", "qwerty", "123", "zxcvbn", "asdfgh"} Sample Output: {"qwerty", "asdfgh", "qwer", "123", "zxcvbn"}
CS 209 Data Structure 3. a. Create a class named Point3D that contains 3 instance variables...
CS 209 Data Structure 3. a. Create a class named Point3D that contains 3 instance variables x, y, and z. b. Create a constructor that sets the variables. Also, create get and set methods for each variable. c. Create a toString() method. d. Make Point3D implement Comparable. Also, create a compareTo(Point3D other) method that compares based on the x-coordinate, then y-coordinate for tiebreakers, then z-coordinate for tiebreakers. For example, (1, 2, 5) comes before (2, 1, 4), which comes before...
CS 209 Data Structure 5. Consider the Pair class covered in class: class Pair {    ...
CS 209 Data Structure 5. Consider the Pair class covered in class: class Pair {     public A first;     public B second;     public Pair(A a, B b)     {         first = a;         second = b;     }     public void setFirst(A a)     {         first = a;     }     public A getFirst()     {         return first;     }     public void setSecond(B b)     {         second = b;     }     public...
CS 209 Data Structure 1. show how to apply a selection sort on {45, 11, 50,...
CS 209 Data Structure 1. show how to apply a selection sort on {45, 11, 50, 59, 60, 2, 4, 7, 10}. 2. show how to apply a Insertion sort on {45, 11, 50, 59, 60, 2, 4, 7, 10}. 3. show how to apply a Bubble sort on {45, 11, 50, 59, 60, 2, 4, 7, 10}. 4. show how to apply a Merge sort on {45, 11, 50, 59, 60, 2, 4, 7, 10}. 5. show how to...
CS 209 Data Structure (Postfix notation) Postfix notation is a way of writing expressions without using...
CS 209 Data Structure (Postfix notation) Postfix notation is a way of writing expressions without using parentheses. For example, the expression (1 + 2) * 3 would be written as 1 2 + 3 *. A postfix expression is evaluated using a stack. Scan a postfix expression from left to right. A variable or constant is pushed into the stack. When an operator is encountered, apply the operator with the top two operands in the stack and replace the two...
In java we will need to create a method that takes the array {1,2,3,4,5} and returns...
In java we will need to create a method that takes the array {1,2,3,4,5} and returns the head reference to a linkedList. We will also need to display the linked list in out main method.
Create a Java method findLongestPalindrome that takes a Scanner scn as its parameter and returns a...
Create a Java method findLongestPalindrome that takes a Scanner scn as its parameter and returns a String. It returns the longest token from scn that is a palindrome (if one exists) or the empty string (otherwise). (Implementation note: Use the built in isPalindrome method. This method calls for an optimization loop.) Where the isPalindrome Method takes a String s as a parameter and returns a boolean. It returns true if s reads the same forwards and backwards, i.e., is a...
Modify the Complex structure in the notes and create a method that returns the complex conjugate....
Modify the Complex structure in the notes and create a method that returns the complex conjugate. // Structure Method Notes C++ #include using namespace std; // Object Definition struct Complex {    // Data members that define a complex number a + bi    double a, b;    // function within a data structure = METHOD    // functions specific to this type of object.    void printComplex(void); }; int main(void) {    int size = 3;    // Declare...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT