Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.95 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.regex.Pattern;
  3. //import java.util.*;
  4. import java.lang.Math;
  5. public class snellslaw {
  6.  
  7. public static void main(String[] args) {
  8. double taDouble = 0;
  9. double tbDouble = 0;
  10. double n1Double = 0;
  11. double n2Double = 0;
  12. //Ta = teta1 1=a
  13. //Tb = teta2 2=b
  14. //n1 = denisty of teta1
  15. //n2 denisty of teta2
  16. Scanner input = new Scanner(System.in);
  17. //double Tb;
  18. //double Ta;
  19. //double n2;
  20. System.out.println("Snell's Law Calculator + Features");
  21. System.out.println("What kind of action do you want to make?");
  22. System.out.println("A - Snell's Law Calculator");
  23. System.out.println("B- Critical Angle Calculator");
  24. String choice = input.next();
  25. int x = 1;
  26. do {
  27. if(choice.equalsIgnoreCase("A")) {
  28. x = 2;
  29.  
  30. }
  31. else if (choice.equalsIgnoreCase("B")) {
  32. x = 2;
  33. }
  34. else {
  35. System.out.println("\"" + choice + "\"" + " Is not a valid option! Try again: ");
  36. choice = input.next();
  37. }
  38. }while(x==1);
  39. System.out.println("Enter n1: ");
  40. //double n1 = input.nextDouble();
  41. String n1String = input.next();
  42. System.out.println("Enter θ1: ");
  43. String taString = input.next();
  44. /* if(TaS.contentEquals("x")) {
  45. double TbN = Math.toRadians(Tb);
  46. double snella = n2 * Math.sin(TbN);
  47. double DivA = snella / n1;
  48. double ASin = Math.asin(DivA);
  49. double snellf = Math.toDegrees(ASin);
  50. System.out.println(snellf);
  51. }
  52. else {
  53. // Not done yet.
  54. double Tb = input.nextDouble();
  55. double snella = n1 * Math.toRadians(Ta);
  56. double snellb = n2 * Math.toRadians(Tb);
  57. System.out.println(snella = snellb);
  58. }*/
  59. System.out.println("Enter n2: ");
  60. //double n2 = input.nextDouble();
  61. String n2String = input.next();
  62. // Calculate Critical Angle, If user writes Critical.
  63. /* if(Double.parseDouble(n1String) > Double.parseDouble(n2String) && input.equals("Critical")) {
  64. double div = Double.parseDouble(n2String) / Double.parseDouble(n1String);
  65. double critic = Math.asin(div);
  66. System.out.println(Math.toDegrees(critic));
  67. input.close();
  68. }
  69. else if (Double.parseDouble(n2String) > Double.parseDouble(n1String) && input.equals("Critical") ) {
  70. double div = Double.parseDouble(n1String) / Double.parseDouble(n2String);
  71. double critic = Math.asin(div);
  72. System.out.println(Math.toDegrees(critic));
  73. }*/
  74. System.out.println("Enter θ2: ");
  75. String tbString = input.next();
  76.  
  77. if(tbString.contentEquals("x")) {
  78. // If θ2 is a variable - Calculate it.
  79. double taN = Math.toRadians(Double.parseDouble(taString));
  80. double snella = Double.parseDouble(n1String) * Math.sin(taN);
  81. double divA = snella / Double.parseDouble(n2String);
  82. double aSin = Math.asin(divA);
  83. double snellfa = Math.toDegrees(aSin);
  84. System.out.println(tbString + " Is equal to: " + snellfa);
  85. }
  86. else {
  87. tbDouble = Double.parseDouble(tbString);
  88. }
  89. if(calc(Double a)) {
  90. //if θ1 is a variable - Calculate it.
  91. double tbN = Math.toRadians(tbDouble);
  92. double snella = Double.parseDouble(n2String) * Math.sin(tbN);
  93. double divA = snella / Double.parseDouble(n1String);
  94. double aSin = Math.asin(divA);
  95. double snellfb = Math.toDegrees(aSin);
  96. System.out.println(taString + " Is equal to: " + snellfb);
  97. }
  98. else{
  99. //If θ1 is a normal number, Continue as normal.
  100. taDouble = Double.parseDouble(taString);
  101. }
  102. if(n2String.contentEquals("x")) {
  103. //If n2 is a variable - Calculate it.
  104. double taN = Math.toRadians(taDouble);
  105. double tbN = Math.toRadians(tbDouble);
  106. double snella = Double.parseDouble(n1String) * Math.sin(taN);
  107. double sinTb = Math.sin(tbN);
  108. double divA = snella / sinTb;
  109. double snellfc = divA;
  110. System.out.println(n2String + " Is equal to: " + snellfc);
  111. }
  112. else {
  113. //If n2 is a normal number, Continue as normal.
  114. n2Double = Double.parseDouble(n2String);
  115. }
  116. if(n1String.contentEquals("x")) {
  117. //If n1 is a variable - Calculate it.
  118. double taN = Math.toRadians(taDouble);
  119. double tbN = Math.toRadians(tbDouble);
  120. double snella = Double.parseDouble(n2String) * Math.sin(tbN);
  121. double sinTa = Math.sin(taN);
  122. double divA = snella / sinTa;
  123. double snellfd = divA;
  124. System.out.println(n1String + " Is equal to: " + snellfd);
  125. }
  126. else {
  127. //If n1 is a normal number, Continue as normal.
  128. n1Double = Double.parseDouble(n1String);
  129. }
  130. }
  131. public static void calc(String n1String, String taString, String n2String, String tbString) {
  132. // Parse the inputs
  133. Pattern varName = Pattern.compile("[A-Za-z]\\w*");
  134. Double a = (varName.matcher(n1String).matches() ? null : Double.valueOf(n1String));
  135. Double b = (varName.matcher(taString).matches() ? null : Double.valueOf(taString));
  136. Double c = (varName.matcher(n2String).matches() ? null : Double.valueOf(n2String));
  137. Double d = (varName.matcher(tbString).matches() ? null : Double.valueOf(tbString));
  138. }
  139. /*public static void calc(String taDouble, String tbDouble, String n1Double, String n2Double, double snellfa, double snellfb, double snellfc, double snellfd) {
  140. if (1 != (taDouble == null ? 1 : 0) + (tbDouble == null ? 1 : 0) +
  141. (n1Double == null ? 1 : 0) + (n2Double == null ? 1 : 0)) {
  142. throw new IllegalArgumentException("Warning! Exactly 3 of the input must be number! Try again:");
  143. }
  144. if (taDouble == null) {
  145. System.out.println(taDouble + " = " + snellfa);
  146. } else if (tbDouble == null) {
  147. System.out.println(tbDouble + " = " + snellfb);
  148. } else if (n1Double == null) {
  149. System.out.println(n1Double + " = " + snellfc);
  150. } else {
  151. System.out.println(n2Double + " = " + snellfd);
  152. }
  153.  
  154. }
  155.  
  156.  
  157.  
  158. private static Double parse(String s) {
  159. try {
  160. return Double.valueOf(s);
  161. } catch (NumberFormatException e) {
  162. return null;
  163. }
  164. }*/
  165.  
  166.  
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement