Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.regex.Pattern;
- //import java.util.*;
- import java.lang.Math;
- public class snellslaw {
- public static void main(String[] args) {
- double taDouble = 0;
- double tbDouble = 0;
- double n1Double = 0;
- double n2Double = 0;
- //Ta = teta1 1=a
- //Tb = teta2 2=b
- //n1 = denisty of teta1
- //n2 denisty of teta2
- Scanner input = new Scanner(System.in);
- //double Tb;
- //double Ta;
- //double n2;
- System.out.println("Snell's Law Calculator + Features");
- System.out.println("What kind of action do you want to make?");
- System.out.println("A - Snell's Law Calculator");
- System.out.println("B- Critical Angle Calculator");
- String choice = input.next();
- int x = 1;
- do {
- if(choice.equalsIgnoreCase("A")) {
- x = 2;
- }
- else if (choice.equalsIgnoreCase("B")) {
- x = 2;
- }
- else {
- System.out.println("\"" + choice + "\"" + " Is not a valid option! Try again: ");
- choice = input.next();
- }
- }while(x==1);
- System.out.println("Enter n1: ");
- //double n1 = input.nextDouble();
- String n1String = input.next();
- System.out.println("Enter θ1: ");
- String taString = input.next();
- /* if(TaS.contentEquals("x")) {
- double TbN = Math.toRadians(Tb);
- double snella = n2 * Math.sin(TbN);
- double DivA = snella / n1;
- double ASin = Math.asin(DivA);
- double snellf = Math.toDegrees(ASin);
- System.out.println(snellf);
- }
- else {
- // Not done yet.
- double Tb = input.nextDouble();
- double snella = n1 * Math.toRadians(Ta);
- double snellb = n2 * Math.toRadians(Tb);
- System.out.println(snella = snellb);
- }*/
- System.out.println("Enter n2: ");
- //double n2 = input.nextDouble();
- String n2String = input.next();
- // Calculate Critical Angle, If user writes Critical.
- /* if(Double.parseDouble(n1String) > Double.parseDouble(n2String) && input.equals("Critical")) {
- double div = Double.parseDouble(n2String) / Double.parseDouble(n1String);
- double critic = Math.asin(div);
- System.out.println(Math.toDegrees(critic));
- input.close();
- }
- else if (Double.parseDouble(n2String) > Double.parseDouble(n1String) && input.equals("Critical") ) {
- double div = Double.parseDouble(n1String) / Double.parseDouble(n2String);
- double critic = Math.asin(div);
- System.out.println(Math.toDegrees(critic));
- }*/
- System.out.println("Enter θ2: ");
- String tbString = input.next();
- if(tbString.contentEquals("x")) {
- // If θ2 is a variable - Calculate it.
- double taN = Math.toRadians(Double.parseDouble(taString));
- double snella = Double.parseDouble(n1String) * Math.sin(taN);
- double divA = snella / Double.parseDouble(n2String);
- double aSin = Math.asin(divA);
- double snellfa = Math.toDegrees(aSin);
- System.out.println(tbString + " Is equal to: " + snellfa);
- }
- else {
- tbDouble = Double.parseDouble(tbString);
- }
- if(calc(Double a)) {
- //if θ1 is a variable - Calculate it.
- double tbN = Math.toRadians(tbDouble);
- double snella = Double.parseDouble(n2String) * Math.sin(tbN);
- double divA = snella / Double.parseDouble(n1String);
- double aSin = Math.asin(divA);
- double snellfb = Math.toDegrees(aSin);
- System.out.println(taString + " Is equal to: " + snellfb);
- }
- else{
- //If θ1 is a normal number, Continue as normal.
- taDouble = Double.parseDouble(taString);
- }
- if(n2String.contentEquals("x")) {
- //If n2 is a variable - Calculate it.
- double taN = Math.toRadians(taDouble);
- double tbN = Math.toRadians(tbDouble);
- double snella = Double.parseDouble(n1String) * Math.sin(taN);
- double sinTb = Math.sin(tbN);
- double divA = snella / sinTb;
- double snellfc = divA;
- System.out.println(n2String + " Is equal to: " + snellfc);
- }
- else {
- //If n2 is a normal number, Continue as normal.
- n2Double = Double.parseDouble(n2String);
- }
- if(n1String.contentEquals("x")) {
- //If n1 is a variable - Calculate it.
- double taN = Math.toRadians(taDouble);
- double tbN = Math.toRadians(tbDouble);
- double snella = Double.parseDouble(n2String) * Math.sin(tbN);
- double sinTa = Math.sin(taN);
- double divA = snella / sinTa;
- double snellfd = divA;
- System.out.println(n1String + " Is equal to: " + snellfd);
- }
- else {
- //If n1 is a normal number, Continue as normal.
- n1Double = Double.parseDouble(n1String);
- }
- }
- public static void calc(String n1String, String taString, String n2String, String tbString) {
- // Parse the inputs
- Pattern varName = Pattern.compile("[A-Za-z]\\w*");
- Double a = (varName.matcher(n1String).matches() ? null : Double.valueOf(n1String));
- Double b = (varName.matcher(taString).matches() ? null : Double.valueOf(taString));
- Double c = (varName.matcher(n2String).matches() ? null : Double.valueOf(n2String));
- Double d = (varName.matcher(tbString).matches() ? null : Double.valueOf(tbString));
- }
- /*public static void calc(String taDouble, String tbDouble, String n1Double, String n2Double, double snellfa, double snellfb, double snellfc, double snellfd) {
- if (1 != (taDouble == null ? 1 : 0) + (tbDouble == null ? 1 : 0) +
- (n1Double == null ? 1 : 0) + (n2Double == null ? 1 : 0)) {
- throw new IllegalArgumentException("Warning! Exactly 3 of the input must be number! Try again:");
- }
- if (taDouble == null) {
- System.out.println(taDouble + " = " + snellfa);
- } else if (tbDouble == null) {
- System.out.println(tbDouble + " = " + snellfb);
- } else if (n1Double == null) {
- System.out.println(n1Double + " = " + snellfc);
- } else {
- System.out.println(n2Double + " = " + snellfd);
- }
- }
- private static Double parse(String s) {
- try {
- return Double.valueOf(s);
- } catch (NumberFormatException e) {
- return null;
- }
- }*/
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement