Ads

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; }

}

Ads

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(); } }

Ads

Related Tutorials/Questions & Answers:

Ads

- Java Tutorials
- Java Code example
- Java Programming
- Java Beginners Examples
- Applet Tutorials
- Awt Tutorials
- Java Certification
- Interview Question
- Java Servlets Tutorial
- Jsp Tutorials
- Java Swing Tutorials
- JDBC Tutorial
- EJB Tutorials
- Java Server Faces (JSF) Tutorial
- WAP Tutorial
- Struts Tutorial
- JAXB Tutorial
- Spring FrameWork Tutorial
- SOA&Web Services Tutorials
- Bioinformatics Tutorials
- MySQL Tutorials
- JAVA DOM Tutorial
- XML Tutorial
- EAI Articles
- Many Programming Tutorials Links
- Tutorials Books
**Java Script Tutorial****Ajax Tutorial****Dojo Tutorials****Programming Books****Trainings****Flex****Ant****RDF**

Ads