source: doc/theses/jiada_liang_MMath/testP1.java @ 55ba259e

Last change on this file since 55ba259e was 1725989, checked in by Peter A. Buhr <pabuhr@…>, 3 weeks ago

add enumeration test programs for different programming languages

  • Property mode set to 100644
File size: 1.3 KB
Line 
1import java.io.*;
2
3public class testP1 {
4    public enum Planet {
5                //          mass      radius
6                MERCURY ( 3.303E23, 2.4397E6 ),
7                VENUS   ( 4.869E24, 6.0518E6 ),
8                EARTH   ( 5.976E24, 6.37814E6 ),
9                MARS    ( 6.421E23, 3.3972E6 ),
10                JUPITER ( 1.9E27,   7.1492E7 ),
11                SATURN  ( 5.688E26, 6.0268E7 ),
12                URANUS  ( 8.686E25, 2.5559E7 ),
13                NEPTUNE ( 1.024E26, 2.4746E7 );
14
15                private final double mass;   // in kilograms
16                private final double radius; // in meters
17
18                Planet( double mass, double radius ) {
19                        this.mass = mass;
20                        this.radius = radius;
21                }
22                private double mass() { return mass; }
23                private double radius() { return radius; }
24
25                // universal gravitational constant  (m3 kg-1 s-2)
26                public static final double G = 6.67300E-11;
27
28                double surfaceGravity() {
29                        return G * mass / ( radius * radius );
30                }
31                double surfaceWeight( double otherMass ) {
32                        return otherMass * surfaceGravity();
33                }
34    }
35    public static void main( String[] args ) {
36                if ( args.length != 1 ) {
37                        System.err.println( "Usage: java Planet <earth_weight>" );
38                        System.exit( -1 );
39                }
40                double earthWeight = Double.parseDouble( args[0] );
41                double mass = earthWeight / Planet.EARTH.surfaceGravity();
42                for ( Planet p : Planet.values() )
43                        System.out.printf( "Your weight on %s is %f%n", p, p.surfaceWeight( mass ) );
44    }
45}
46
47// java test
Note: See TracBrowser for help on using the repository browser.