Print a formatted object of a class
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.
iamthesgt
Junior Poster
119 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
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)
DavidKroukamp
Master Poster
735 posts since Dec 2011
Reputation Points: 279
Solved Threads: 181
Skill Endorsements: 4
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;
}
}
Philippe.Lahaie
Posting Whiz
360 posts since Oct 2007
Reputation Points: 103
Solved Threads: 54
Skill Endorsements: 4
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.
iamthesgt
Junior Poster
119 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 1 Year Ago by
DavidKroukamp
and
Philippe.Lahaie