QuestionAsk Questions?

Posted in : Java Interview Questions

Complete the Solver constructor so that a call to solveAll return a list with 2 values including the square root and the inverse of the integer passed as parameter.

public interface MathFunction { double calculate(double x); }

public class Solver {

private List<MathFunction> functionList; public Solver() {

// Complete here

} public List<Double> solveAll(double x) { List<Double> result = new ArrayList<Double>(); for (MathFunction function : this.functionList) { result.add(new Double(function.calculate(x))); } return result; }

}

View Answers

June 8, 2012 at 3:26 PM

**The given code accept a given value of double type and determine the square root of that number. Moreover, it also find the inverse of the given number. Both results will get store into the list.**

import java.util.*; public class Solver { public Solver() { List<Double> list=solveAll(25); for(int i=0;i<list.size();i++){ System.out.println(list.get(i)); } } public List<Double> solveAll(double x) { List<Double> result = new ArrayList<Double>(); result.add(new Double(Math.sqrt(x))); double inverse=(double)1/x; result.add(new Double(inverse)); return result; } public static void main(String[]args){ new Solver(); } }

Related Tutorials/Questions & Answers: