0

配列は正しい位置で初期化され、他のすべては正常に見えます。完成した配列を印刷すると、すべての要素が同一になる理由がわかりません。

デバッグすると、新しい要素が配列上の新しい場所に保存されますが、そのオブジェクトの新しいインスタンスは作成されません。

アレイの初期化:

public class CalcFrame implements ActionListener
{
   private static Calculation[] calc_array = new Calculation[10];

アクションで...

if(ae.getSource() == calc_answer_position)
{ 
      calc_array[count] = new Calculation();
      calc_array[count].setIntVel(Double.parseDouble(init_vel_tf.getText())); 
      calc_array[count].setIntPos(Double.parseDouble(init_pos_tf.getText()));
      calc_array[count].setAccel(Double.parseDouble(acc_tf.getText()));
      calc_array[count].setTime(Double.parseDouble(time_tf.getText()));


      double ans = getArray(count).calcPosition(getArray(count).getIntVel(),
                   getArray(count).getIntPos(), getArray(count).getAccel(),
                    getArray(count).getTime());
      count++;

      int n = JOptionPane.showOptionDialog(null, "Position x(t) = " + ans,
              "Answer: Calculate Position", JOptionPane.YES_NO_OPTION,
                 JOptionPane.QUESTION_MESSAGE, null, options, options[0]);

      if (n == JOptionPane.YES_OPTION)
      {
          frame.dispose();
      }
          if (n == JOptionPane.NO_OPTION)
      {
          System.exit(0);
      } 
}

計算クラス....編集

public class Calculation 
{
   //These are the attributes for the Variables object.
   private static double initial_accel = 0.0,
                                 initial_velocity = 0.0,
                                 initial_position = 0.0,
                                 acceleration = 0.0,
                                 velocity = 0.0,
                                 position = 0.0,
                                 time = 0.0,
                                 height = 0.0,
                                 mass = 0.0,
                                 weight = 0.0,
                                 answer = 0.0;


    public Calculation() {}

    public Calculation(double initial_velocity, double initial_position, double acceleration, double time)
    {
        setIntVel(initial_velocity);
        setIntPos(initial_position);
        setAccel(acceleration);
        setTime(time);
    }

    public Calculation(double initial_velocity, double initial_position, double acceleration, double time, double position)
    {
        setIntVel(initial_velocity);
        setIntPos(initial_position);
        setAccel(acceleration);
        setPos(position);
        setTime(0);
    }

    public static double calcPosition(double in_vel, double in_pos, double acc, double time)
    {
       //x(t) = xi + vi(t) + .5(a)t^2

        double answer = .5 * time * time * acc;
        answer = (in_pos + (in_vel * time) + answer);
       return answer;
    }

    public static double calcTime(double in_vel, double in_pos, double acc, double pos)                 
    {
       //t = (-vi + sqrt(vi^2 - 2(a)xi)) / a

        double answer = (in_vel * in_vel) - (2 * acc * (in_pos - pos));
      answer = ((-1 * in_vel) + Math.sqrt(answer)) / (acc);
       return answer;
    }

    //===MUTATORS===//

    public void setIntAccel(double initial_accel) 
    {
       this.initial_accel = initial_accel;
    }

    public void setIntVel(double initial_velocity) 
    {
       this.initial_velocity = initial_velocity;
    }

    public void setIntPos(double initial_position) 
    {
       this.initial_position = initial_position;
    }

    public void setAccel(double acceleration) 
    {
       this.acceleration = acceleration;
    }

    public void setVel(double velocity) 
    {
       this.velocity = velocity;
    }

    public void setPos(double position) 
    {
       this.position = position;
    }

    public void setTime(double time) 
    {
       this.time = time;
    }

    public void setHeight(double height) 
    {
       this.height = height;
    }

    public void setMass(double mass) 
    {
       this.mass = mass;
    }

    public void setWeight(double weight) 
    {
       this.weight = weight;
    }

    public void setAnswer(double answer) 
    {
       this.answer = answer;
    }


    //===ACCESSORS===//

    public double getIntAccel()
    {
       return initial_accel;
    }

    public double getIntVel() 
    {
       return initial_velocity;
    }

    public double getIntPos() 
    {
       return initial_position;
    }

    public double getAccel()
    {
       return acceleration;
    }

    public double getVel()
    {
       return velocity;
    }

    public double getPos()
    {
       return position;
    }

    public double getTime()
    {
       return time;
    }

    public double getHeight()
    {
       return height;
    }

    public double getMass()
    {
       return mass;
    }

    public double getWeight() 
    {
       return weight;
    }

    public double getAnswer() 
    {
       return answer;
    }

    public String toString()
    {

        String result = "Initial Position: " + getIntPos();
        result += "\nAcceleration: " + getAccel();
        result += "\nInitial Velocity: " + getIntVel();
        result += "\nPosition: " + getPos();
        result += "\nTime: " + getTime();
        return result;
    }


}

CalcFrameクラス...編集

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JFrame;

public class CalcFrame implements ActionListener
{
   private static Calculation[] calc_array = new Calculation[10];  

    private JLabel init_vel_label,
                   init_pos_label,
                        acc_label,
                        time_label,
                        pos_label;

    private JButton calc_answer_time,
                    calc_answer_position;

    private static JFrame frame;

    private JTextField init_vel_tf,
                       init_pos_tf,
                             acc_tf,
                             time_tf,
                             pos_tf;

    private static int count = 0;

   //-----------------------------------------------------------------
   //  Constructor: Sets up the main GUI components.
   //-----------------------------------------------------------------
   public CalcFrame(int operation)
   {
      if(operation == 2)
           CalcTime();
        if(operation == 1)
           CalcPosition();
   }

    public void CalcTime()
   {
      frame = new JFrame ("Quadratic Calculator");
      frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new FlowLayout());


        Toolkit tk = Toolkit.getDefaultToolkit();
       Dimension d = tk.getScreenSize();

        init_vel_label = new JLabel ("Initial Velocity");
        init_pos_label = new JLabel ("Initial Position");
        acc_label = new JLabel ("Acceleration");
        pos_label = new JLabel ("Position at time (t)");

        //textfields
      init_vel_tf = new JTextField (10);
        init_pos_tf = new JTextField (10);
        acc_tf = new JTextField (10);
        pos_tf = new JTextField (10);

        //button
        calc_answer_time = new JButton("Calculate");
      calc_answer_time.addActionListener(this);


      frame.add (init_vel_label);
        frame.add (init_vel_tf);
        frame.add (init_pos_label);
        frame.add (init_pos_tf);
        frame.add (acc_label);
        frame.add (acc_tf);
        frame.add (pos_label);
        frame.add (pos_tf);
        frame.add (calc_answer_time);


        frame.setSize(new Dimension(200, 275));
      frame.pack();
        frame.setResizable(false);
        frame.setLocation((d.width/2)-380, (d.height/2)-200);
      frame.setVisible(true);
      frame.setBackground (new Color(0,0,156));
        frame.getContentPane();
   }

    public void CalcPosition()
   {
      frame = new JFrame ("Quadratic Calculator");
      frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new FlowLayout());


        Toolkit tk = Toolkit.getDefaultToolkit();
       Dimension d = tk.getScreenSize();

        init_vel_label = new JLabel ("Initial Velocity");
        init_pos_label = new JLabel ("Initial Position");
        acc_label = new JLabel ("Acceleration");
        time_label = new JLabel ("Time (or change in time)");

        //textfields
      init_vel_tf = new JTextField (10);
        init_pos_tf = new JTextField (10);
        acc_tf = new JTextField (10);
        time_tf = new JTextField (10);

        //button
        calc_answer_position = new JButton("Calculate");
      calc_answer_position.addActionListener(this);


      frame.add (init_vel_label);
        frame.add (init_vel_tf);
        frame.add (init_pos_label);
        frame.add (init_pos_tf);
        frame.add (acc_label);
        frame.add (acc_tf);
        frame.add (time_label);
        frame.add (time_tf);
        frame.add (calc_answer_position);


        frame.setSize(new Dimension(200, 275));
      frame.pack();
        frame.setResizable(false);
        frame.setLocation((d.width/2)-380, (d.height/2)-200);
      frame.setVisible(true);
      frame.setBackground (new Color(0,0,156));
        frame.getContentPane();
   }

    public static void sort()
    {
         int i = 0;
         int j = 0;
         Calculation k;
         for(i = getSize() - 1; i >= 0; i--)
         {
             for(j = 0; j <= i - 1; j++)
              {
                  if(getArray(j).getIntVel() > getArray(j + 1).getIntVel())
                    {
                        k = getArray(j);
                         setArray(j, getArray(j + 1));
                         setArray(j + 1, k);
                    }
              }
         }
    }

    public static Calculation getArray(int i)
    {
       return calc_array[i];
    }

    public static Calculation[] getEntireArray()
    {
       return calc_array;
    }

    public static void setArray(int i, Calculation c)
    {
       calc_array[i] = c;
    }

    public static int getSize()
    {
       return count;
    }


    public void actionPerformed(ActionEvent ae)
    { 
          Object[] options = {"Main Menu",
                                "Exit"};

      if(ae.getSource() == calc_answer_position)
      { 
         calc_array[count] = new Calculation();
          calc_array[count].setIntVel(Double.parseDouble(init_vel_tf.getText())); 
          calc_array[count].setIntPos(Double.parseDouble(init_pos_tf.getText()));
          calc_array[count].setAccel(Double.parseDouble(acc_tf.getText()));
          calc_array[count].setTime(Double.parseDouble(time_tf.getText()));


          double ans = getArray(count).calcPosition(getArray(count).getIntVel(),
                       getArray(count).getIntPos(), getArray(count).getAccel(),
                        getArray(count).getTime());
          count++;

          int n = JOptionPane.showOptionDialog(null, "Position x(t) = " + ans,
                  "Answer: Calculate Position", JOptionPane.YES_NO_OPTION,
                     JOptionPane.QUESTION_MESSAGE, null, options, options[0]);

                     if (n == JOptionPane.YES_OPTION)
                     {
                         frame.dispose();
                     }
                     if (n == JOptionPane.NO_OPTION)
                     {
                         System.exit(0);
                     }


        }

          if(ae.getSource() == calc_answer_time)
          { 

              calc_array[count] = new Calculation(Double.parseDouble(init_vel_tf.getText()), 
                       Double.parseDouble(init_pos_tf.getText()), Double.parseDouble(acc_tf.getText()),
                          0, Double.parseDouble(pos_tf.getText()));


              double ans = getArray(count).calcTime(getArray(count).getIntVel(), getArray(count).getIntPos(), getArray(count).getAccel(), getArray(count).getPos());
              count++;

              int n = JOptionPane.showOptionDialog(null, "Time t = " + ans, "Answer: Calculate Time", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);

                         if (n == JOptionPane.YES_OPTION)
                         {
                             frame.dispose();
                         }
                         if (n == JOptionPane.NO_OPTION)
                         {
                             System.exit(0);
                         }


          }

   }
}

質問に必要なのはそれだけだと思います。さらに情報が必要な場合はお知らせください。待機させていただきます。

ありがとうございました

更新されたコードは次のとおりです。

行われた唯一の修正は、Calculationクラスの静的キーワードの削除と配列への呼び出しの変更です。

例:

getArray(j).getIntVel() changed to ... calc_array[j].getIntVel()

計算クラスが更新されました...

public class Calculation 
{
   //These are the attributes for the Variables object.
   private double initial_accel = 0.0,
                                 initial_velocity = 0.0,
                                 initial_position = 0.0,
                                 acceleration = 0.0,
                                 velocity = 0.0,
                                 position = 0.0,
                                 time = 0.0,
                                 height = 0.0,
                                 mass = 0.0,
                                 weight = 0.0,
                                 answer = 0.0;


    public Calculation() {}

    public Calculation(double initial_velocity, double initial_position, double acceleration, double time)
    {
        setIntVel(initial_velocity);
        setIntPos(initial_position);
        setAccel(acceleration);
        setTime(time);
    }

    public Calculation(double initial_velocity, double initial_position, double acceleration, double time, double position)
    {
        setIntVel(initial_velocity);
        setIntPos(initial_position);
        setAccel(acceleration);
        setPos(position);
        setTime(0);
    }

    public double calcPosition(double in_vel, double in_pos, double acc, double time)
    {
       //x(t) = xi + vi(t) + .5(a)t^2

        double answer = .5 * time * time * acc;
        answer = (in_pos + (in_vel * time) + answer);
       return answer;
    }

    public double calcTime(double in_vel, double in_pos, double acc, double pos)                    
    {
       //t = (-vi + sqrt(vi^2 - 2(a)xi)) / a

        double answer = (in_vel * in_vel) - (2 * acc * (in_pos - pos));
      answer = ((-1 * in_vel) + Math.sqrt(answer)) / (acc);
       return answer;
    }

    //===MUTATORS===//

    public void setIntAccel(double initial_accel) 
    {
       this.initial_accel = initial_accel;
    }

    public void setIntVel(double initial_velocity) 
    {
       this.initial_velocity = initial_velocity;
    }

    public void setIntPos(double initial_position) 
    {
       this.initial_position = initial_position;
    }

    public void setAccel(double acceleration) 
    {
       this.acceleration = acceleration;
    }

    public void setVel(double velocity) 
    {
       this.velocity = velocity;
    }

    public void setPos(double position) 
    {
       this.position = position;
    }

    public void setTime(double time) 
    {
       this.time = time;
    }

    public void setHeight(double height) 
    {
       this.height = height;
    }

    public void setMass(double mass) 
    {
       this.mass = mass;
    }

    public void setWeight(double weight) 
    {
       this.weight = weight;
    }

    public void setAnswer(double answer) 
    {
       this.answer = answer;
    }


    //===ACCESSORS===//

    public double getIntAccel()
    {
       return initial_accel;
    }

    public double getIntVel() 
    {
       return initial_velocity;
    }

    public double getIntPos() 
    {
       return initial_position;
    }

    public double getAccel()
    {
       return acceleration;
    }

    public double getVel()
    {
       return velocity;
    }

    public double getPos()
    {
       return position;
    }

    public double getTime()
    {
       return time;
    }

    public double getHeight()
    {
       return height;
    }

    public double getMass()
    {
       return mass;
    }

    public double getWeight() 
    {
       return weight;
    }

    public double getAnswer() 
    {
       return answer;
    }

    public String toString()
    {

        String result = "Initial Position: " + getIntPos();
        result += "\nAcceleration: " + getAccel();
        result += "\nInitial Velocity: " + getIntVel();
        result += "\nPosition: " + getPos();
        result += "\nTime: " + getTime();
        return result;
    }


}

CalcFrameクラスが更新されました...

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JFrame;

public class CalcFrame implements ActionListener
{
   private static Calculation[] calc_array = new Calculation[10];  

    private JLabel init_vel_label,
                   init_pos_label,
                        acc_label,
                        time_label,
                        pos_label;

    private JButton calc_answer_time,
                    calc_answer_position;

    private static JFrame frame;

    private JTextField init_vel_tf,
                       init_pos_tf,
                             acc_tf,
                             time_tf,
                             pos_tf;

    private static int count = 0;

   //-----------------------------------------------------------------
   //  Constructor: Sets up the main GUI components.
   //-----------------------------------------------------------------
   public CalcFrame(int operation)
   {
      if(operation == 2)
           CalcTime();
        if(operation == 1)
           CalcPosition();
   }

    public void CalcTime()
   {
      frame = new JFrame ("Quadratic Calculator");
      frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new FlowLayout());


        Toolkit tk = Toolkit.getDefaultToolkit();
       Dimension d = tk.getScreenSize();

        init_vel_label = new JLabel ("Initial Velocity");
        init_pos_label = new JLabel ("Initial Position");
        acc_label = new JLabel ("Acceleration");
        pos_label = new JLabel ("Position at time (t)");

        //textfields
      init_vel_tf = new JTextField (10);
        init_pos_tf = new JTextField (10);
        acc_tf = new JTextField (10);
        pos_tf = new JTextField (10);

        //button
        calc_answer_time = new JButton("Calculate");
      calc_answer_time.addActionListener(this);


      frame.add (init_vel_label);
        frame.add (init_vel_tf);
        frame.add (init_pos_label);
        frame.add (init_pos_tf);
        frame.add (acc_label);
        frame.add (acc_tf);
        frame.add (pos_label);
        frame.add (pos_tf);
        frame.add (calc_answer_time);


        frame.setSize(new Dimension(200, 275));
      frame.pack();
        frame.setResizable(false);
        frame.setLocation((d.width/2)-380, (d.height/2)-200);
      frame.setVisible(true);
      frame.setBackground (new Color(0,0,156));
        frame.getContentPane();
   }

    public void CalcPosition()
   {
      frame = new JFrame ("Quadratic Calculator");
      frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new FlowLayout());


        Toolkit tk = Toolkit.getDefaultToolkit();
       Dimension d = tk.getScreenSize();

        init_vel_label = new JLabel ("Initial Velocity");
        init_pos_label = new JLabel ("Initial Position");
        acc_label = new JLabel ("Acceleration");
        time_label = new JLabel ("Time (or change in time)");

        //textfields
      init_vel_tf = new JTextField (10);
        init_pos_tf = new JTextField (10);
        acc_tf = new JTextField (10);
        time_tf = new JTextField (10);

        //button
        calc_answer_position = new JButton("Calculate");
      calc_answer_position.addActionListener(this);


      frame.add (init_vel_label);
        frame.add (init_vel_tf);
        frame.add (init_pos_label);
        frame.add (init_pos_tf);
        frame.add (acc_label);
        frame.add (acc_tf);
        frame.add (time_label);
        frame.add (time_tf);
        frame.add (calc_answer_position);


        frame.setSize(new Dimension(200, 275));
      frame.pack();
        frame.setResizable(false);
        frame.setLocation((d.width/2)-380, (d.height/2)-200);
      frame.setVisible(true);
      frame.setBackground (new Color(0,0,156));
        frame.getContentPane();
   }

    public static void sort()
    {
         int i = 0;
         int j = 0;
         Calculation k;
         for(i = count - 1; i >= 0; i--)
         {
             for(j = 0; j <= i - 1; j++)
              {
                  if(calc_array[j].getIntVel() > calc_array[j+1].getIntVel())
                    {
                        k = calc_array[j];
                         calc_array[j] = calc_array[j+1];
                         calc_array[j + 1] = k;
                    }
              }
         }
    }

    public static Calculation getArray(int i)
    {
       return calc_array[i];
    }

    public Calculation[] getEntireArray()
    {
       return calc_array;
    }

    public void setArray(int i, Calculation c)
    {
       calc_array[i] = c;
    }

    public static int getSize()
    {
       return count;
    }


    public void actionPerformed(ActionEvent ae)
    { 
          Object[] options = {"Main Menu",
                                "Exit"};

      if(ae.getSource() == calc_answer_position)
      { 
         calc_array[count] = new Calculation();
          calc_array[count].setIntVel(Double.parseDouble(init_vel_tf.getText())); 
          calc_array[count].setIntPos(Double.parseDouble(init_pos_tf.getText()));
          calc_array[count].setAccel(Double.parseDouble(acc_tf.getText()));
          calc_array[count].setTime(Double.parseDouble(time_tf.getText()));


          double ans = getArray(count).calcPosition(getArray(count).getIntVel(),
                       getArray(count).getIntPos(), getArray(count).getAccel(),
                        getArray(count).getTime());
          count++;

          int n = JOptionPane.showOptionDialog(null, "Position x(t) = " + ans,
                  "Answer: Calculate Position", JOptionPane.YES_NO_OPTION,
                     JOptionPane.QUESTION_MESSAGE, null, options, options[0]);

                     if (n == JOptionPane.YES_OPTION)
                     {
                         frame.dispose();
                     }
                     if (n == JOptionPane.NO_OPTION)
                     {
                         System.exit(0);
                     }


        }

          if(ae.getSource() == calc_answer_time)
          { 

              calc_array[count] = new Calculation(Double.parseDouble(init_vel_tf.getText()), 
                       Double.parseDouble(init_pos_tf.getText()), Double.parseDouble(acc_tf.getText()),
                          0, Double.parseDouble(pos_tf.getText()));


              double ans = getArray(count).calcTime(getArray(count).getIntVel(), getArray(count).getIntPos(), getArray(count).getAccel(), getArray(count).getPos());
              count++;

              int n = JOptionPane.showOptionDialog(null, "Time t = " + ans, "Answer: Calculate Time", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);

                         if (n == JOptionPane.YES_OPTION)
                         {
                             frame.dispose();
                         }
                         if (n == JOptionPane.NO_OPTION)
                         {
                             System.exit(0);
                         }


          }

   }
}
4

2 に答える 2

4

計算クラスを提供できますか?静的な問題である可能性があります


編集:

変数宣言で静的を削除してみてください

そこの

//These are the attributes for the Variables object.
   private **static** double initial_accel = 0.0,

また、これらの変数が表示されるすべてのメソッドの静的を削除する必要があります

静的変数やメソッドはスタンドアロンであり、現在のインスタンスに依存しないと考えてください。つまり、静的属性は同じインスタンスのオブジェクト間で常に同じであり、インスタンスなしでそれらの属性を参照することもできます。

Person person1 = new Person();
person1.name = "User";
Person person2 = new Person()
person2.name = "Admin";

名前が静的な場合、person1.nameは常にperson2.nameになります。それ以外の場合は、独自の名前になります。

于 2012-05-16T21:48:10.803 に答える
2

コード内のどこかでメソッドsort()を呼び出していますか?私は以下の内部ソートメソッドを見ているので

setArray(j, getArray(j + 1));
setArray(j + 1, k);

Calculation k;配列からgetArray(int i)メソッドを介して参照を割り当てているため です。参照で遊んでいるので、ここで何かが起こるかもしれません。それは単なる推測です。

于 2012-05-16T22:02:21.793 に答える