/*** CelestialBody class supports record keeping and computation concerning properties of stars, planets, and moons. Draft -bds 9/21/98 - built from basic version in Arnold/Gosling. ***/ import java.io.*; public class CelestialBody { // instance data private long idNum; private String name = ""; private CelestialBody orbits = null; // the body around which this one orbits. private double radius = 0.0; private double density = 0.0; private double majorAxis = 0.0; private double minorAxis; // class data private static long nextID = 0; // shared var to allow unique id's. private final static double PI = 3.145926535; private final static int majorVersion = 0; private final static int versionUpdate = 0; // constructors CelestialBody() { idNum = nextID++; } CelestialBody(String bodyName, CelestialBody whatItOrbits) { this(); // get done what the no arg constructor does. name = bodyName; orbits = whatItOrbits; } // constructor CelestialBody(String bodyName, CelestialBody whatItOrbits, double itsRadius, double itsDensity, double itsMajorAxis, double itsMinorAxis) { this( bodyName, whatItOrbits); radius = itsRadius; density = itsDensity; majorAxis = itsMajorAxis; if ( itsMinorAxis < 0.0 ) minorAxis = majorAxis; else minorAxis = itsMinorAxis; } // constructor // methods public double volume() { return area()*radius/3.0; } // method volume public double area() { return 4.0*PI*radius*radius; } // method area public static void displayVersion () { System.out.println("Using CelestialBody class version " + Integer.toString(majorVersion) + "." + Integer.toString(versionUpdate) ); } // class method displayVersion } // class CelestialBody /* Examples of declaring and using CelestialBodies (in a method (main, for instance) of another class CelestialBody planets[10]; //sun at 0, pluto at 9. CelestialBody sun = new CelestialBody( "Sol", null); CelestialBody earth = new CelestialBody( "Earth", sun); planet[0] = sun; planet[4] = new CelestialBody( "Mars", planet[0], 1000.0, ... ); x = earth.radius; // doesn't work, radius is private y = earth.volume(); Sysrem.out.print( sun.area() ); CelestialBody.displayVersion(); */