For a (homework) project, I have a class ComplexNumber that needs to be printed. I have a number of operations that can be done on objects of the class (addition, subtraction, multiplication, division). Each operation returns a new ComplexNumber to be printed. How exactly do I print this? Do I have to write a special constructor or operator to print this in the format I want it? Here is an example of one method.

//code to select the addition case
case 'a': System.out.println("Please enter the two complex numbers to be other in the form 7i+5j, separated by a whitespace");
					base = getComplex();
					other = getComplex();
					System.out.println("The addition of numbers " + base + " and " + other + " is " + addition(base, other));
					break;
//the addition method
public static ComplexNumber addition(ComplexNumber cmplx1, ComplexNumber cmplx2) {
		return new ComplexNumber(cmplx1.i + cmplx2.i, cmplx1.j + cmplx2.j);
	}

In other words, how do I print a 'ComplexNumber'? Thanks.

For a (homework) project, I have a class ComplexNumber that needs to be printed. I have a number of operations that can be done on objects of the class (addition, subtraction, multiplication, division). Each operation returns a new ComplexNumber to be printed. How exactly do I print this? Do I have to write a special constructor or operator to print this in the format I want it? Here is an example of one method.

//code to select the addition case
case 'a': System.out.println("Please enter the two complex numbers to be other in the form 7i+5j, separated by a whitespace");
					base = getComplex();
					other = getComplex();
					System.out.println("The addition of numbers " + base + " and " + other + " is " + addition(base, other));
					break;
//the addition method
public static ComplexNumber addition(ComplexNumber cmplx1, ComplexNumber cmplx2) {
		return new ComplexNumber(cmplx1.i + cmplx2.i, cmplx1.j + cmplx2.j);
	}

In other words, how do I print a 'ComplexNumber'? Thanks.

This should be of help:http://en.literateprograms.org/Complex_numbers_(Java)

sounds like you just need to overwrite the toString() method for your complexNumber class.

i dont know what variables you have in a complexNumber class but lets pretend its just 2 ints called iPartI and iPartJ :

public class complexNumber{
     private int iPartI;
     private int iPartJ;

     public complexNumber(int i , int j){
        iPartI = i;
        iPartJ = j;
     }

     public String toString(){
     //either return directly a string or build one and return it
     //lets build it first to clearly express what we are doing :

      String sBuiltString;

      //time to format the output look of your complex number
      sBuiltString = iPartI + "i + " + iPartJ + "j";

      //now return the string we built
      return sBuiltString;
     }
}
commented: Exactly what I needed. Thanks. +3

This was exactly what I needed. Thanks for the help. I didn't know if I needed to overload and operator or what, but this answered my questions.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.