Advertisement
Guest User

pa3b

a guest
Feb 18th, 2019
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. public static double[] validateArgs(String[] args) {
  2. double[] parameters = new double[6];
  3.  
  4. if (args.length == 6) {
  5. for (int i = 0; i < args.length; i++) {
  6. try {
  7. parameters[i] = Double.parseDouble(args[i]);
  8. } catch (Exception e) {
  9. return null;
  10. }
  11. }
  12. } else {
  13. return null;
  14. }
  15. return parameters;
  16. public static void main(String[] args) {
  17. double[] input = null;
  18. if(args == null) {
  19. System.out.println(ERR_USAGE);
  20. }else {
  21. input = validateArgs(args);
  22. }
  23. try {
  24. LinearEquation LE = new LinearEquation(input);
  25.  
  26. if (LE.isSolvable() != true) {
  27. System.out.println(ERR_NOSLTN);
  28. } else {
  29. System.out.printf("Solution: x=%.3f, y=%.3f%n", LE.getX(), LE.getY());
  30. }
  31. }catch(Exception e) {
  32. System.out.println(ERR_USAGE);
  33. }
  34.  
  35.  
  36. }
  37.  
  38. public class LinearEquation {
  39.  
  40. private double a;
  41. private double b;
  42. private double c;
  43. private double d;
  44. private double e;
  45. private double f;
  46.  
  47. /**
  48. * Initialize the linear equation of form:
  49. * ax + by = e
  50. * cx + dy = f
  51. *
  52. * @param a parameter a
  53. * @param b parameter b
  54. * @param c parameter c
  55. * @param d parameter d
  56. * @param e parameter e
  57. * @param f parameter f
  58. */
  59. public LinearEquation(double a, double b, double c, double d, double e, double f) {
  60. this.a = a;
  61. this.b = b;
  62. this.c = c;
  63. this.d = d;
  64. this.e = e;
  65. this.f = f;
  66. }
  67.  
  68. /**
  69. * Convenience constructor to initialize the linear equation via array
  70. *
  71. * THIS CONSTRUCTOR CALLS THE CONSTRUCTOR ABOVE USING THE ARRAY CONTENTS
  72. *
  73. * @param p parameter array, assumed to be length 6 (a-f, in order)
  74. */
  75. public LinearEquation(double[] p) {
  76. // MUST call the above constructor
  77. // with the contents of p
  78. this(p[0], p[1], p[2], p[3], p[4], p[5]);
  79. }
  80.  
  81. /**
  82. * Returns parameter a
  83. *
  84. * @return a
  85. */
  86. public double getA() {
  87. return a; // replace with your code
  88. }
  89.  
  90. /**
  91. * Returns parameter b
  92. *
  93. * @return b
  94. */
  95. public double getB() {
  96. return b; // replace with your code
  97. }
  98.  
  99. /**
  100. * Returns parameter c
  101. *
  102. * @return c
  103. */
  104. public double getC() {
  105. return c; // replace with your code
  106. }
  107.  
  108. /**
  109. * Returns parameter d
  110. *
  111. * @return d
  112. */
  113. public double getD() {
  114. return d; // replace with your code
  115. }
  116.  
  117. /**
  118. * Returns parameter e
  119. *
  120. * @return e
  121. */
  122. public double getE() {
  123. return e; // replace with your code
  124. }
  125.  
  126. /**
  127. * Returns parameter f
  128. *
  129. * @return f
  130. */
  131. public double getF() {
  132. return f; // replace with your code
  133. }
  134.  
  135. /**
  136. * Returns true if the parameterized equation is solvable (i.e. denominator
  137. * ad-bc is not 0)
  138. *
  139. * @return true if solvable, false otherwise
  140. */
  141. public boolean isSolvable() {
  142. if (((a * d) - (b * c)) != 0)
  143. return true;
  144. else
  145. return false;
  146. }
  147.  
  148. /**
  149. * Returns solution for x if solvable, null otherwise
  150. *
  151. * @return x if solvable, null otherwise
  152. */
  153. public Double getX() {
  154. Double x = null;
  155. if (isSolvable())
  156. x = (e * d - b * f) / (a * d - b * c);
  157. return x;
  158. }
  159.  
  160. /**
  161. * Returns solution for y if solvable, null otherwise
  162. *
  163. * @return y if solvable, null otherwise
  164. */
  165. public Double getY() {
  166. Double y = null;
  167. if (isSolvable())
  168. y = (a * f - e * c) / (a * d - b * c);
  169. return y;
  170. }
  171.  
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement