数组未正确更新对象

时间:2015-12-07 00:14:15

标签: java

因此,我在SolarSystem类中的planetArray对象数组不会使用分配的对象进行更新。真的只是在寻找一条线索,告诉我这样做的方式有什么问题。这是一个课程作业,所以我不是在寻找一个完全重写我的作品的人,只是关于具体问题的一些指示。

以下是我们的讲师使用的自动测试:

/*This is the automatic test class for CS-110 coursework 2. The output of the student's program
 * under test should match the string TARGET_OUTPUT
 */
public class AutoTest {

    static final String TARGET_OUTPUT =
            "Our System\n"
            + "Planet Mercury has a mass of 0.055 Earths, is 0.387AU from its star, and orbits in 88.025 days\n"
            + "Planet Venus has a mass of 0.815 Earths, is 0.723AU from its star, and orbits in 224.629 days\n"
            + "Planet Earth has a mass of 1.0 Earths, is 1.0AU from its star, and orbits in 365.25 days\n"
            + "Planet Mars has a mass of 0.107 Earths, is 1.52AU from its star, and orbits in 1.874 years\n"
            + "Planet Jupiter has a mass of 317.8 Earths, is 5.2AU from its star, and orbits in 11.858 years\n"
            + "Planet Saturn has a mass of 95.2 Earths, is 9.58AU from its star, and orbits in 29.652 years\n"
            + "Planet Uranus has a mass of 14.5 Earths, is 19.2AU from its star, and orbits in 84.13 years\n"
            + "Planet Neptune has a mass of 17.1 Earths, is 30.05AU from its star, and orbits in 164.728 years\n";

    public static void main(String[] args) {
        SolarSystem ourSystem = new SolarSystem("Our System");

        //Add planets in our solar system
        ourSystem.addPlanet("Mercury", 0.0553, 0.387);
        ourSystem.addPlanet("Venus", 0.815, 0.723);
        ourSystem.addPlanet("Earth", 1.0, 1.0);
        ourSystem.addPlanet("Mars", 0.107, 1.52);
        ourSystem.addPlanet("Jupiter", 317.8, 5.20);
        ourSystem.addPlanet("Saturn", 95.2, 9.58);
        ourSystem.addPlanet("Uranus", 14.5, 19.20);
        ourSystem.addPlanet("Neptune", 17.1, 30.05);

        //Check the output
        if(ourSystem.toString().equals(TARGET_OUTPUT)){
            System.out.println("Pass!");
        } else {
            System.out.println("Fail!\n*****");
            System.out.println("Expected output:\n");
            System.out.println(TARGET_OUTPUT);
            System.out.println("\n\nActual output:\n");
            System.out.println(ourSystem);
        }

    }
}

这是我的SolarSystem课程:

public class SolarSystem{
    public static final int PLANET_MAX = 10;
    public static int planetCount = 0;
    String systemName;
    Planet[] planetArray = new Planet[PLANET_MAX];
    public SolarSystem(String name){
        systemName = name;
    }

    public void addPlanet(String planetName, double planetMass, double planetDistance){
        Planet newPlanet = new Planet(planetName, planetMass, planetDistance);
        newPlanet.calculatePeriod(planetDistance);
        planetArray[planetCount] = newPlanet;
        planetCount++;
    }

    public String toString(){
        String myString = systemName + "\n";
        for(int i = 0; i <= PLANET_MAX; i++){
            int count = 0;
            String name = planetArray[count].getPlanetName();
            double mass = planetArray[count].getPlanetMass();
            double distance = planetArray[count].getPlanetDistance();
            double period = planetArray[count].getPlanetPeriod();
            if(period < 1){
                myString = myString + "Planet " + name + " has a mass of " + mass + " Earths, is " + distance + " from its star and orbits in " + period + " days.\n";
            }
            else{
                myString = myString + "Planet" + name + "has a mass of" + mass + "Earths, is" + distance + "from its star and orbits in" + period + "years.\n";
            }
            count++;
        }
        return myString;
    }
}

这是我的星球

public class Planet{
    private String planetName;
    private double planetMass;
    private double planetDistance;
    private double planetPeriod;

    public Planet(String planetName, double planetMass, double planetDistance){
        this.planetName = planetName;
        this.planetMass = planetMass;
        this.planetDistance = planetDistance;
    }
    //sets the period for the current object
    public void calculatePeriod(double planetDistance){
        double period = Math.sqrt(planetDistance*planetDistance*planetDistance);
        setPlanetPeriod(period);
    }

    //accessors
    public String getPlanetName(){
        return planetName;
    }
    public double getPlanetMass(){
        return planetMass;
    }
    public double getPlanetDistance(){
        return planetDistance;
    }
    public double getPlanetPeriod(){
        return planetPeriod;
    }

    //mutators
    public void setPlanetName(String planetName){
        this.planetName = planetName;
    }
    public void setPlanetMass(double planetMass){
        this.planetMass = planetMass;
    }
    public void setPlanetDistance(double planetDistance){
        this.planetDistance = planetDistance;
    }
    public void setPlanetPeriod(double planetPeriod){
        this.planetPeriod = planetPeriod;
    }
}

我的输出显示数组中填充了mecury的值

1 个答案:

答案 0 :(得分:0)

它只显示水银,因为它保持0,即第一个指数。此外,在for循环中它应该小于PLANET_MAX,因为这是元素的数量(长度)。否则你会得到java.lang.arrayindexoutofboundsexception。这样做:

public String toString(){
        String myString = systemName + "\n";
        for(int i = 0; i < PLANET_MAX; i++){ //< instead of <=
            String name = planetArray[i].getPlanetName();
            double mass = planetArray[i].getPlanetMass();
            double distance = planetArray[i].getPlanetDistance();
            double period = planetArray[i].getPlanetPeriod();