import java.util.*;
import java.lang.*;
import java.io.*;
class Main {
/*Your getTopStocks method should return an array containing the names
of the three stocks with the highest average value.
The array should be sorted by decreasing average value.*/
public static List<String> getTopStock(String[] stocks, float [][] prices){
ArrayList<Float> list = new ArrayList<>();
HashMap<Float,String> map = new HashMap<>();
float moy=0;
for(int i=0; i<prices.length; i++){
for(int j=0; j<prices[i].length; j++){
//System.err.println(prices[i][j]);
moy+=prices[i][j];
}
moy=moy/prices[i].length;
list.add (moy);
map.put(moy, stocks[i]);
moy=0;
}
//for (Map.Entry<Float, String> entry : map.entrySet())
//System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue());
list.sort(Collections.reverseOrder());
//System.err.println(list);
ArrayList<String> listToReturn = new ArrayList<>();
for(int i=2; i>=0; i--)
listToReturn.add(map.get(list.get(i)));
return listToReturn;
}
public static void main(String[] args) {
String[] stocks={"AMZN", "CACC", "EQIX", "GOOG", "ORLY"};
float [][] prices={
{1.09f, 10.93f, 9.83f, 34.0f},
{10.34f, 10.56f, 10.14f, 12.17f, 13.1f, 11.2f},
{10.818334f, 22.073332f, 33.153331f},
{1.09f, 12.11f, 10.93f, 9.83f, 34.0f},
{10.34f, 10.56f, 10.14f,11.2f}};
System.out.println(getTopStock(stocks,prices));
}
}
To embed this program on your website, copy the following code and paste it into your website's HTML: