Guest User

Untitled

a guest
Feb 16th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.74 KB | None | 0 0
  1. public interface MathFunction {
  2. double calculate(double x);
  3. }
  4.  
  5. public class Solver {
  6.  
  7. private List<MathFunction> functionList;
  8.  
  9. public Solver() {
  10.  
  11. //Complete here
  12.  
  13. }
  14.  
  15. public List<Double> solveAll(double x) {
  16. List<Double> result = new ArrayList<Double>();
  17. for (MathFunction function : this.functionList) {
  18. result.add(new Double(function.calculate(x)));
  19. }
  20.  
  21. return result;
  22. }
  23. }
  24.  
  25. public Solver() {
  26. functionList = new ArrayList<MathFunction>();
  27.  
  28. functionList.add(new MathFunction() {
  29.  
  30. @Override
  31. public double calculate(double x) {
  32. return 1d/x;
  33. }
  34. });
  35.  
  36. functionList.add(new MathFunction() {
  37.  
  38. @Override
  39. public double calculate(double x) {
  40. return Math.sqrt(x);
  41. }
  42. });
  43. }
  44.  
  45. public Solver() {
  46. functionList = new ArrayList<MathFunction>();
  47. MathFunction sqrt = new MathFunction() {
  48. @Override
  49. public double calculate(double x) {
  50. return Math.sqrt(x);
  51. }
  52.  
  53. };
  54. functionList.add(sqrt);
  55. MathFunction inverse = new MathFunction() {
  56. @Override
  57. public double calculate(double x) {
  58. return 1.0D / x;
  59. }
  60.  
  61. };
  62. functionList.add(inverse);
  63. }
  64.  
  65. import java.util.ArrayList;
  66. import java.util.List;
  67. import java.lang.Math;
  68.  
  69. public class Solver {
  70.  
  71. private List<MathFunction> functionList = new ArrayList<MathFunction>();;
  72.  
  73. public Solver() {
  74.  
  75. // Complete here
  76.  
  77. }
  78.  
  79. public void initFunctionList() {
  80.  
  81. MathFunction functionSquareRoot = new MathFunction(){
  82.  
  83. @Override
  84. public double calculate(double x) {
  85. 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
  86. }};
  87.  
  88. MathFunction functionInverse = new MathFunction(){
  89.  
  90. @Override
  91. public double calculate(double x) {
  92. return (x!=0.0 ? 1/x : 0);
  93. }
  94.  
  95. };
  96.  
  97. functionList.add(functionSquareRoot);
  98. functionList.add(functionInverse);
  99.  
  100. }
  101.  
  102. public List<Double> solveAll(double x) {
  103. List<Double> result = new ArrayList<Double>();
  104.  
  105. for (MathFunction function : this.functionList) {
  106. result.add(new Double(function.calculate(x)));
  107. }
  108.  
  109. return result;
  110. }
  111.  
  112. }
  113.  
  114.  
  115. public interface MathFunction {
  116. double calculate(double x);
  117. }
  118.  
  119.  
  120. public class TestSolver {
  121.  
  122. /**
  123. * @param args
  124. */
  125. public static void main(String[] args) {
  126. Solver s = new Solver();
  127. s.initFunctionList();
  128. System.out.println(s.solveAll(16.0));
  129.  
  130. }
  131.  
  132. }
  133.  
  134. public Solver() {
  135.  
  136. // Complete here
  137. MathFunction functionSquareRoot = new MathFunction(){
  138.  
  139. @Override
  140. public double calculate(double x) {
  141. 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
  142. }};
  143.  
  144. MathFunction functionInverse = new MathFunction(){
  145.  
  146. @Override
  147. public double calculate(double x) {
  148. return (x!=0.0 ? 1/x : 0);
  149. }
  150.  
  151. };
  152.  
  153. functionList.add(functionSquareRoot);
  154. functionList.add(functionInverse);
  155.  
  156. }
  157.  
  158. package math;
  159.  
  160. import java.util.ArrayList;
  161. import java.util.List;
  162.  
  163. public class DecoratorMath
  164. {
  165.  
  166. interface MathFunction
  167. {
  168. double calculate(double x);
  169. }
  170.  
  171. public static void main(String[] args)
  172. {
  173. DecoratorMath decoratorMath = new DecoratorMath();
  174. decoratorMath.go();
  175. }
  176.  
  177. public void go()
  178. {
  179. Solver solver = new Solver();
  180. decorate(solver);
  181. List<Double> results = solver.solveAll(02);
  182. for (Double d :results)
  183. {
  184. System.out.println(d);
  185. }
  186. }
  187.  
  188. public void decorate(Solver solver)
  189. {
  190. solver.addFunction(new MathFunction()
  191. {
  192. @Override
  193. public double calculate(double x)
  194. {
  195. return Math.sqrt(x);
  196. }
  197. });
  198.  
  199. solver.addFunction(new MathFunction()
  200. {
  201. @Override
  202. public double calculate(double x)
  203. {
  204. return 1d/x;
  205. }
  206. });
  207. }
  208.  
  209. class Solver
  210. {
  211. private List<MathFunction> mathFunctions = new ArrayList<MathFunction>();
  212.  
  213. public void addFunction(MathFunction mathFunction)
  214. {
  215. mathFunctions.add(mathFunction);
  216. }
  217.  
  218. public List<Double> solveAll(double x)
  219. {
  220. List<Double> result = new ArrayList<Double>();
  221. for (MathFunction function : mathFunctions)
  222. {
  223. result.add(new Double(function.calculate(x)));
  224. }
  225. return result;
  226. }
  227. }
  228. }
  229.  
  230. import java.util.*;
  231. import java.math.*;
  232.  
  233. //sqrt / inverse
  234.  
  235. public class Solver{
  236.  
  237. private List<MathFunction> functionList;
  238.  
  239. public interface MathFunction{
  240. double calculate(double x);
  241. }
  242.  
  243. class X implements MathFunction {
  244. public double calculate(double x) {
  245. return Math.sqrt(x);
  246. }
  247. }
  248.  
  249. class Y implements MathFunction {
  250. public double calculate(double y) {
  251. return 1/y;
  252. }
  253. }
  254.  
  255.  
  256.  
  257. public Solver(){
  258. //here
  259. functionList = new ArrayList<MathFunction>();
  260.  
  261. MathFunction f = (MathFunction) new X();
  262. functionList.add(f);
  263.  
  264. MathFunction f2 = (MathFunction) new Y();
  265. functionList.add(f2);
  266.  
  267. }
  268.  
  269.  
  270. public List<Double> solveAll(double x){
  271.  
  272. List<Double> result=new ArrayList<Double>();
  273.  
  274. for (MathFunction function : this.functionList){
  275.  
  276. result.add(new Double(function.calculate(x)));
  277.  
  278. }
  279.  
  280. return result;
  281.  
  282. }
  283.  
  284. public static void main(String... args) {
  285.  
  286. System.out.println("result="+new Solver().solveAll(123));
  287.  
  288. }
  289.  
  290. }
Add Comment
Please, Sign In to add comment