-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVoyage.java
169 lines (144 loc) · 5.86 KB
/
Voyage.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// File: Voyage.java
// This applet is a small example to illustrate how to write an interactive
// Applet. This particular applet gets some information about a long voyage
// on a relativistic rocket, and prints the time of the voyage and other data.
// -- Michael Main ([email protected])
//import edu.colorado.collections.IntArrayBag;
import java.applet.Applet;
import java.awt.*; // Imports Button, Canvas, TextArea, TextField
import java.awt.event.*; // Imports ActionEvent, ActionListener
public class Voyage extends Applet
{
// Applet components
// These cannot be private because some browsers won't allow an inner
// class to access private members.
TextField distanceText = new TextField(10);
TextField accelerationText = new TextField(10);
Button launch = new Button("LAUNCH!");
TextArea answers = new TextArea(
"I am ready for your first trip.",
8,
60,
TextArea.SCROLLBARS_NONE
);
public void init( )
{
// Some messages for the top of the Applet:
addHorizontalLine(Color.orange);
addNewLine( );
// The two text fields and the launch button:
add(distanceText);
add(new Label("Distance of trip in light years"));
addNewLine( );
add(accelerationText);
add(new Label("Acceleration of rocket in g's"));
addNewLine( );
add(launch);
addNewLine( );
// A text area for printing the answers:
answers.setEditable(false);
add(answers);
addNewLine( );
addHorizontalLine(Color.orange);
// Tell the button what it should do when clicked:
launch.addActionListener(new LaunchListener( ));
}
class LaunchListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
double distance; // Distance of the trip in light years.
double acceleration; // Acceleration of the trip in g's.
distance = atod(distanceText.getText( ));
if (Double.isNaN(distance) || distance < 0)
{
answers.setText("Type a non-negative distance before launching.");
distanceText.requestFocus( );
distanceText.selectAll( );
return;
}
acceleration = atod(accelerationText.getText( ));
if (acceleration == Double.NaN || acceleration <= 0)
{
answers.setText("Type a positive acceleration before launching.");
accelerationText.requestFocus( );
accelerationText.selectAll( );
return;
}
makeTrip(distance, acceleration);
}
}
// The inverse hyperbolic sine function:
double asinh(double x)
{
return Math.log(x + Math.sqrt(x*x + 1));
}
double atod(String s)
{
double answer;
Double d;
try
{
d = new Double(s);
answer = d.doubleValue( );
}
catch (NumberFormatException e)
{
answer = Double.NaN;
}
return answer;
}
// Method to compute information about the trip. The parameters are
// the total trip distance (in light years) and the acceleration (in
// g's). The computation methods come from
// http://www.desy.de/user/projects/Physics/rocket.html
// The makeTrip method is not private because some browsers won't allow
// an inner class to access a private method.
void makeTrip(double distance, double acceleration)
{
final double LIGHT_YEAR = 9.47e15; // meters per light year
final double c = 3.00e8; // speed of light in meters/sec
final double g = 9.81; // gravity in meters/sec^2
final double SECONDS_PER_YEAR = 60.0 * 60.0 * 24.0 * 365.25;
double d; // Half of trip in meters
double a; // Acceleration in meters/sec^2
double time_earth; // Time for trip as viewed from Earth (seconds)
double time_ship; // Time for trip as viewed from rocket(seconds)
double years_earth; // Time for trip as viewed from Earth (years)
double years_ship; // Time for trip as viewed from rocket(years)
// Computations:
d = LIGHT_YEAR * distance / 2;
a = g * acceleration;
time_earth = 2*Math.sqrt( (d*d)/(c*c) + 2*d/a );
time_ship = 2*(c/a) * asinh(a*time_earth/c);
years_earth = time_earth / SECONDS_PER_YEAR;
years_ship = time_ship / SECONDS_PER_YEAR;
// Print the answers:
answers.setText("");
answers.append("Trip length: ");
answers.append((new Double(distance)).toString( ) + " light years.\n");
answers.append("Acceleration: ");
answers.append((new Double(acceleration)).toString( ) + " g.\n");
answers.append("Time on earth: ");
answers.append((new Double(years_earth)).toString( ) + " years.\n");
answers.append("Time on ship: ");
answers.append((new Double(years_ship)).toString( ) + " years.\n");
answers.append("Bon Voyage!\n");
answers.append("\nI am ready for your next trip.");
}
private void addHorizontalLine(Color c)
{
// Add a Canvas 10000 pixels wide but only 1 pixel high, which acts as
// a horizontal line to separate one group of components from the next.
Canvas line = new Canvas( );
line.setSize(10000,1);
line.setBackground(c);
add(line);
}
private void addNewLine( )
{
// Add a horizontal line in the background color. The line itself is
// invisible, but it serves to force the next Component onto a new line.
addHorizontalLine(getBackground( ));
}
}