首页 > 解决方案 > 如何修复错误“承包商不能将上传应用到给定类型”?

问题描述

所以我被要求为我的课程创建一个简单的太阳系模拟器。我们的讲师就构造函数的结构和类的方法制定了非常具体的指导方针。

大多数情况下,我已经设法编写程序而没有出现错误,但仍有部分代码我不知道如何更改以使其根据任务工作。

这是我的代码:

import java.util.ArrayList;

public class SolarSystem {
    private String solarName;
    private static double luminosity;
    private ArrayList<Planet> planetList = new ArrayList<>();   

    public void setSolarSystem (String solarName, double luminosity) {
        this.solarName = solarName;
        SolarSystem.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(){
        return getsolarName() + "\n" + 
               "Planet "+ Planet.getPlanetname() +
               " has a mass of " + Planet.getma() + 
               " Earths, is " + Planet.getdist() + 
               " from its star, and orbits in " + Planet.getPeriod() +
               " years: could be habitable? " + Planet.getHabitable() + "\n";

    }
    public static double getLuminosity() {
        return luminosity;
    }
    public static void setLuminosity(double luminosity) {
        SolarSystem.luminosity = luminosity;
    }

    static class Planet {
        private static String planetname;
        private static double ma;
        private static double dist;
        private static double period;
        private static String habitable;
        private double sqlum;
        private static double luminos;
        SolarSystem system;
        public Planet(String name, double mass, double distance) {
            setPlanetname(name);
            ma=mass;
            dist=distance;
            SolarSystem.setLuminosity(luminos);
            period = Math.sqrt(distance*distance*distance);
            sqlum = 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 String getdist() {
            return null;
        }
        public static String getma() {
            return null;
        }
        public static String getPlanetname() {
            return planetname;
        }
        public void setPlanetname(String planetname) {
            Planet.planetname = planetname;
        }
        public static double getPeriod() {
            return period;
        }
        public void setPeriod(double period) {
            Planet.period = period;
        }
        public static String getHabitable() {
            return habitable;
        }
        public static void setHabitable(String habitable) {
            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));*/
    }
}

}

最后编译后,我得到 2 个属于代码同一部分的错误,说承包商不能将上传应用到给定的类型。如果我没有看到明显的错误,我很抱歉,我花了整夜写这篇文章。提前感谢您的帮助。

标签: java

解决方案


我将只处理编译错误,因为这就是问题所在,并忽略代码的工作方式,但会尽量使其接近工作。

好吧,我看到的第一个错误是缺少构造函数!您没有定义一个以String, Double参数为参数的构造函数,所以就这样吧。

SolarSystem(String solarName, double luminosity) {
    this.solarName = solarName;
    this.luminosity = luminosity;
}

您试图在通过实例调用的方法中使用静态值,我已经删除了所有静态值并静态地修复了您所做的调用值。

你的toString()方法 concat 完全搞砸了,所以我们也会解决这个问题。

所以在这里,没有编译错误的 SolarSystem 类。

import java.util.ArrayList;

public class SolarSystem {
    private double luminosity;
    private String solarName;
    private ArrayList<Planet> planetList = new ArrayList<>();

    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 void setSolarSystem(String solarName, double luminosity) {
        this.solarName = solarName;
        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() {
        Planet planet = planetList.get(0);
        return getsolarName() + "\n" + "Planet " + planet.getPlanetname() +
                " has a mass of " + planet.getma() + " Earths, is " +
                planet.getdist() + " from its star, and orbits in " +
                planet.getPeriod() +
                " years: could be habitable? " + planet.getHabitable() + "\n";

    }

    class Planet {
        SolarSystem system;
        private String planetname;
        private double ma;
        private double dist;
        private double period;
        private String habitable;
        private double luminos;
        private double sqlum;

        public Planet(String name, double mass, double distance) {
            setPlanetname(name);
            ma = mass;
            dist = distance;
            setLuminosity(luminos);
            period = java.lang.Math.sqrt(distance * distance * distance);
            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 String getdist() {
            return null;
        }

        public String getma() {
            return null;
        }

        public String getPlanetname() {
            return planetname;
        }

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

        public double getPeriod() {
            return period;
        }

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

        public String getHabitable() {
            return habitable;
        }

        public void setHabitable(String habitable) {
            this.habitable = habitable;
        }
    }
}

推荐阅读