* q3: Write a public static method named q3 that takes no parameters and returns
ID: 3700436 • Letter: #
Question
* q3: Write a public static method named q3 that takes no parameters and returns a new * HashMap with keys of type Integer mapped to values of type String containing the key-val * pairs (91: "workout", 73: "gift", 67: "teen") 94: Write a public static method name q4 that takes an Arraylist of Doubles as a e parameter and returns a double. This method will first iterate over the Arraylist while " computing the square root of each element and compute the sum of all of the * method will then return the cube of this sum se values. Your * aq5: Write a public static method named q5 that takes no parameters and returns a new Arraylist of type Double containing the values 13.79, 5.99, 4.51, and 14.57. The order * these values must be maintained roblems eJavadoc & Declaration ConsoleCoverageExplanation / Answer
PROGRAM
import java.util.*;
//Create Main Class TestHashMap
class TestHashMap
{
// Declare and Implement static method q3()
public static HashMap<Integer,String> q3()
{
// Declare HashMap Object map it contains integer and String values
HashMap<Integer,String> map=new HashMap<Integer,String>();
//insert integer and string values into map
map.put(91,"workout");
map.put(73,"gift");
map.put(67,"teen");
return map; // return HashMap Object
}
// Declare and Implement static method q4() with ArrayList of Double element arr
public static double q4(ArrayList<Double> arr)
{
double sq,sum=0; // Declare double variables sq and sum
System.out.println(" ArrayList Elements: "+arr);
for(Double n:arr) // Create for loop of arraylist elements
{
sq=Math.sqrt(n); // Calculate square root of each element
sum+=sq; // calculate sum of square root elements
}
return Math.pow(sum,3); // return cube of sum
}
// Declare and Implement static method q5 with return ArrayList elements
public static ArrayList<Double> q5()
{
// Declare arr Object of ArrayList
ArrayList<Double> arr=new ArrayList<Double>();
return arr; // return ArrayList elements
}
// Main Method
public static void main(String[] args)
{
HashMap<Integer,String> map=new HashMap<Integer,String>(); // Create HashMap Object map
map=q3(); // call q3() static method and return map
for(Map.Entry m:map.entrySet()) // create for loop of map entry set
{
System.out.println(m.getKey()+" "+m.getValue()); // display integer and string values
}
// Create ArrayList Object a
ArrayList<Double> a=new ArrayList<Double>();
// adding double elements into arraylist Object "a"
a.add(3.4);
a.add(4.2);
a.add(1.2);
double t=q4(a); // call q4() static method and pass arraylist object and return cube sum value
System.out.println(" Cube Sum= "+t); // display cube sum value
// Create another ArrayList Object b
ArrayList<Double> b=new ArrayList<Double>();
b=q5(); // call q5() static method and return ArrayList Object b then
// add double elements into ArrayList Object b
b.add(13.79);
b.add(5.99);
b.add(4.51);
b.add(14.57);
System.out.println(" ArrayList Elements: "+b); // Display ArrayList Elements
}
}
OUTPUT
F:>javac TestHashMap.java
F:>java TestHashMap
67 teen
73 gift
91 workout
ArrayList Elements: [3.4, 4.2, 1.2]
Cube Sum= 124.15771095764151
ArrayList Elements: [13.79, 5.99, 4.51, 14.57]
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.