Advertisement
Triacontakai

Trig

Jan 30th, 2017
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.12 KB | None | 0 0
  1. /**
  2. * Triacontakai
  3. * Just a trig calculator that I may or may not convert to do more stuff than just trig later
  4. * Can solve for angles and am currently working on solving for triangles.
  5. * Outputs in DEGREES, I might make this an option later
  6. */
  7. import java.util.*;
  8. public class Trig {
  9.     public static void main(String[] args){
  10.         Scanner lnscan = new Scanner(System.in);
  11.         Scanner optscan = new Scanner(System.in);
  12.         System.out.println("Trig Calculator");
  13.         System.out.println("1) Solve for angle");
  14.         System.out.println("2) Solve for triangle");
  15.         //System.out.println("2) Solve for side");
  16.         int opt = optscan.nextInt();
  17.         if (opt == 1){
  18.             //solve for angle
  19.             System.out.println("1) Sine (O/H)");
  20.             System.out.println("2) Cosine (A/H)");
  21.             System.out.println("3) Tangent (O/A)");
  22.             opt = optscan.nextInt();
  23.             if (opt == 1){
  24.                 System.out.println("Enter your side lengths (O/H): ");
  25.                 String stemp = lnscan.nextLine();
  26.                 String[] sides = stemp.split("/");
  27.                 double x = Double.parseDouble(sides[0]);
  28.                 double y = Double.parseDouble(sides[1]);
  29.                 double ans = Math.toDegrees(Math.asin(x/y));
  30.                 System.out.println("Answer: "+ ans);
  31.             }
  32.             else if (opt == 2){
  33.                 System.out.println("Enter your side lengths (A/H): ");
  34.                 String stemp = lnscan.nextLine();
  35.                 String[] sides = stemp.split("/");
  36.                 double x = Double.parseDouble(sides[0]);
  37.                 double y = Double.parseDouble(sides[1]);
  38.                 double ans = Math.toDegrees(Math.acos(x/y));
  39.                 System.out.println("Answer: "+ ans);
  40.             }
  41.             else if (opt == 3){
  42.                 System.out.println("Enter your side lengths (O/A): ");
  43.                 String stemp = lnscan.nextLine();
  44.                 String[] sides = stemp.split("/");
  45.                 double x = Double.parseDouble(sides[0]);
  46.                 double y = Double.parseDouble(sides[1]);
  47.                 double ans = Math.toDegrees(Math.acos(x/y));
  48.                 System.out.println("Answer: "+ ans);
  49.             }
  50.             else{
  51.                 System.out.println("Invalid option!");
  52.             }
  53.         }
  54.         else if (opt == 2){
  55.             //solve for triangle
  56.             int op = 0;
  57.             String aleg = "";
  58.             String oleg = "";
  59.             String hypo = "";
  60.             System.out.println("Enter angle measure: ");
  61.             String ang = lnscan.nextLine();
  62.             if (!ang.isEmpty()){
  63.                 op++;
  64.             }
  65.             System.out.println("Enter adjacent leg (if no angle, ignore 'adjacent'): ");
  66.             aleg = lnscan.nextLine();
  67.             if (!aleg.isEmpty()){
  68.                 op++;
  69.             }
  70.             if (op < 2){
  71.                 System.out.println("Enter opposite leg (if no angle, ignore 'opposite'): ");
  72.                 oleg = lnscan.nextLine();
  73.                 if (!oleg.isEmpty()){
  74.                     op++;
  75.                 }
  76.             }
  77.             if (op < 2){
  78.                 System.out.println("Enter hypotenuse: ");
  79.                 hypo = lnscan.nextLine();
  80.                 if (!hypo.isEmpty()){
  81.                     op++;
  82.                 }
  83.             }
  84.             if (op < 2){
  85.                 System.out.println("Enter 2 values please!");
  86.             }
  87. //START CALCULATIONS HERE
  88.                 if (!ang.isEmpty()){
  89.                         if (!hypo.isEmpty()){
  90.                             double angr = Math.toRadians(Double.parseDouble(ang));
  91.                             double adjacent = Double.parseDouble(hypo)*Math.cos(angr);
  92.                             double opposite = Double.parseDouble(hypo)*Math.sin(angr);
  93.                             double ang2 = 90.0 - Double.parseDouble(ang);
  94.                             System.out.println("Inputted angle: "+ ang);
  95.                             System.out.println("Hypotenuse: "+ hypo);
  96.                             System.out.println("Adjacent: "+ adjacent);
  97.                             System.out.println("Opposite: "+ opposite);
  98.                             System.out.println("Other angle: "+ ang2);
  99.                         }
  100.                         if (!aleg.isEmpty()){
  101.                             double angr = Math.toRadians(Double.parseDouble(ang));
  102.                             double hypotenuse = Double.parseDouble(aleg)/Math.cos(angr);
  103.                             double opposite = Double.parseDouble(aleg)*Math.tan(angr);
  104.                             double ang2 = 90.0 - Double.parseDouble(ang);
  105.                             System.out.println("Inputted angle: "+ ang);
  106.                             System.out.println("Hypotenuse: "+ hypotenuse);
  107.                             System.out.println("Adjacent: "+ aleg);
  108.                             System.out.println("Opposite: "+ opposite);
  109.                             System.out.println("Other angle: "+ ang2);
  110.                         }
  111.                         if (!oleg.isEmpty()){
  112.                             double angr = Math.toRadians(Double.parseDouble(ang));
  113.                             double hypotenuse = Double.parseDouble(oleg)/Math.sin(angr);
  114.                             double adjacent = Double.parseDouble(oleg)/Math.tan(angr);
  115.                             double ang2 = 90.0 - Double.parseDouble(ang);
  116.                             System.out.println("Inputted angle: "+ ang);
  117.                             System.out.println("Hypotenuse: "+ hypotenuse);
  118.                             System.out.println("Adjacent: "+ adjacent);
  119.                             System.out.println("Opposite: "+ oleg);
  120.                             System.out.println("Other angle: "+ ang2);
  121.                         }
  122.                 }
  123.                 else{
  124.                     if (!oleg.isEmpty() && !aleg.isEmpty()){
  125.                         double ang1 = Math.atan(Double.parseDouble(oleg)/Double.parseDouble(aleg));
  126.                         double hypotenuse = Double.parseDouble(oleg)/Math.sin(ang1);
  127.                         ang1 = Math.toDegrees(ang1);
  128.                         double ang2 = 90.0 - ang1;
  129.                         System.out.println("Main angle: "+ ang1);
  130.                         System.out.println("Hypotenuse: "+ hypotenuse);
  131.                         System.out.println("Adjacent: "+ aleg);
  132.                         System.out.println("Opposite: "+ oleg);
  133.                         System.out.println("Other angle: "+ ang2);
  134.                     }
  135.                     if (!oleg.isEmpty() && !hypo.isEmpty()){
  136.                         double ang1 = Math.asin(Double.parseDouble(oleg)/Double.parseDouble(hypo));
  137.                         double adjacent = Double.parseDouble(hypo)*Math.cos(ang1);
  138.                         ang1 = Math.toDegrees(ang1);
  139.                         double ang2 = 90.0 - ang1;
  140.                         System.out.println("Main angle: "+ ang1);
  141.                         System.out.println("Hypotenuse: "+ hypo);
  142.                         System.out.println("Adjacent: "+ adjacent);
  143.                         System.out.println("Opposite: "+ oleg);
  144.                         System.out.println("Other angle: "+ ang2);
  145.                     }
  146.                     if (!aleg.isEmpty() && !hypo.isEmpty()){
  147.                         double ang1 = Math.acos(Double.parseDouble(aleg)/Double.parseDouble(hypo));
  148.                         double opposite = Double.parseDouble(hypo)*Math.sin(ang1);
  149.                         ang1 = Math.toDegrees(ang1);
  150.                         double ang2 = 90.0 - ang1;
  151.                         System.out.println("Main angle: "+ ang1);
  152.                         System.out.println("Hypotenuse: "+ hypo);
  153.                         System.out.println("Adjacent: "+ aleg);
  154.                         System.out.println("Opposite: "+ opposite);
  155.                         System.out.println("Other angle: "+ ang2);
  156.                     }
  157.                 }
  158.  
  159.             /*System.out.println("Do you have an angle? Y/N")
  160.             char yn = optscan.next().charAt(0);
  161.             switch(yn){
  162.                 case y: System.out.println("State angle measure: ");
  163.                        
  164.                 case n: System.out.println("State your first side in the format (H/L:Measure): ");
  165.                         String stemp = lnscan.nextLine();
  166.                         String[] sides = stemp.split(":");
  167.                         String s1type = sides[0];
  168.                         String s1meas = sides[1];
  169.  
  170.                 case default:   System.out.println("Invalid option!");*/
  171.            
  172.             /*System.out.println("1) Sine (O/H)");
  173.             System.out.println("2) Cosine (A/H)");
  174.             System.out.println("3) Tangent (O/A)");
  175.             opt = optscan.nextInt();
  176.             if (opt == 1){
  177.                
  178.             }
  179.             else if (opt == 2){
  180.                
  181.             }
  182.             else if (opt == 3){
  183.                
  184.             }
  185.             else{
  186.                 System.out.println("Invalid option!");
  187.             }*/
  188.         }
  189.         else{
  190.             System.out.println("Invalid option!");
  191.         }
  192.     }
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement