无法使程序更改arraylist内的对象 [英] Can't make program change object inside arraylist

查看:71
本文介绍了无法使程序更改arraylist内的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我清理了toString方法,但再次卡住了.我找不到方法更改代码以使其仅显示最后一颗行星的方式工作.我看不到问题. 从概念上讲,它应该记录所有行星,但实际上,它只记录最后一个行星几次.

So, I cleaned up the toString method, but I stuck once again. I can't find way how to change code to make it work in the way to show not only the last of the planets. I can't see the problem. In idea it should write down all the planets, but in reality it writes down only last planet several times.

这是我的代码:

         import java.util.ArrayList;

public class SolarSystem {
private static int i = 0;
private static double luminosity;
private String solarName;
private ArrayList<Planet> planetList = new ArrayList<>(i);
public static final int PLANET_MAX = 10;
public static int planetCount = 0;
SolarSystem(String solarName, double luminosity) {
    this.solarName = solarName;
    this.luminosity = luminosity;
}

public double getLuminosity() {
    return luminosity;
}

public void setLuminosity(double luminosity) {
    this.luminosity = luminosity;
}

public String getsolarName() {
    return solarName;
}

public String getsolarname() {
    return solarName;
}

public void addPlanet(String name, double mass, double distance) {
    Planet newPlanet = new Planet(name, mass, distance);
    planetList.add(newPlanet);
}

public String toString() {
    String myString = solarName + "\n";
    for(int i = 0; i < planetList.size(); i++){
        String name = Planet.getPlanetname(i);
        double mass = Planet.getma(i);
        double distance = Planet.getdist(i);
        double period = Planet.getPeriod(i);
        String habitable = Planet.getHabitable(i);
            myString = myString + " Planet " + name + " has a mass of " + mass + " Earths, is " + distance + "AU from its star, and orbits in " + period + " years: could be habitable? "+ habitable+ "\n";
    }
    return myString;
}



static class Planet {
    SolarSystem system;
    private static String Planetname;
    private static double ma;
    private static double dist;
    private static double period;
    private static String habitable;
    private double luminos;
    private double sqlum;
    public Planet(String name, double mass, double distance) {
        setPlanetname(name);
        ma = mass;
        dist = distance;
        distance=Math.round(distance*1000)/1000;
        distance=dist;
        luminosity=luminos;
        period = java.lang.Math.sqrt(dist * dist * dist);
       period= Math.round(period*1000.0)/1000.0;  
        sqlum = java.lang.Math.sqrt(luminos);
        if ((ma >= 0.6) && (ma <= 7.0) && (dist >= 0.75 * sqlum) && (dist <= 2.0 * sqlum)) {
            habitable = "yes";

        } else {
            habitable = "no";
        }
    }

    public static double getPeriod(int i) {
        // TODO Auto-generated method stub
        return period;
    }

    public static double getdist(int i) {
        return dist;
    }

    public static double getma(int i) {
        return ma;
    }

    public static String getPlanetname(int i) {
        return Planetname;
    }

    public void setPlanetname(String planetname) {
        Planet.Planetname = planetname;
    }

    public double getPeriod() {
        return period;
    }

    public void setPeriod(double period) {
        Planet.period = period;
    }

    public static String getHabitable(int i) {
        return habitable;
    }

    public String setHabitable(String habitable) {
        return Planet.habitable = habitable;
    }
}
}

这是我的测试程序:

         //Uncomment if using extra tests
        //import org.apache.commons.lang3.StringUtils;

          /*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_SUN
*/
public class AutoTest {

static final String TARGET_OUTPUT_SUN = "Our System\n"
        + "Planet Mercury has a mass of 0.055 Earths, is 0.387AU from its star, and orbits in 0.241 years: could be habitable? no\n"
        + "Planet Venus has a mass of 0.815 Earths, is 0.723AU from its star, and orbits in 0.615 years: could be habitable? no\n"
        + "Planet Earth has a mass of 1.0 Earths, is 1.0AU from its star, and orbits in 1.0 years: could be habitable? yes\n"
        + "Planet Mars has a mass of 0.107 Earths, is 1.52AU from its star, and orbits in 1.874 years: could be habitable? no\n"
        + "Planet Jupiter has a mass of 317.8 Earths, is 5.2AU from its star, and orbits in 11.858 years: could be habitable? no\n"
        + "Planet Saturn has a mass of 95.2 Earths, is 9.58AU from its star, and orbits in 29.652 years: could be habitable? no\n"
        + "Planet Uranus has a mass of 14.5 Earths, is 19.2AU from its star, and orbits in 84.13 years: could be habitable? no\n"
        + "Planet Neptune has a mass of 17.1 Earths, is 30.05AU from its star, and orbits in 164.728 years: could be habitable? no\n";

static final String TARGET_OUTPUT_TRAPPIST1 = "Trappist 1\n" +
        "Planet Trappist1b has a mass of 1.017 Earths, is 0.012AU from its star, and orbits in 0.001 years: could be habitable? no\n" +
        "Planet Trappist1c has a mass of 1.156 Earths, is 0.016AU from its star, and orbits in 0.002 years: could be habitable? no\n" +
        "Planet Trappist1d has a mass of 0.297 Earths, is 0.022AU from its star, and orbits in 0.003 years: could be habitable? no\n" +
        "Planet Trappist1e has a mass of 0.772 Earths, is 0.029AU from its star, and orbits in 0.005 years: could be habitable? yes\n" +
        "Planet Trappist1f has a mass of 0.934 Earths, is 0.038AU from its star, and orbits in 0.007 years: could be habitable? yes\n" +
        "Planet Trappist1g has a mass of 1.148 Earths, is 0.049AU from its star, and orbits in 0.011 years: could be habitable? yes\n" +
        "Planet Trappist1h has a mass of 0.331 Earths, is 0.062AU from its star, and orbits in 0.015 years: could be habitable? no\n";

public static void main(String[] args) {


    //Create our solar system
    SolarSystem ourSystem = new SolarSystem("Our System",1.0);

    //Add planets in our solar system
    ourSystem.addPlanet("Mercury", 0.055, 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 for our solar system
    if (ourSystem.toString().equals(TARGET_OUTPUT_SUN)) {
        System.out.println("Solar System: Pass!");
    } else {
        System.out.println("Solar System: Fail!\n*****");
        System.out.println("Expected output:\n");
        System.out.println(TARGET_OUTPUT_SUN);
        System.out.println("\n\nActual output:\n");
        System.out.println(ourSystem.toString());
        // Uncomment if using extra tests*/
        /*System.out.println("\n\nDifferences:");
        System.out.println(StringUtils.difference(ourSystem.toString(),
        TARGET_OUTPUT_SUN));*/
    }

    System.out.println("\n\n");//blank lines to separate output

    //Create the Trappist1 system - a much dimmer star with closer planets
    SolarSystem trappist1 = new SolarSystem("Trappist 1",0.00128);

    //Add planets in Trappist 1 system
    trappist1.addPlanet("Trappist1b", 1.017, 0.012);
    trappist1.addPlanet("Trappist1c", 1.156, 0.016);
    trappist1.addPlanet("Trappist1d", 0.297, 0.022);
    trappist1.addPlanet("Trappist1e", 0.772, 0.029);
    trappist1.addPlanet("Trappist1f", 0.934, 0.038);
    trappist1.addPlanet("Trappist1g", 1.148, 0.049);
    trappist1.addPlanet("Trappist1h", 0.331, 0.062);

    //Check the output for trappist1
    if (trappist1.toString().equals(TARGET_OUTPUT_TRAPPIST1)) {
        System.out.println("Trappist1: Pass!");
    } else {
        System.out.println("Trappist1: Fail!\n*****");
        System.out.println("Expected output:\n");
        System.out.println(TARGET_OUTPUT_TRAPPIST1);
        System.out.println("\n\nActual output:\n");
        System.out.println(trappist1.toString());
        // Uncomment if using extra tests*/
        /*System.out.println("\n\nDifferences:");
        System.out.println(StringUtils.difference(ourSystem.toString(),
        TARGET_OUTPUT_TRAPPIST1));*/
    }
}

}

推荐答案

Planet类的属性声明为static意味着Planet的所有实例将共享相同的属性.

Declaring the attributes of the Planet class as static means that all instances of Planet will share the same attributes.

您应该将属性声明更改为:

You should change the attributes declaration to :

private  String Planetname;
private  double ma;
private  double dist;
private  double period;
private  String habitable;
private double luminos;
private double sqlum;

然后,您还应该更改方法签名以删除static关键字.因为由于您的属性不再是静态的,所以访问它们的方法也不会是静态的.

Then you should also change your method signatures to remove the static keyword. Because as your attributes are not anymore static the method to access them cannot be static.

示例:

public static String getPlanetname(int i) {

应更改为:

public String getPlanetname(int i) {

最后,您的for循环中遇到问题

And finally you have an issue in your for loop

for(int i = 0; i < planetList.size(); i++){
    Planet p = planetList.get(i); // <-- Here you get the Planet at the desired index
    String name = p.getPlanetname(i);  // The parameter i here is useless
    //...
}

这篇关于无法使程序更改arraylist内的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆