/* * Einstein -- Performs calculations concerning time dilation. * * Tell Einstein what fraction of the speed of light you're * traveling to the Andromeda galaxy, and Einstein tells you * how long the trip will appear to you, taking relativistic * effects (i.e., time dilation) into account. */ class Einstein { static public void main(String[] args) { Galaxy destination = new Andromeda(); double distanceLightYears = destination.getDistance(); SpaceShip enterprise; double time; showInstructions(); try { enterprise = new SpaceShip(getPercentC()); time = enterprise.calcTimeDilation(distanceLightYears); showResults(time); showBackOnEarth(distanceLightYears, enterprise.speed); } catch (WarpException e) { System.out.println("Sorry, dilithium crystals have not been invented yet."); } catch (Exception e) { } } /** Tell the user how this program works. */ static void showInstructions() { System.out.println("Enter your speed as a percentage of the speed of "); System.out.println("light. I will calculate the time required for you to travel "); System.out.println("to the Andromeda galaxy, 2.2 million light years away."); System.out.println(""); } /** Get input from the user. */ static double getPercentC() throws java.io.IOException { StringBuffer buffer = new StringBuffer(); char c; while ((c = (char)System.in.read()) != '\n') buffer.append(c); return Double.valueOf(buffer.toString()).doubleValue(); } /** Tell the user the results of the calculation. */ static void showResults(double time) { int years = getYears(time); int days = getDays(time); System.out.println("Travel time as you perceive it, "); System.out.println("with relativistic effects, is: "); System.out.println(years + " years and " + days + " days."); System.out.println(""); } /** Show what's happening back on earth. */ static void showBackOnEarth(double distanceLightYears, double speed) { double distanceKm = Light.lightYearToKm(distanceLightYears); double elapsed = distanceKm/speed; System.out.println("Back on earth, " + getYears(elapsed) + " years and "); System.out.println(getDays(elapsed) + " days would have elapsed."); System.out.println(""); } private static int getYears(double time) { return (int)time; // integer portion } private static int getDays(double time) { return (int)((time - (double)(int)time)* 365.0); } } /** Light keeps track of C and has utilities to perform conversions. */ class Light { public static final double Cm_s = 2.998E8; // meters/second public static final double Ckm_yr = 94544.928E8; // kilometers/year public static final double lightYearToKm(double lightYear) { return lightYear * Ckm_yr; } } /** SpaceShips instances maintain a speed and know about * time dilation. */ class SpaceShip { double fraction; // fraction of the speed of light double speed; // speed in km/yr SpaceShip(double percent) { fraction = percent/100.0; speed = fraction * Light.Ckm_yr; } /** Determine the time required to travel a distance in the * moving time frame, given the speed maintained by this object. */ double calcTimeDilation(double distanceLightYr) throws WarpException { double distanceKm = Light.lightYearToKm(distanceLightYr); if (fraction > 1.0) throw new WarpException(); return (distanceKm/speed) * Math.sqrt(1.0 - (fraction*fraction)); } } abstract class Galaxy { abstract public double getDistance(); } class Andromeda extends Galaxy { public double getDistance() { return 2.2E6; // in light-years } } class WarpException extends Exception { }