2015-04-24 225 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