1. Make a public static method named indexOf that takes an ArrayList of type Dou
ID: 3705426 • Letter: 1
Question
1. Make a public static method named indexOf that takes an ArrayList of type Double and returns an int. This method returns the index of the minimum result of taking the tangent of each value from the input.
2. Make a public static method named hashValue that takes a HashMap of type String to Integer
and returns an int. This method returns the minimum result (do not return the original
value) of taking the cosine of each value from the input while considering only
positive numbers from the inputs
Explanation / Answer
import java.util.*;
class Solution
{
public static int indexOf(ArrayList<Double> arr)
{
double min = Double.MAX_VALUE;
int index = -1;
int i;
for( i = 0 ; i < arr.size() ; i++ )
{
// get the tangent value of current term of arr
double tan = Math.tan( arr.get(i) );
// if current tan is smaller than max
if( tan < min )
{
min = tan;
index = i;
}
}
return index;
}
public static int hashValue( HashMap<String , Integer> hm )
{
int min = Integer.MAX_VALUE;
// keySet() returns the set of keys
for( String str : hm.keySet() )
{
// if current element is positive
if( hm.get(str) >= 0 )
{
// get() method returns the value for the given key passed as argument
int cos = (int)Math.cos( hm.get( str ) );
// if cos of current value is smaller than min
if( cos < min )
min = cos;
}
}
return min;
}
public static void main(String[] args)
{
ArrayList<Double> arr = new ArrayList<Double>();
arr.add(2.0);
arr.add(4.3);
arr.add(-5.56);
arr.add(-8.1);
arr.add(4.3);
arr.add(-8.98);
arr.add(-7.4);
System.out.println("Index of minimum tan value : " + indexOf(arr));
HashMap<String, Integer> hm = new HashMap<String , Integer>();
hm.put("a" , 1);
hm.put("b" , 4);
hm.put("c" , -4);
hm.put("d" , 8);
hm.put("e" , 6);
hm.put("f" , 5);
System.out.println(" Maximum cos value : " + hashValue(hm));
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.