扩展宇宙

时间:2015-04-24 02:04:35

标签: java

我的问题是我应该研究每个天体物体的大小及其与太阳的距离(对于行星)以及卫星与行星的距离

计算任意两个天体之间的距离(例如,在两个行星之间或两个卫星之间)。

我有2个类设置来进行这些计算。我的问题是如何将这些称为例如,计算太阳与2个不同行星之间的距离。我是否需要创建一个对象并调用这些类,还是调用行星的对象,并计算出星号?

package universe;
    import java.util.*;

     abstract class CelestialObject {
      private String name;   
      private double size;   
      private long distance;
      public CelestialObject()             { this.name = "";   this.size = 0.0; this.distance = 0; } 
      public CelestialObject(String name)  { this.name = name; this.size = 0.0; this.distance = 0; } 
      public CelestialObject(double size)  { this.name = "";   this.size = size;this.distance = 0; } 
      public CelestialObject(int distance) { this.name = "";   this.size = 0.0; this.distance = distance; } 
      public CelestialObject(String name,  double size, int distance) { this.name = name; this.size = size; this.distance = distance; } 
      public void   setName(String name)      { this.name = name; }         
      public void   setSize(double size)      { this.size = size; }         
      public void   setDistance(long distance){ this.distance = distance; } 
      public String getName()                 { return this.name; }         
      public double getSize()                 { return this.size; }         
      public long   getDistance()             { return this.distance; }     
      abstract public double  calculateDistanceBetweenCelestialObject(CelestialObject otherObj);
    }

    class Star extends CelestialObject { 
    public double calculateDistanceBetweenCelestialObject(CelestialObject otherStar) {
    return this.getDistance() - otherStar.getDistance();
    }

    }

    class Moon extends CelestialObject { 
    public double calculateDistanceBetweenCelestialObject(CelestialObject otherMoon) {
    return this.getDistance() - otherMoon.getDistance();
    }

    }

    class Planet extends CelestialObject {             
      private ArrayList <Moon> moon = new ArrayList <Moon>() ;                            

      public Planet() {}                                         
      public Planet(Moon moon)          { this.moon.add(moon); } 
      public Planet(ArrayList <Moon> moon)
                                        { this.moon = moon;    } 
      public void setMoon(Moon moon)    { this.moon.add(moon); } 
      public void setMoon(ArrayList <Moon> moon)
                                        { this.moon = moon;    } 
      public ArrayList <Moon> getMoon() { return this.moon;    } 
      public Moon getMoon(int position) { return this.moon.get(position); }  
      public double  calculateDistanceBetweenCelestialObject(CelestialObject otherPlanet) {
          return this.getDistance() - otherPlanet.getDistance();}
    }

    class SolarSystem {

      private Star star;
      private ArrayList <Planet> planet = new ArrayList <Planet>() ; 

      public SolarSystem()                 {}                    
      public SolarSystem(Star star)        { this.star = star; Planet planet; } 
      public SolarSystem(Star star, Planet planet)
                                           { this.star = star; this.planet.add(planet); } 
      public SolarSystem(Star star, ArrayList <Planet> planet)
                                           { this.star = star; this.planet = planet;    } 
      public void setStar(Star star)       { this.star = star; } 
      public Star getStar()                { return this.star; } 

      public void setPlanet(Planet planet)    { this.planet.add(planet); } 
      public void setPlanet(ArrayList <Planet> planet)
                                        { this.planet = planet;    } 
      public ArrayList <Planet> getPlanet() { return this.planet;    } 
      public Planet getPlanet(int position) { return this.planet.get(position); }  
    } 

    class Galaxy {
    SolarSystem solarSystem;
    public Galaxy()                 {}                    
      public Galaxy(SolarSystem solarSystem)        { this.solarSystem = solarSystem; } 

      public void setSolarSystem(SolarSystem solarSystem)       { this.solarSystem = solarSystem; } 
      public SolarSystem getSolarSystem()                { return this.solarSystem; } 
    }

    public class Universe {
      Galaxy galaxy;

      public Universe()                 {}                   
      public Universe(Galaxy galaxy)        { this.galaxy = galaxy; } 

      public void setGalaxy(Galaxy galaxy)       { this.galaxy = galaxy; } 
      public Galaxy getGalaxy()                { return this.galaxy; } 




        public static void main(String[] args) {
            Star calc = new Star();

            Moon calc2 = new Moon();

            Galaxy MilkyWay = new Galaxy();

            SolarSystem InterStellerSpace = new SolarSystem();
            MilkyWay.setSolarSystem(InterStellerSpace);

            Star Sun = new Star();
            Sun.setName("SOL");
            Sun.setSize(864938);
            InterStellerSpace.setStar(Sun);

            Planet Earth = new Planet();
            Earth.setName("Blue Planet");
            Earth.setSize(3959);
            Earth.setDistance(92960000);
            InterStellerSpace.setPlanet(Earth);

            Moon BlueMoon = new Moon();
            BlueMoon.setName("Blue Moon");
            BlueMoon.setSize(1097.6);
            BlueMoon.setDistance(238900);
            Earth.setMoon(BlueMoon);

            Planet Mars = new Planet();
            Mars.setName("Red Planet");
            Mars.setSize(2106);
            Mars.setDistance(141600000);
            InterStellerSpace.setPlanet(Mars);

            Moon Phobos = new Moon();
            Phobos.setName("Phobos");
            Phobos.setSize(6.9);
            Phobos.setDistance(5738);
            Mars.setMoon(Phobos);

            Moon Deimus = new Moon();
            Deimus.setName("Deimus");
            Deimus.setSize(3.9);
            Deimus.setDistance(14576);
            Mars.setMoon(Deimus);      

            System.out.println("The Solor System contains the following");
            System.out.println("-- Sun Information --");
            System.out.println("   Name    : " + MilkyWay.getSolarSystem().getStar().getName());
            System.out.println("   Size    : " + MilkyWay.getSolarSystem().getStar().getSize());
            System.out.println(" ");
            for (int i = 0; i < MilkyWay.getSolarSystem().getPlanet().size(); i ++){
                System.out.println("  ++ Planet Information ++");
                System.out.println("     Name    : " + MilkyWay.getSolarSystem().getPlanet(i).getName());
                System.out.println("     Size    : " + MilkyWay.getSolarSystem().getPlanet(i).getSize());
                System.out.println("     Distance: " + MilkyWay.getSolarSystem().getPlanet(i).getDistance());
                System.out.println(" ");

             for (int m = 0; m < MilkyWay.getSolarSystem().getPlanet(i).getMoon().size(); m ++){
                System.out.println("    !! Moon Information !!");
                System.out.println("       Name    : " + MilkyWay.getSolarSystem().getPlanet(i).getMoon(m).getName());
                System.out.println("       Size    : " + MilkyWay.getSolarSystem().getPlanet(i).getMoon(m).getSize());
                System.out.println("       Distance: " + MilkyWay.getSolarSystem().getPlanet(i).getMoon(m).getDistance());
                  System.out.println(" ");
                }
            }  

            System.out.println("Distance between" 
         //**MODIFICATION NEEDED**   
         // Calculate the distance between the two planets
        }

    }

1 个答案:

答案 0 :(得分:0)

CelestialObject类应该像

public abstract class CelestialObject {

    double distance;

    public double getDistance() {
        return distance;
    }

    public void setDistance(double distance) {
        this.distance = distance;
    }

    public double calculateDistanceBetweenCelestialObject(CelestialObject cObj) {
        distance = this.getDistance() - cObj.getDistance();
        return distance;
    }

}

Make Star Moon Planet扩展了该类

从CelestialObject&amp;中删除类属性。分别上课。如果需要,可以覆盖各个类中的calculateDistanceBetweenCelestialObject方法。