1,105,371 Community Members

Print a formatted object of a class

Member Avatar
iamthesgt
Junior Poster
119 posts since Nov 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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.

Member Avatar
DavidKroukamp
Master Poster
737 posts since Dec 2011
Reputation Points: 105 [?]
Q&As Helped to Solve: 180 [?]
Skill Endorsements: 4 [?]
Team Colleague
Featured
 
1
 

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)

Member Avatar
Philippe.Lahaie
Posting Whiz
376 posts since Oct 2007
Reputation Points: 42 [?]
Q&As Helped to Solve: 60 [?]
Skill Endorsements: 9 [?]
 
1
 

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;
     }
}
Member Avatar
iamthesgt
Junior Poster
119 posts since Nov 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Question Answered as of 2 Years Ago by Philippe.Lahaie and DavidKroukamp
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: