新对象参数/对象构造函数设置

时间:2016-02-13 23:52:35

标签: java

所以,我试图弄清楚如何正确设置我的新对象参数以及我的对象构造函数参数。当我离开新对象行时,我得到错误消息"类KingFallingItem中的构造函数KingFallingItem不能应用于给定类型; 要求:双倍,双倍 发现:没有争论 原因:实际和正式的参数列表长度不同"

我不太确定我应该在新对象创建行的()中放置什么。我尝试了double initialPosition和initialVelocity,以及getInitPosition和getInitVelocity。我觉得我在这里忘记了一些重要的信息。任何帮助,将不胜感激。我还没有对我的程序的所有部分进行编码,所以for循环毫无意义,我想让第一部分完全正常工作。

第一个文件:

public class KingFallingItem {

    //data fields
    private final double INITIAL_POSITION;
    private final double INITIAL_VELOCITY;
    private int currentTime;
    private double currentPosition;
    private double currentVelocity;
    public static final int TERMINAL_VELOCITY = -500;

    // box contstructor
    public KingFallingItem(double initialPosition, double initialVelocity) {     
        INITIAL_POSITION = initialPosition;
        INITIAL_VELOCITY = initialVelocity;
        currentTime = 0;
        currentPosition = INITIAL_POSITION;
        currentVelocity = INITIAL_VELOCITY;                
    }
}

第二档:

import java.util.Scanner;

public class KingTrajectoryProjector {
    public static int HEIGHT_THRESHOLD = 600;

    public static void main(String[] args) {
        System.out.println("This program will calculate the position and"
            + "velocity of a alling object until it reaches "
            + HEIGHT_THRESHOLD + " feet above ground");

        getInitPosition();
        getInitVelocity();
        System.out.println();
        System.out.println();

        KingFallingItem fallingItem = new KingFallingItem();


        int count;
        for (count = 5; count >= 1; count--) {
            System.out.println("Countdown:");
            System.out.println("    " + count);
        }
    }

public static double getInitPosition() {        
    Scanner keyboard = new Scanner(System.in);
     double initialPosition;

    do {
        System.out.print("Enter an initial position"
                       + " (must be over 600.0 feet): ");
         initialPosition = keyboard.nextDouble();

    } while (initialPosition < HEIGHT_THRESHOLD);
    return initialPosition;
}

public static double getInitVelocity() {
  Scanner keyboard = new Scanner(System.in);
  double initialVelocity;

  do {
      System.out.print("Enter an initial velocity "
                     + "(-500.0 ft/sec or more: ");
     initialVelocity = keyboard.nextDouble(); 
  } while (initialVelocity < -500.0);
  return initialVelocity;
}

}

1 个答案:

答案 0 :(得分:1)

我不明白为什么这不起作用

double position = getInitPosition();
double velocity = getInitVelocity();
System.out.println();
System.out.println();

KingFallingItem fallingItem = new KingFallingItem(position, velocity);