Question

I've looked at various sites to try to solve my problem. I've also tried to look for any YT or other videos on trig in Java, but couldn't find anything. (I'm a noob, so I also don't always understand everything that most sites refer to).

Anyways, I'm trying to make a simple program to calculate parts of Snell's Law (I realize there are websites that do this). But arc-sine doesn't seem to affect my variable values at all

Here is the code:

import java.util.Scanner;

public class trig_functions_test {
    public static void main(String[] args) {
        Scanner HumanInput = new Scanner (System.in);
        double n1, n2, Oi, OR;
        System.out.println("Enter the first medium's index of refraction.");
        n1 = HumanInput.nextDouble();
        System.out.println("Enter the second medium's index of refraction.");
        n2 = HumanInput.nextDouble();
        System.out.println("Enter the angle of incidence.");
        Oi = HumanInput.nextDouble();
        System.out.println("Enter the angle of refraction.");
        OR = HumanInput.nextDouble();

        //if angle of refraction is the missing variable
        if (OR == 0) {
           Oi = Math.toRadians(Oi);
           OR = (n1*Math.sin(Oi)/n2);
           OR = Math.toRadians(OR);
           OR = Math.asin (OR);
           OR = Math.toDegrees(OR);
           System.out.println(OR);
        }
    }
}

When I debug the program I get this:

First, here is the program in action:

(the 0 is just to signify no angle of refraction)

These are the results after the 2nd line in the if statement is evaluated(?):

After "OR" is converted to radians, the value of "OR" becomes 0.011364657670640462.

Then, and here is the problematic part, the part with the arc-sine is evaluated, and "OR" becomes 0.011364*90231927541* (the changed part is indicated between the *'s)

And then finally, "OR" is converted to degrees once more, and I am reverted back to my value after the second line (more or less) "OR," is then equal to 0.6511*609374816383* (again, the changed part is indicated between the *'s).

Was it helpful?

Solution

Your trouble stems from this line:

OR = Math.toRadians(OR);       

You already have your answer in radians when you do this calculation:

OR = (n1*Math.sin(Oi)/n2);

When you convert it to radians again, you are skewing the result. Delete OR = Math.toRadians(OR); and your program will work as intended.

OTHER TIPS

You're making your solution much more complicated than it needs to be. You should evaluate your expression in one shot by solving for the angle of refraction in Snell's law, like so:

Oi = Math.toRadians(Oi);
OR = Math.asin((n1/n2)*Math.sin(Oi));
OR = Math.toDegrees(OR)

System.out.println("Angle of refraction: "+OR);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top