Advertisement
dimipan80

Angle Unit Converter (Degrees ↔ Radians)

Aug 18th, 2014
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.16 KB | None | 0 0
  1. /* Write a method to convert from degrees to radians.
  2.  * Write a method to convert from radians to degrees.
  3.  * You are given a number n and n queries for conversion.
  4.  * Each conversion query will consist of a number + space + measure.
  5.  * Measures are "deg" and "rad".
  6.  * Convert all radians to degrees and all degrees to radians.
  7.  * Print the results as n lines, each holding a number + space + measure.
  8.  * Format all numbers with 6 digit after the decimal point. */
  9.  
  10. import java.util.Locale;
  11. import java.util.Scanner;
  12.  
  13. public class _05_AngleUnitConverterDegreesToRadians {
  14.  
  15.     public static void main(String[] args) {
  16.         // TODO Auto-generated method stub
  17.         Locale.setDefault(Locale.ROOT);
  18.         Scanner scan = new Scanner(System.in);
  19.         System.out.print("Enter a positive Integer for the Count of Coversions: ");
  20.         int count = scan.nextInt();
  21.         scan.nextLine();
  22.  
  23.         if (count > 0) {
  24.             String[] outputs = new String[count];
  25.             for (int i = 0; i < outputs.length; i++) {
  26.                 System.out.println("Enter Angle value and measure on single line, separated by a space:");
  27.                 String[] inputs = scan.nextLine().split("[ ]+");
  28.                 double angleValue = Double.parseDouble(inputs[0]);
  29.                 String measure = inputs[1];
  30.                 if (measure.equals("deg")) {
  31.                     double radians = convertAngleDegreesToRadians(angleValue);
  32.                     outputs[i] = String.format("%.6f rad", radians);
  33.                 } else {
  34.                     double degrees = convertAngleRadiansToDegrees(angleValue);
  35.                     outputs[i] = String.format("%.6f deg", degrees);
  36.                 }
  37.             }
  38.  
  39.             printTheResults(outputs);
  40.         } else {
  41.             System.out.println("Error! - Invalid Count number!!!");
  42.         }
  43.     }
  44.  
  45.     private static double convertAngleRadiansToDegrees(double angle) {
  46.         // Short variant: method "Math.toDegrees(angleValue)";
  47.         // otherwise:
  48.         return (180 * angle) / Math.PI;
  49.     }
  50.  
  51.     private static double convertAngleDegreesToRadians(double angle) {
  52.         // Short variant: method "Math.toRadians(angleValue)";
  53.         // otherwise:
  54.         return (Math.PI * angle) / 180;
  55.     }
  56.  
  57.     private static void printTheResults(String[] outputs) {
  58.         System.out.println("The all conversions of your angles are:");
  59.         for (String value : outputs) {
  60.             System.out.println(value);
  61.         }
  62.     }
  63.  
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement