Advertisement
Rustikus

Travelling at speed of light

Jun 11th, 2017
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.74 KB | None | 0 0
  1. import java.math.BigDecimal;
  2. import java.math.RoundingMode;
  3. import java.util.Scanner;
  4.  
  5. public class TravellingAtLightSpeed {
  6.     public static void main(String[] args) {
  7.         Scanner console = new Scanner(System.in);
  8.         BigDecimal lightYears = BigDecimal.valueOf(Double.parseDouble(console.nextLine()));
  9.         BigDecimal kmPerLightYear = BigDecimal.valueOf(9450000000000.0);
  10.         BigDecimal distanceInKm = BigDecimal.ZERO;
  11.         distanceInKm = lightYears.multiply(kmPerLightYear);
  12.         BigDecimal speed = BigDecimal.valueOf(300000.0);
  13.         BigDecimal totalSeconds = distanceInKm.divide(speed, 100, RoundingMode.HALF_EVEN);
  14.         BigDecimal totalWeeks = totalSeconds.divide(new BigDecimal("604800"), 0, RoundingMode.FLOOR);
  15.         totalSeconds = totalSeconds.subtract(totalWeeks.multiply(new BigDecimal("604800")));
  16.         BigDecimal totalDays = totalSeconds.divide(new BigDecimal("86400"), 0, RoundingMode.FLOOR);
  17.         totalSeconds = totalSeconds.subtract(totalDays.multiply(new BigDecimal("86400")));
  18.         BigDecimal totalHours = totalSeconds.divide(new BigDecimal("3600"), 0, RoundingMode.FLOOR);
  19.         totalSeconds = totalSeconds.subtract(totalHours.multiply(new BigDecimal("3600")));
  20.         BigDecimal totalMinutes = totalSeconds.divide(new BigDecimal("60"), 0, RoundingMode.FLOOR);
  21.         totalSeconds = totalSeconds.subtract(totalMinutes.multiply(new BigDecimal("60")));
  22.         totalSeconds = totalSeconds.setScale(0, RoundingMode.HALF_EVEN);
  23.         System.out.println(totalWeeks + " weeks");
  24.         System.out.println(totalDays + " days");
  25.         System.out.println(totalHours + " hours");
  26.         System.out.println(totalMinutes + " minutes");
  27.         System.out.println(totalSeconds + " seconds");
  28.     }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement