Java programming test for interviews

Here is the programming test used in the interview. I find that he has a very strange perspective, different from OO, and wonder why someone approaches the constructor from this point of view. Being a very experienced Java programmer, I immediately doubt the ability of the person who wrote this code and the strange perspective of the question.

I find these odd contextual questions for the job interview disturbing. I would like to receive feedback from other experienced Java OO programmers.

Complete the Solver constructor so that the call to solveAll returns a list with two values, including the square root and the inverse of the integer passed as a 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; } } 
+46
java oop
Aug 21 2018-12-12T00:
source share
9 answers

This is testing your design patterns using the simplest method possible. I think this may be a Strategy (or some other behavioral models). See the following:

http://en.wikipedia.org/wiki/Strategy_pattern

http://en.wikipedia.org/wiki/Behavioral_pattern

If you intend to conduct a Java interview, you should be able to identify the design template that they are hinting at, and this should prevent you from being too unresolved!

To answer the question, create two classes that implement MathFunction as needed, then create two instances and store them in functionList .

The point here is not that “you can do the calculations in this strange way”, it is “you can define the design patterns”.

+37
Aug 21 2018-12-12T00:
source share

I agree that it is confusing and overworking.

But I really think that the code is quite object oriented. This is an example of a strategy. The code that generates the answer list does not care how the answers are calculated - the two problems are separated, and a different calculation strategy can be applied without having to touch the code that generates the list.

To make the class more useful, these functions should be passed from the outside (for example, dependency injections), and not created in the constructor.

You know the answer, I suppose, but why is it worth it ...

 public Solver() { functionList = new ArrayList<MathFunction>(); functionList.add(new MathFunction() { @Override public double calculate(double x) { return 1d/x; } }); functionList.add(new MathFunction() { @Override public double calculate(double x) { return Math.sqrt(x); } }); } 
+31
Aug 21 2018-12-18T00:
source share

IMHO, this is a really strange approach. The name Solver is common; it does not have to perform certain operations by default. However, maybe this was part of the interview? Part one: just complete the request. Part Two: Say it is weird.

I would say that it is much better to use the addMathFunction(MathFunction mf) method. And if you want, create subclasses that extend the Solver class and add MathFunctions to their constructor.

+6
Aug 21 2018-12-12T00:
source share

Although I agree that this is probably not the best way or the OO way to do this, I had to assume that the essence of this exercise is to understand how well you understand Inheritance, Interfaces, and possibly anonymous inner classes. the only thing i can understand.

+3
Aug 21 '12 at 14:52
source share

I think they wanted you to add two elements to the list of functions. Each of them will implement the MathFunction interface, one for the square root and one for the inverse. The problem is in the details:

1- You have a function that returns 2 values ​​because it does two different things: bad

2- If you want to have this all-all class, m it would be interesting to get Mathfunctions as a parameter, so that you can do any types of MathFunctions, MathFunctions would be parameterizable

+3
Aug 21 2018-12-12T00:
source share

Here is my solution. This is a simple illustration of a factory class .

 public Solver() { functionList = new ArrayList<MathFunction>(); MathFunction sqrt = new MathFunction() { @Override public double calculate(double x) { return Math.sqrt(x); } }; functionList.add(sqrt); MathFunction inverse = new MathFunction() { @Override public double calculate(double x) { return 1.0D / x; } }; functionList.add(inverse); } 

This question shows two things:

  • Regardless of whether the programmer understands mathematical terms, such as inverse.
  • Does the programmer know that instances of interfaces or classes can be stored in a list and repeated later.
+3
Aug 21 '12 at 14:56
source share

Ok, I encoded a solution to my question. My instinct is that nothing should be in the constructor seems correct. The List function is not static, so you need an instance to initialize it. It specifies an integer, so I round to the integer. The inverse function is not advanced math.

 import java.util.ArrayList; import java.util.List; import java.lang.Math; public class Solver { private List<MathFunction> functionList = new ArrayList<MathFunction>();; public Solver() { // Complete here } public void initFunctionList() { MathFunction functionSquareRoot = new MathFunction(){ @Override public double calculate(double x) { return (x<0 ? 0: Math.sqrt(x)); // maybe we need throw an exception here for negative numbers, but we'll just set it to 0 }}; MathFunction functionInverse = new MathFunction(){ @Override public double calculate(double x) { return (x!=0.0 ? 1/x : 0); } }; functionList.add(functionSquareRoot); functionList.add(functionInverse); } 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; } } public interface MathFunction { double calculate(double x); } public class TestSolver { /** * @param args */ public static void main(String[] args) { Solver s = new Solver(); s.initFunctionList(); System.out.println(s.solveAll(16.0)); } } 

I am mistaken that the constructor may be

 public Solver() { // Complete here MathFunction functionSquareRoot = new MathFunction(){ @Override public double calculate(double x) { return (x<0 ? 0: Math.sqrt(x)); // maybe we need throw an exception here for negative numbers, but we'll just set it to 0 }}; MathFunction functionInverse = new MathFunction(){ @Override public double calculate(double x) { return (x!=0.0 ? 1/x : 0); } }; functionList.add(functionSquareRoot); functionList.add(functionInverse); } 
+3
Aug 21 2018-12-12T00:
source share

A little far-fetched, it seems to me closer to the design of the decorator. Not sure what I would say during the interview, but here is how I code it:

 package math; import java.util.ArrayList; import java.util.List; public class DecoratorMath { interface MathFunction { double calculate(double x); } public static void main(String[] args) { DecoratorMath decoratorMath = new DecoratorMath(); decoratorMath.go(); } public void go() { Solver solver = new Solver(); decorate(solver); List<Double> results = solver.solveAll(02); for (Double d :results) { System.out.println(d); } } public void decorate(Solver solver) { solver.addFunction(new MathFunction() { @Override public double calculate(double x) { return Math.sqrt(x); } }); solver.addFunction(new MathFunction() { @Override public double calculate(double x) { return 1d/x; } }); } class Solver { private List<MathFunction> mathFunctions = new ArrayList<MathFunction>(); public void addFunction(MathFunction mathFunction) { mathFunctions.add(mathFunction); } public List<Double> solveAll(double x) { List<Double> result = new ArrayList<Double>(); for (MathFunction function : mathFunctions) { result.add(new Double(function.calculate(x))); } return result; } } } 
+2
Jan 6 '13 at 1:50
source share

Doing all of this in a constructor is just bad practice. Anyway, my all-in-one solution.

 import java.util.*; import java.math.*; //sqrt / inverse public class Solver{ private List<MathFunction> functionList; public interface MathFunction{ double calculate(double x); } class X implements MathFunction { public double calculate(double x) { return Math.sqrt(x); } } class Y implements MathFunction { public double calculate(double y) { return 1/y; } } public Solver(){ //here functionList = new ArrayList<MathFunction>(); MathFunction f = (MathFunction) new X(); functionList.add(f); MathFunction f2 = (MathFunction) new Y(); functionList.add(f2); } 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; } public static void main(String... args) { System.out.println("result="+new Solver().solveAll(123)); } } 
0
Jan 09 '15 at 23:48
source share



All Articles