2014-10-18 79 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()); 

现在,你会得到正确的输出