Here is a programming test used in a job interview. I find it has a very strange non-OO perspective and wonder why anyone would approach a constructor from this perspective. As a very experienced Java programmer, I immediately question the ability of the individual who wrote this code and the strange perspective of the question.
I find these strange out of context questions on interviews disturbing. I would love feedback from other experienced OO Java programmers.
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; } }
This is testing your design patterns, by using the simplest possible method. I think this could be the Strategy (or some other behavioural pattern). See these:
http://en.wikipedia.org/wiki/Strategy_pattern
http://en.wikipedia.org/wiki/Behavioral_pattern
If you are going for a Java interview, you should be able to identify the design pattern they are hinting at and that should prevent you from being too unsettled!
To answer the question, create two classes that implement MathFunction
as required, then create two instances and store them in functionList
.
The point here is not 'can you do calculations in this strange way', it is 'can you identify design patterns'.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With