title: Calling an instance method from a different class
body: <p>The purpose of this program is to test another program I created.</p>
  
  <p>It's called <code>ComplexNumber</code>. This class has everything from add, multiply, dividing complex numbers numbers and stuff and they are all in methods. The teacher wants us to create a testing class, here is what I have so far.</p>
  
  <p>The problem I am having is calling the methods from the <code>ComplexNumber</code> class. For example: I tried calling the <code>plus</code> method, this method takes in two <code>ComplexNumber</code>s and adds them up. So far I've been testing these methods using the interaction panel and it has worked great. The way I called them in the interaction panel was by doing <code>first.plus(Second)</code> and this would give the final values.</p>
  
  <p>On the testing class, I am having difficulty calling the methods.<br>
  I know that I need the class name.</p>
  
  <p>I tried:  </p>
  
  <pre><code>ComplexNumber.first.plus(second)
  </code></pre>
  
  <p>But it didn't work.<br>
  How can I do it?</p>
  
  <p>Here is my code:</p>
  
  <pre><code>class TestComplexNumber
  {
      double real;         
      double imag;
  
      public TestComplexNumber(double a, double b)
      {
          this.real=a;
          if ((b&lt;1000)&amp;&amp;(b&gt;-1000))
              this.imag=b;
          else
          {
              this.imag=0;
              System.out.println("The value you typed in for imag is &lt;1000 or &gt;-1000, value of imag is assigned the value of 0.");
          }
      }
  
      public String toString()
      {
          double real,imag;
          real=this.real;
          imag=this.imag;
  
          if (((real&lt;0)||(real&gt;0))&amp;&amp;(imag%1!=0))
          {
              if (roundThreeDecimals(imag)&gt;0)
                  return ""+roundThreeDecimals(real)+"+"+roundThreeDecimals(imag)+"i";
              else
                  return ""+roundThreeDecimals(real)+""+roundThreeDecimals(imag)+"i";
          }
          else if ((real%1!=0)&amp;&amp;(imag!=0))
              return ""+roundThreeDecimals(real)+"+"+(int)imag+"i";
          else if((real==0)&amp;&amp;(imag%1!=0))
              return ""+imag+"i";        
          else if ((real==0)&amp;&amp;(imag !=0))
              return ""+(int)imag+"i";
          else if ((imag==0)&amp;&amp;(real!=0))
               return ""+(int)real+"";
          else if (((real&lt;0)||(real&gt;0))&amp;&amp;(imag&lt;0))
              return ""+(int)real+"-"+(int)Math.abs(imag)+"i";
          else if((real!=0)&amp;&amp;(imag!=0))
              return ""+(int)real+"+"+(int)imag+"i";
          else 
              return "";
       }
  
       public static double roundThreeDecimals(double c)
       {
           double temp = c*1000;
           temp = Math.round(temp);
           temp = temp /1000;
           return temp;
       }  
  
      public static void main(String args[])
      {
          for(int i=0;i&lt;1;i++)
          {
              //Testing decimal values
          TestComplexNumber first=new TestComplexNumber((int)(Math.random()*100)-(int)(Math.random()*100),(Math.random()*100));
              TestComplexNumber second=new      TestComplexNumber((Math.random()*100),(Math.random()*100)-(int)(Math.random()*100));
              //Testing whole values
              TestComplexNumber third=new TestComplexNumber((int)(Math.random()*100)-(int)(Math.random()*100),(int)(Math.random()*100));
              TestComplexNumber fourth=new TestComplexNumber((Math.random()*100)-(int)(Math.random()*100),(int)(Math.random()*100));
  
              System.out.println(first);
              System.out.println(second);
              System.out.println(third);
              System.out.println(fourth);      
              System.out.println("Test value for plus:"+first+second+" which added="+plus(second));  
          }
  
      }
  }
  </code></pre>
  
  <p>Example of a method on the ComplexNumber class:</p>
  
  <pre><code>public ComplexNumber plus(ComplexNumber other) {
      ComplexNumber sum= new ComplexNumber(this.real,this.getImag());
  
      sum.real=(this.real)+(other.real);
      sum.setImag((this.getImag())+(other.getImag()));
  
      return sum;
  }
  </code></pre>