Question
Hi! Iwrote a java program, not for homework, just for myself ,to try to write a time dilation calculator. I used the special relativity formula,
Hi! Iwrote a java program, not for homework, just for myself ,to try to write a time dilation calculator. I used the special relativity formula, and I like my result, I also organized the code into three seperate parts, a public abstract class, a sub class, and a client class to test the program. As of now, while it works, I would appreciate someone reviewing it and seeing how to improve the organization, I think it is not as good as it should be in terms of the three part structures intentions. What follows is my code:
//part one of my code
public abstract class TimeDilation { private String time;
public TimeDilation() { } public TimeDilation(String time) { this.time = time; } public String toString() { return " Time Dilation: " + time; } public abstract double calculateTime(); }
// part two of my code
public class SingleCalculation extends TimeDilation { // instance variables private double observerTime = 0.0; private double spaceCraftTime = 0.0; private double constant = 0.0; private double velocity = 0.0; private double lightSpeed = 0.0; private double denominator = 0.0;
// Overloaded constructor public SingleCalculation(double observerTime, double spaceCraftTime, double constant, double velocity, double lightSpeed, double denominator, String time) { super(time); this.observerTime = observerTime; this.spaceCraftTime = spaceCraftTime; this.constant = constant; this.velocity = velocity; this.lightSpeed =lightSpeed; this.denominator = denominator;
}
public String toString() { System.out.println("");
System.out.println("Single Calculation");
return super.toString() + " Spacecraft time in seconds " + spaceCraftTime + ". Velocity in km per second " + velocity + "."; }
public double calculateTime() { denominator = 1-((velocity*velocity)/(lightSpeed*lightSpeed)); double squareRoot = Math.sqrt(denominator); return observerTime = spaceCraftTime/squareRoot;
} }
// part three of my code to test it
import java.util.ArrayList;
public class TestTimeDilation { public static void main( String [] args ) {
ArrayList
myList = new ArrayList
SingleCalculation single = new SingleCalculation(0.0, 1.0, 1.0, 239833.9664, 299792.458, 0.0, ""); myList.add(single);
for(int i =0; i } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started