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}
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.
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...
A function calledsumOfFactorsthat takes n as a parameter and then returns the sum of the proper...
A function calledsumOfFactorsthat takes n as a parameter and then returns the sum of the proper factors/divisors ofn; for example,the sum of factorsof 20= 10+5+4+2 = 21.Note that 1 is nota properfactor of any number.Sample command => output:(display (sumOfFactors20))=> 21Hint: Calla recursivefunction sum_helperfrom sumOfFactorsfunction as follows:(define (sumOfFactors n)(sum_helper n (-n 1)))Here sum_helperfunction takes two parameters: n, d and returns the sum of all factors of n whichare <= d; for e.g. (sum_helper 20 6) returnsall proper factors of 20 which...
Database - SQL - Operations CS 203 Discrete Structure 2 Create a Microsoft Access Database consisting...
Database - SQL - Operations CS 203 Discrete Structure 2 Create a Microsoft Access Database consisting of the following two tables: Part_needs and Parts_Inventory Part_needs Supplier Part_number Project 23 1092 1 23 1101 3 23 9048 4 31 4975 3 31 3477 2 32 6984 4 32 9191 2 33 1001 1 Parts_Inventory Part_number Project Quantity Color_code 1001 1 14 8 1092 1 2 2 1101 3 1 1 3477 2 25 2 4975 3 6 2 6984 4 10...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT