Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Write a method to convert from degrees to radians.
- * Write a method to convert from radians to degrees.
- * You are given a number n and n queries for conversion.
- * Each conversion query will consist of a number + space + measure.
- * Measures are "deg" and "rad".
- * Convert all radians to degrees and all degrees to radians.
- * Print the results as n lines, each holding a number + space + measure.
- * Format all numbers with 6 digit after the decimal point. */
- import java.util.Locale;
- import java.util.Scanner;
- public class _05_AngleUnitConverterDegreesToRadians {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Locale.setDefault(Locale.ROOT);
- Scanner scan = new Scanner(System.in);
- System.out.print("Enter a positive Integer for the Count of Coversions: ");
- int count = scan.nextInt();
- scan.nextLine();
- if (count > 0) {
- String[] outputs = new String[count];
- for (int i = 0; i < outputs.length; i++) {
- System.out.println("Enter Angle value and measure on single line, separated by a space:");
- String[] inputs = scan.nextLine().split("[ ]+");
- double angleValue = Double.parseDouble(inputs[0]);
- String measure = inputs[1];
- if (measure.equals("deg")) {
- double radians = convertAngleDegreesToRadians(angleValue);
- outputs[i] = String.format("%.6f rad", radians);
- } else {
- double degrees = convertAngleRadiansToDegrees(angleValue);
- outputs[i] = String.format("%.6f deg", degrees);
- }
- }
- printTheResults(outputs);
- } else {
- System.out.println("Error! - Invalid Count number!!!");
- }
- }
- private static double convertAngleRadiansToDegrees(double angle) {
- // Short variant: method "Math.toDegrees(angleValue)";
- // otherwise:
- return (180 * angle) / Math.PI;
- }
- private static double convertAngleDegreesToRadians(double angle) {
- // Short variant: method "Math.toRadians(angleValue)";
- // otherwise:
- return (Math.PI * angle) / 180;
- }
- private static void printTheResults(String[] outputs) {
- System.out.println("The all conversions of your angles are:");
- for (String value : outputs) {
- System.out.println(value);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement