Thanks for taking the time. I'm a beginner and I ran into a roadblock with the following code. The assignment is to create a class Complex to add and subtract complex numbers and a Class to test it. Unfortunately, I can't get it to compile so I can debug it. The error I can't seem to get past is in the ComplexMath( firstComplexNumber, secondComplexNumber ) method call. My instructor quit teaching in the middle of the term, so any help would be greatly appreciated.

import javax.swing.JOptionPane;

public class Complex {

public static void main( String args[] ) {
	
ComplexNumber firstComplexNumber = new ComplexNumber();
ComplexNumber secondComplexNumber = new ComplexNumber();		
		
String sReal = "";
String sImaginary = "";
float fReal = 0;
float fImaginary = 0;
String result = "";
				
sReal = JOptionPane.showInputDialog( "Please enter the real part 
of your first number:" );
fReal = Float.parseFloat( sReal );		
sImaginary = JOptionPane.showInputDialog( "Please enter the 
imaginary part of your first number:" );
fImaginary = Float.parseFloat( sImaginary );
			
firstComplexNumber.setReal( fReal );
firstComplexNumber.setImaginary( fImaginary );		
					
sReal = JOptionPane.showInputDialog( "Please enter the real 
part of your second number:" );
fReal = Float.parseFloat( sReal );				
sImaginary = JOptionPane.showInputDialog( "Please enter the 
imaginary part of your second number:" );
fImaginary = Float.parseFloat( sImaginary );				
							
secondComplexNumber.setReal( fReal );
secondComplexNumber.setImaginary( fImaginary );

// *****this is the method call the compiler doesn't like*****		
result = ComplexMath( firstComplexNumber, secondComplexNumber );
			
JOptionPane.showMessageDialog( null, result, "Result", 
JOptionPane.INFORMATION_MESSAGE );			

System.exit( 0 );
}	
}
public class ComplexNumber {

private float real;
private float imaginary;
	
public void ComplexNumber() {		
real = 0;
maginary = 0;	
}
	
public float getReal() {		
return real;		
}
	
public float getImaginary() {
return imaginary;	
}
		
public void setReal( float a ){
real = a;
}	
	
public void setImaginary( float a ){
imaginary = a;
}	
	
public String ComplexMath( ComplexNumber a, ComplexNumber b ) {
	
String answer = "";
float realSum = 0;
float imaginarySum = 0;
float realDifference = 0;
float imaginaryDifference = 0;
	
realSum = a.real + b.real;
imaginarySum = a.imaginary + b.imaginary;
realDifference = a.real - b.real;
imaginaryDifference = a.imaginary - b.imaginary;
	
answer += "The sum of " + a.real + " + " + a.imaginary + 
"i and " + b.real + " + " + a.imaginary + "i is " + realSum + " + "
+ imaginarySum + "i.\n" + "The difference of " + a.real + " + " 
+ a.imaginary + "i       and " + b.real + " + " + a.imaginary + "i is " 
+ realDifference + " + " + imaginaryDifference + "i.";					 			 
return answer;
}		
}

You have to precede the method call with a reference to the Complex class. I made the ComplexMath method static, and called it using the dot operator.

import javax.swing.JOptionPane;

public class Complex {

public static void main( String args[] ) {
	
ComplexNumber firstComplexNumber = new ComplexNumber();
ComplexNumber secondComplexNumber = new ComplexNumber();	

		
String sReal = "";
String sImaginary = "";
float fReal = 0;
float fImaginary = 0;
String result = "";
				
sReal = JOptionPane.showInputDialog("Please enter the real part of your first number:" );
fReal = Float.parseFloat( sReal );		
sImaginary = JOptionPane.showInputDialog( "Please enter the imaginary part of your first number:" );
fImaginary = Float.parseFloat( sImaginary );
			
firstComplexNumber.setReal( fReal );
firstComplexNumber.setImaginary( fImaginary );		
					
sReal = JOptionPane.showInputDialog( "Please enter the real part of your second number:" );
fReal = Float.parseFloat( sReal );				
sImaginary = JOptionPane.showInputDialog( "Please enter the imaginary part of your second number:" );
fImaginary = Float.parseFloat( sImaginary );				
							
secondComplexNumber.setReal( fReal );
secondComplexNumber.setImaginary( fImaginary );

// *****this is the method call the compiler doesn't like*****		
result = ComplexNumber.ComplexMath( firstComplexNumber, secondComplexNumber );
			
JOptionPane.showMessageDialog( null, result, "Result", 
JOptionPane.INFORMATION_MESSAGE );			

System.exit( 0 );
}	
}
public class ComplexNumber {

private float real;
private float imaginary;
	
public void ComplexNumber() {		
real = 0;
imaginary = 0;	
}
	
public float getReal() {		
return real;		
}
	
public float getImaginary() {
return imaginary;	
}
		
public void setReal( float a ){
real = a;
}	
	
public void setImaginary( float a ){
imaginary = a;
}	
	
public static String ComplexMath( ComplexNumber a, ComplexNumber b ) {
	
String answer = "";
float realSum = 0;
float imaginarySum = 0;
float realDifference = 0;
float imaginaryDifference = 0;
	
realSum = a.real + b.real;
imaginarySum = a.imaginary + b.imaginary;
realDifference = a.real - b.real;
imaginaryDifference = a.imaginary - b.imaginary;
	
answer += "The sum of " + a.real + " + " + a.imaginary + 
"i and " + b.real + " + " + a.imaginary + "i is " + realSum + " + "
+ imaginarySum + "i.\n" + "The difference of " + a.real + " + " 
+ a.imaginary + "i       and " + b.real + " + " + a.imaginary + "i is " 
+ realDifference + " + " + imaginaryDifference + "i.";					 			 
return answer;
}		
}

Your very welcome. I do have one question for you!

What form do you enter the numbers in?

I tried this form, but it didn't work:

a+bi

This article has been dead for over six months. Start a new discussion instead.