q2: Write a public static method named q2 that takes an ArrayList of Doubles asa
ID: 3703764 • Letter: Q
Question
q2: Write a public static method named q2 that takes an ArrayList of Doubles asa parameter and returns an ArrayList of Doubles. The returned ArrayList will contain all values from the input ArrayList except those that have a cube root within 2.0 of 3.46 (ie *the target value of the cube root is 3.46 and the allowed variance from this target is 2.0) q3: Write a public static method named q3 that takes no parameters and returns a HashMap of Integers to Doubles. The returned HashMap should contain all the integers from 23 to 67 * as keys mapping to their square as values. The output should be inclusive of the end * points Nke * q4: Write a public static method named q4 that takes a String as a parameter and returns * an int. The return value is the total number of times "1" and "p" appear in the input * String Hint: If the second argument of the replace method is "" the matching characters will be removed (Replaced with nothing) * See (https://docs.oracle.com/java se/8/docs/api/java/lang/String . html#method. summary) for a *complete list of methods contained in the String class. You may call any of these methods *in your methodExplanation / Answer
public static ArrayList<Double> q2(ArrayList<Double> arr)
{
// create a new arraylist
ArrayList<Double> ans = new ArrayList<Double>();
int i;
// traverse through the list
for( i = 0 ; i < arr.size() ; i++ )
{
// get the cube root of current element in arr
double cube_root = Math.pow( arr.get( i ) , 0.33 );
// if the cube root is not in range 2.0 and 3.46
if( cube_root < 2.0 || cube_root > 3.46 )
ans.add( arr.get(i) );
}
return ans;
}
public static HashMap<Integer, Double> q3()
{
// create a HashMap object
HashMap<Integer, Double> hm = new HashMap<Integer, Double>();
int i;
for( i = 23 ; i <= 67 ; i++ )
// add key-value pair to HashMap using put() method
hm.put( i , (double)( i * i ) );
return hm;
}
public static int q4(String str)
{
int count = 0;
int i;
// traverse through the String str
for( i = 0 ; i < str.length() ; i++ )
{
// if current character is l or p
// charAt(i) returns the i th character in string
if( str.charAt(i) == 'l' || str.charAt(i) == 'p' )
count++;
}
return count;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.