建模一个复数,我在创建类时缺少主要方法。有人可以指导我如何解决它吗?

时间:2019-12-03 20:36:52

标签: java

  

您的任务是创建一个将对复数建模的类。

     

复数是a + bi形式的数字,其中a是“实数”   b是“虚构”部分。它基于数学   前提是i2 = -1。因此,像分数类一样,您将定义两个   对于实数和虚数,将数据成员加倍。

我将从教授提供给我们的驱动程序文件中提取信息。我在编译时没有问题,但是在运行程序时有问题。我从JGrasp获得一个弹出窗口,其中指出“在文件中找不到Main方法,Java FX应用程序,Applet或Midlet”。我假设我需要在创建的类中放入一个main方法,但是我不确定在哪里放置或如何标记/定义它。有人可以指导我解决这个问题吗?

谢谢,下面是我的代码。

class Complex {

   private double real;

   private double imaginary;

   final double LIMIT = 10;// final means that this object stays the same for all.

   Complex() {//constructors (there are 3, progressing in size)

      real = 0;
      imaginary = 0;
   }

   Complex(double actual) {// parameter calls what I'm assigning real to

      this.real = actual;
      imaginary = 0;

   }

   Complex(double actual, double fake) {

      this.real = actual;
      this.imaginary = fake;

   }

   public double getReal() {//accessors (there are 2, one for each parameter)

      return real;

   }

   public double getImaginary() {

      return imaginary;

   }

   public void setReal(double actual) {// sets real to actual, mutator.

      this.real = actual;

   }

   public void setImaginary(double fake) {// sets imaginary to fake, mutator.

      this.imaginary = fake;

   }

   public String toString() {//returns a String neatly in the form a + bi

      return real + " " + imaginary + "i";

   }

   public boolean equals(Complex complexNumber) {

      if(real == complexNumber.real && imaginary == complexNumber.imaginary) {//takes a complex number as a parameter type and 
      //returns a boolean of true if the calling object is equal to the parameter.

         return true;

      }

      else {

         return false;

      }
   }   

      public Complex add(Complex complexNumber) {

         Complex temp = new Complex (0.0,0.0);

         temp.real = real + complexNumber.real;

         temp.imaginary = imaginary + complexNumber.imaginary;

         return temp;

      }

      public Complex add (double val) {

         Complex temp = new Complex(0.0, 0.0);

         temp.real = real + val;

         temp.imaginary = imaginary + val;


         return temp;

      }



// Override method to add fist parameter Complex object value with second Complex parameter object value

         public static Complex add(Complex complexNumber, Complex c2) {

            Complex temp = new Complex(0.0, 0.0);

            temp.real = complexNumber.real + c2.real;

            temp.imaginary = complexNumber.imaginary + c2.imaginary;

            return temp;

         }// End of method



// Method to check the size of implicit Complex object is greater than the LIMIT

// If greater than return true

// Otherwise return false

         public boolean isBig() {

// Calculates size

            double size = Math.sqrt((Math.pow(real, 2) + Math.pow(imaginary, 2)));

// Checks if size is greater than LIMIT return true

            if(size > LIMIT)

               return true;

// Otherwise not big return false

            else

               return false;

         }// End of method


   }   // End of class

1 个答案:

答案 0 :(得分:1)

您不应将应用程序的主方法放在此类中,而应将其单独放置,例如应用,位于相同包或更高包层次中,且正文类似

public class App {
    public static void main(String[] args){
        System.out.println(new Complex(0.1, 0.2));
    }
}