2014-10-18 76 views
0

我的問題是,對於getTime();命令,你需要所有的速度,處理,xcord,ycord和terrainDifficultry變量來得到答案,但我只能調用getTime();來自mb1類。基本上,當我到達System.out getTime()時,我一直得到0.0,我不知道如何解決它。Java-涉及對象和多個類

import java.util.Scanner; 
public class Main_MoonRace { 

    public static void main(String[] args) 
    { 
     Scanner keyboard = new Scanner (System.in); 
     System.out.println("Enter the speed of the moonbuggy as an integer."); 
     int s = keyboard.nextInt(); 
     System.out.println("Enter the handling of the moonbuggy (between 0-0.9)"); 
     double h = keyboard.nextDouble(); 
     moonbuggy mb1 = new moonbuggy(s,h); 

     System.out.println("Enter the x-coordinate of where the moonbuggy will be headed to as an integer."); 
     int xcord = keyboard.nextInt(); 
     System.out.println("Enter the y-coordinate of where the moonbuggy will be headed to as an integer."); 
     int ycord = keyboard.nextInt(); 
     System.out.println("Enter the difficulty of the terrain that the moonbuggy will be experiencing (integer from 1-10)."); 
     int terrainDifficulty = keyboard.nextInt(); 
     MoonLocation mL1 = new MoonLocation(xcord,ycord,terrainDifficulty); 

     System.out.println(mb1.getTime()); 
    } 
} 

moonbuggy.java

public class moonbuggy {  
    private int speed = 1; 
    private double handling = 0; 
    moonbuggy(){ 
     return; 
    } 
    moonbuggy(int s, double h){ 
     speed = s; 
     handling = h; 

     return; 
    } 
    public void setSpeed (int s){ 
     speed = s; 
    } 
    public void setHandling (double h){ 
     handling = h; 
    } 
    public int getSpeed(){ 
     return speed; 
    } 
    public double getHandling(){ 
     return handling; 
    } 
    MoonLocation obj1 = new MoonLocation(); 

    public double getTime(){ 
     double time = (((obj1.getdistance())/(getSpeed()))*(obj1.getTerrain())*(1-(getHandling()))); 
     return time; 
    } 
} 

MoonLocation.java

public class MoonLocation { 
    private int x = 0; 
    private int y = 0; 
    private int terrain = 1; 

    MoonLocation(){ 
     return; 
    } 
    MoonLocation(int xcord, int ycord, int terrainDifficulty){ 
     x= xcord; 
     y = ycord; 
     terrain = terrainDifficulty; 

     return; 
    } 

    public void setX (int xcord){ 
     x = xcord; 
    } 
    public void setY (int ycord){ 
     y = ycord; 
    } 
    public void setTerrain (int terrainDifficulty){ 
     terrain = terrainDifficulty; 
    } 
    public int getX() { 
     return x; 
    } 
    public int getY() { 
     return y; 
    } 
    public int getTerrain() { 
     return terrain; 
    } 
    public double getdistance() { 
     double distance = Math.sqrt((Math.pow(x,2))+(Math.pow(y,2))); 
     return distance; 
    } 
} 
+3

你正在構建一個'MoonLocation',然後完全忽略了一個事實這是一個問題的指示...但你並沒有真正問過一個問題,這使得它很難幫助你... – 2014-10-18 14:55:26

+0

你需要給代碼moonbuggy – dave 2014-10-18 14:58:22

回答

0

看一看這部分代碼在你moonbuggy類(請注意,按照慣例類應該總是以大寫字母開頭)。

MoonLocation obj1 = new MoonLocation(); 
public double getTime(){ 
    double time = (((obj1.getdistance())/(getSpeed()))*(obj1.getTerrain())*(1-(getHandling()))); 
    return time; 
} 

你實例化一個MoonLocation不帶任何參數,那麼你訪問它在你的getTime方法。這就解釋了爲什麼你在撥打getTime時總是得到0.0。

現在修改getTime方法

public double getTime(MoonLocation location){ 
    return (((location.getdistance())/(getSpeed()))*(location.getTerrain())*(1-(getHandling()))); 
} 

注意,我去掉了時間變量,因爲它是完全地無用的存在。

,並更改您的主要

MoonLocation mL1 = new MoonLocation(xcord,ycord,terrainDifficulty); 
System.out.println(mb1.getTime()); 

MoonLocation mL1 = new MoonLocation(xcord,ycord,terrainDifficulty); 
System.out.println(mb1.getTime(mL1)); 

另外,請您moonbuggy類中刪除未使用的MoonLocation obj1 = new MoonLocation()

0

問題在於你的代碼。首先,要創建在Main_MoonRace類下main()方法MoonLocation的一個目的爲:

MoonLocation mL1 = new MoonLocation(xcord,ycord,terrainDifficulty); 

這裏創建MoonLocation的目的,並與xcord,ycord和terrainDifficulty值進行初始化。現在

,在你MoonBuggy類,你又正在創建MoonLocation的對象爲:

MoonLocation obj1 = new MoonLocation(); 

這裏,只是創建MoonLocation類的一個空對象。

現在,當你撥打:

obj1.getDistance(); It will return 0 only. 

下面是MoonBuggy類更正後的代碼。

public class Moonbuggy {  
    private int speed = 1; 
    private double handling = 0; 

    Moonbuggy(){} 

    Moonbuggy(int s, double h){ 
     speed = s; 
     handling = h; 
    } 
    public void setSpeed (int s){ 
     speed = s; 
    } 
    public void setHandling (double h){ 
     handling = h; 
    } 
    public int getSpeed(){ 
     return speed; 
    } 
    public double getHandling(){ 
     return handling; 
    } 

    private MoonLocation obj1; 

    public MoonLocation getObj1() { 
     return obj1; 
    } 

    public void setObj1(MoonLocation obj1) { 
     this.obj1 = obj1; 
    } 

    public double getTime(){ 
     double time = (((obj1.getdistance())/(getSpeed()))*(obj1.getTerrain())*(1-(getHandling()))); 
     return time; 
    } 
} 

,並在main()方法的addtion:

MoonLocation mL1 = new MoonLocation(xcord,ycord,terrainDifficulty); 
     mb1.setObj1(mL1); // set the MoonLocation object 
     System.out.println(mb1.getTime()); 

現在,你會得到正確的輸出