- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
所以我被要求为我的类(class)创建一个简单的太阳系模拟器。我们的讲师在我们的类的构造函数和方法方面制定了非常具体的指导方针。
大多数情况下,我已经成功地编写了程序而没有出现错误,但仍然有部分代码我不知道如何更改以使其根据任务工作。
这是我的代码:
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));*/
}
}
}
最后编译后,我收到两个属于代码同一部分的错误,表示承包商无法将上传应用于给定类型。如果我没有看到明显的错误,我很抱歉,我花了整整一个晚上写这篇文章。感谢您提前提供的帮助。
最佳答案
我将只处理编译错误,因为这就是问题所在,并忽略代码如何工作,但会尝试使其尽可能接近工作。
好吧,我看到的第一个错误是缺少构造函数!您没有定义一个接受 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;
}
}
}
关于java - 如何修复错误 "contractor can' 并将上传应用于给定类型”?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53718245/
在下面的代码中,我得到一个 uninitialized value警告,但仅限于第二个 given/when例子。为什么是这样? #!/usr/bin/env perl use warnings; u
整个“开关”功能是否已成为实验性的?在没有 Perl 的 future 版本破坏我的代码的情况下,我可以依赖其中的某些部分吗?一般来说,将稳定功能更改为实验性的政策是什么? 背景use feature
有没有办法在一个条件语句中写出如下语句? a和b不能同时等于5。 (a可以是5,b可以是5,但是a AND b不能是5) 最佳答案 正如克里斯指出的那样,您要查找的是逻辑异或,相当于逻辑不等于 !=:
我正在寻找一种算法来找到给定 n 条线段的所有交点。以下是来自 http://jeffe.cs.illinois.edu/teaching/373/notes/x06-sweepline.pdf 的伪
数组中有 N 个元素。我可以选择第一项最多 N 次,第二项最多选择 N-1 次,依此类推。 我有 K 个 token 要使用并且需要使用它们以便我可以拥有最大数量的项目。 arr = [3, 4, 8
我正在尝试修复法语文本中的语法性别,想知道是否有办法从某个词条中获取所有单词的列表,以及是否可以在此类列表中进行查找? 最佳答案 尝试: import spacy lemma_lookup = spa
我正在为 Win32 编写一个简单的自动化测试应用程序。它作为一个单独的进程运行,并通过 Windows API 访问目标应用程序。我可以阅读窗口层次结构,查找标签和文本框,并通过发送/发布消息等来单
在 nodeJs 中使用 Sequelize 时,我从 Sequelize 收到此错误,如下所示: { [SequelizeUniqueConstraintError: Validation erro
本文https://arxiv.org/pdf/1703.10757.pdf使用回归激活映射 (RAM) - 而不是类激活映射 (CAM) 来解决问题。有几篇文章描述了如何实现 CAM。但是我找不到
我正在研究 Mach 动态链接器 dyld。这个问题适用于所有 Apple 平台,但很高兴得到特定于平台的答案;我正在使用 ObjC,但如果对你有用的话,我也很乐意翻译 Swift。 The rele
我有一个包含数千个 Instagram 用户 ID 的列表。我如何获得他们的 Instagram 用户名/句柄? 最佳答案 你必须使用这个 Instagram API: https://api.ins
我在下面的代码: def main(args: Array[String]) { val sparkConf = new SparkConf().setAppName("Spark-Hbase").s
我有一个表格,其中包含从 1 到 10 的数字。(从 D2 到 M2) 假设A1中有03/09/2019 并且在B1中有06/09/2019 并且在C1中有Hello 在A 列中,我有多个系列的单词,
我想在给定服务对应的 URI 的情况下检索服务的注释(特别是 @RolesAllowed )。这是一个例子: 服务: @GET @Path("/example") @RolesAllowed({ "B
我看到 OraclePreparedStatementexecuteQuery() 表现出序列化。也就是说,我想使用相同的连接对 Oracle 数据库同时运行两个查询。然而,OraclePrepare
import java.util.Scanner; public class GeometricSumFromK { public static int geometricSum(int k,
我创建了一个抽象基类Page,它说明了如何构建动态网页。我正在尝试想出一种基于作为 HttpServletRequest 传入的 GET 请求生成 Page 的好方法。例如... public cla
我的字符串是一条短信,采用以下两种格式之一: 潜在客户短信: 您已收到 1 条线索 标题:我的领导 潜在客户 ID:12345-2365 警报设置 ID:890 短信回复: 您已收到 1 条回复 标题
我在 python 中有以下代码: class CreateMap: def changeme(listOne, lisrTwo, listThree, listFour, listfive):
这是在 Hibernate 上运行的 JPA2。 我想检索相同实体类型的多个实例,给定它们的 ID。其中许多已经在持久性上下文和/或二级缓存中。 我尝试了几种方法,但似乎都有其缺点: 当我使用 ent
我是一名优秀的程序员,十分优秀!