Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- }
- }
- 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);
- }
- });
- }
- 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);
- }
- 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));
- }
- }
- 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);
- }
- 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;
- }
- }
- }
- 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));
- }
- }
Add Comment
Please, Sign In to add comment