Java Question 12 - all parts must be solved to get credit for the question /** *
ID: 3736213 • Letter: J
Question
Java Question 12 - all parts must be solved to get credit for the question
/**
* P1: Write a public static method named q1 that takes a HashMap with Strings as keys and
* Strings as values as a parameter and returns a single String. The String returned will be
* the concatenation of the Strings "highly", "refer", "remind", "wide", and "package"
* followed by all of the keys from the input HashMap. Each String concatenated into the
* return String must be separated by a single space
*/
/**
* P2: Write a public static method named q2 that takes an ArrayList of Strings as a
* parameter and returns a double. This method will first iterate over the ArrayList while
* computing the length of each element and compute the sum of all of these values. Your
* method will then return the cube of this sum
*/
/**
* P3: Write a public static method named q3 that takes an ArrayList of Integers as a
* parameter and returns an int. The returned int should be the sum of the values int the
* input ArrayList at indices 20, 7, 8, 9, and 15. You can assume the input ArrayList has
* size large enough to avoid an index out of bounds exception at these indices
*/
/**
* P4: Write a public static method named q4 that takes no parameters and returns a new
* ArrayList of type Integer containing the values 2, 5, and 6. The order of these values
* must be maintained
*/
/**
* P5: Write a public static method named q5 that takes no parameters and returns a new
* HashMap with keys of type String mapped to values of type Integer containing the key-value
* pairs {"case":21, "owner":8}
*/
public static void main(String[] args){
/* Test your code here to verify*/
}
}
Explanation / Answer
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map.Entry;
public class MapMethods {
/**
* P1: Write a public static method named q1 that takes a HashMap with
* Strings as keys and Strings as values as a parameter and returns a single
* String. The String returned will be the concatenation of the Strings
* "highly", "refer", "remind", "wide", and "package" followed by all of the
* keys from the input HashMap. Each String concatenated into the return
* String must be separated by a single space
*
*/
public static String q1(HashMap<String, String> map) {
String ans = "highly" + " refer" + " remind" + " wide" + " package ";
for (Entry<String, String> entry : map.entrySet())
ans = ans + entry.getKey() + " ";
return ans;
}
/**
* P2: Write a public static method named q2 that takes an ArrayList of
* Strings as a parameter and returns a double. This method will first
* iterate over the ArrayList while computing the length of each element and
* compute the sum of all of these values. Your method will then return the
* cube of this sum
*/
public static double q2(ArrayList<String> aList) {
double sum = 0.0;
for (int i = 0; i < aList.size(); ++i) {
sum = sum + aList.get(i).length();
}
return sum * sum * sum;
}
/**
* P3: Write a public static method named q3 that takes an ArrayList of
* Integers as a parameter and returns an int. The returned int should be
* the sum of the values int the input ArrayList at indices 20, 7, 8, 9, and
* 15. You can assume the input ArrayList has size large enough to avoid an
* index out of bounds exception at these indices
*/
public static int q3(ArrayList<Integer> aList) {
return aList.get(7) + aList.get(8) + aList.get(9) + aList.get(15) + aList.get(20);
}
/**
* P4: Write a public static method named q4 that takes no parameters and
* returns a new ArrayList of type Integer containing the values 2, 5, and
* 6. The order of these values must be maintained
*/
public static ArrayList<Integer> q4() {
ArrayList<Integer> aList = new ArrayList<Integer>();
aList.add(2);
aList.add(5);
aList.add(6);
return aList;
}
/**
* P5: Write a public static method named q5 that takes no parameters and
* returns a new HashMap with keys of type String mapped to values of type
* Integer containing the key-value pairs {"case":21, "owner":8}
*/
public static HashMap<String, Integer> q5() {
HashMap<String, Integer> map = new HashMap<String, Integer>();
map.put("case", 21);
map.put("owner", 8);
return map;
}
public static void main(String[] args) {
/* Test your code here to verify */
}
}
==========
Written all the 5 methods, PLEASE RATE
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.