java 面试Java编程测试
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12057460/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java programming test for interview
提问by user916115
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.
这是在求职面试中使用的编程测试。我发现它有一个非常奇怪的非 OO 视角,并想知道为什么有人会从这个角度来处理构造函数。作为一个非常有经验的Java程序员,我立即质疑编写此代码的个人的能力以及问题的奇怪视角。
I find these strange out of context questions on interviews disturbing. I would love feedback from other experienced OO Java programmers.
我发现面试中这些奇怪的断章取义的问题令人不安。我希望得到其他有经验的 OO Java 程序员的反馈。
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.
完成 Solver 构造函数,以便对 solveAll 的调用返回一个包含 2 个值的列表,包括平方根和作为参数传递的整数的倒数。
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;
}
}
回答by Joe
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/Strategy_pattern
http://en.wikipedia.org/wiki/Behavioral_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!
如果您要参加 Java 面试,您应该能够确定他们暗示的设计模式,这应该可以防止您过于不安!
To answer the question, create two classes that implement MathFunction
as required, then create two instances and store them in functionList
.
要回答这个问题,创建两个MathFunction
按要求实现的类,然后创建两个实例并将它们存储在functionList
.
The point here is not'can you do calculations in this strange way', it is 'can you identify design patterns'.
这里的重点不是“你能用这种奇怪的方式进行计算吗”,而是“你能识别设计模式吗”。
回答by ???v?т?
I agree that it's confusing and over-engineered.
我同意它令人困惑和过度设计。
But I do think the code is reasonably object-oriented. It's an instance of the strategy pattern. The code that generates a listof answers doesn't care how the answers are calculated - the two concerns are separated and a different calculation strategy could be applied without having to touch the code that generates the list.
但我确实认为代码是合理的面向对象的。它是策略模式的一个实例。生成答案列表的代码并不关心答案是如何计算的——这两个问题是分开的,可以应用不同的计算策略,而不必接触生成列表的代码。
To make the class more useful, these functions should be passed in from the outside (i.e. dependency injection) rather than being instantiated in the constructor.
为了使类更有用,这些函数应该从外部传入(即依赖注入)而不是在构造函数中实例化。
You know the answer, I assume, but for what it's worth...
你知道答案,我想,但对于它的价值......
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);
}
});
}
回答by Martijn Courteaux
IMHO, it is indeed a strange approach. The name Solver
is generic, it shouldn't implement specific operations by default. However, maybe that was part of the interview? Part one: simply fulfill the request. Part two: say that it is strange to do so.
恕我直言,这确实是一种奇怪的方法。该名称Solver
是通用的,默认情况下不应实现特定操作。然而,也许那是采访的一部分?第一部分:简单地满足请求。第二部分:说这样做很奇怪。
I would say that a much nicer approach would be to have an addMathFunction(MathFunction mf)
method. And if wanted, to create subclasses that extend the Solver
class and add MathFunctions in their constructor.
我会说更好的方法是拥有一种addMathFunction(MathFunction mf)
方法。如果需要,可以创建扩展Solver
类的子类并在其构造函数中添加 MathFunctions。
回答by JSBach
I think they wanted you to add two items in the functionlist. Each one would implement the MathFunction interface, one for the square root and one for the inverse. The prboblem lies in the details:
我认为他们希望您在功能列表中添加两个项目。每个都将实现 MathFunction 接口,一个用于平方根,一个用于求逆。问题在于细节:
1- You have a function which returns 2 values because it does two different things, that is bad
1-您有一个返回 2 个值的函数,因为它执行两种不同的操作,这很糟糕
2- If you want to have this "do-it-all" class,m it would be interesting to receive the Mathfunctions as a parameter so you can do any sort of MathFunctions, the MathFunctions would be parameterizable
2- 如果您想拥有这个“全能”类,那么将 Mathfunctions 作为参数接收会很有趣,这样您就可以执行任何类型的 MathFunctions,MathFunctions 将是可参数化的
回答by Gilbert Le Blanc
Here's 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:
这个问题说明了两件事:
- Whether the programmer understands math terms like inverse.
- Whether the programmer understands that instances of interfaces or classes can be stored in a list, and iterated over later.
- 程序员是否理解像逆这样的数学术语。
- 程序员是否理解接口或类的实例可以存储在列表中,并在以后迭代。
回答by Jason Carter
While I agree that this probably isn't the best way, or most OO way to do this, I would have to assume that the point of this exercise is to see how well you understand Inheritance, Interfaces and maybe anonymous inner classes. That's the only thing I can figure.
虽然我同意这可能不是最好的方法,或者大多数面向对象的方法来做到这一点,但我不得不假设这个练习的重点是看看你对继承、接口和匿名内部类的理解程度。这是我唯一能想到的。
回答by user916115
Ok, I coded the solution to my own question. My instinct that nothing should be in the constructor seems to be correct. The functionList is not static so you need an instance to initialize it. It specifies integer so I round to integer. The inverse function is not advanced math in any way.
好的,我将解决方案编码为我自己的问题。我认为构造函数中不应包含任何内容的直觉似乎是正确的。functionList 不是静态的,因此您需要一个实例来初始化它。它指定整数,所以我四舍五入到整数。反函数无论如何都不是高级数学。
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 mislead myself the constructor can 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);
}
回答by Bob
Somewhat contrived, seems closer to the decorator pattern to me. Not sure what I would say during an interview but here is how I would 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;
}
}
}
回答by Bob
Doing this all within the Constructor is just poor 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));
}
}