0% found this document useful (0 votes)
19 views

Import Import Import Import Import Import Import Class Public Static Int Int Int Throws New

This Java code uses the Nashorn JavaScript engine to calculate the average cost over a number of months by summing the travel costs for each month and dividing by the total number of months.

Uploaded by

Debarshi Roy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Import Import Import Import Import Import Import Class Public Static Int Int Int Throws New

This Java code uses the Nashorn JavaScript engine to calculate the average cost over a number of months by summing the travel costs for each month and dividing by the total number of months.

Uploaded by

Debarshi Roy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

import 

java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.script.Bindings;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

class Nashorn {
public static int averageCost(int noOfMonths ,int [] travelCostFo
rMonths)
            throws ScriptException {
        //Write your code here
         ScriptEngineManager scriptEngineManager = new ScriptEngi
neManager();
        ScriptEngine nashorn = scriptEngineManager.getEngineByNam
e("nashorn");
        Integer res = 0;
        for(int i: travelCostForMonths){
            res+=i;
        }
        Integer eval = (Integer) nashorn.eval("");   
    return Math.round((float)res/noOfMonths);
    }
}
public class JavaScriptEngine {
    public static void main(String[] args) throws IOException, Sc
riptException {
        BufferedReader br = new BufferedReader(new InputStreamRea
der(System.in));
        int noOfMonths = Integer.parseInt(br.readLine().trim());
        int [] travelCostForMonths = new int [noOfMonths];
        for(int i=0; i<noOfMonths; i++)
        {
            travelCostForMonths[i] = Integer.parseInt(br.readLine
().trim());
        }
        int result = Nashorn.averageCost(noOfMonths, travelCostFo
rMonths);
        System.out.println(result);
    }
}

You might also like