2015-04-24 189 views
-2

我的问题是我应该研究每CelestialObject的大小和它离太阳距离(行星)和月亮的距离行星的距离扩大宇宙

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

我有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 
     } 

    } 
+2

如果实现全都一样,为什么要使'calculateDistance'方法抽象?将它的实现移回到类中,使该类成为一个接口。然后,你可以调用你的物体层次结构来从'galaxy' - > ...->'(行星或月亮或任何其他天体)获取.calculateDistance(任何其他天体);' –

+1

我不是指听起来很愚蠢,但你是什么意思把它移回课堂。我是否将它分解为扩展天体的类? – jc38723

+1

将方法的主体移动到原始的CelestialBody类中(因为在每个覆盖中都是相同的)。使类和方法不抽象;使该类成为一个接口。 –

回答

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; 
    } 

} 

使星月星继承了该类

在各自的类别中删除从CelestialObject &认沽类的属性。如果你想要的话,你可以在各自的类中覆盖calculateDistanceBetweenCelestialObject方法。

+0

如果你不介意我问你如何让一个类成为一个接口?而且会。 abstract public double calculateDistanceBetweenCelestialObject(CelestialObject otherObj);留在新的界面类? – jc38723