• Post Reply Bookmark Topic Watch Topic
  • New Topic

constructor and toString problem  RSS feed

 
Lovelyn Garcia
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,
I need help for my homework. Been doing it for days but can't seems to work out.

Here is the question:
write class Complex
it has static boolean field called vecForm,
it has nonstatic double field called re and im.

in class Complex, write static with function called setVecForm: no return, have boolean type parameter b.
put Complex.vecForm inside parameter b.

in class Complex write constructor, it has 2 double parameter, to be used for setting re and im.
the formula is: (a+bI)+(c+dI)= ((a+c)+(b+d)I).
in class Complex write add(), it has 1 Complex type of parameter c2, it has return sum of this and c2.
in class Complex write public String toString(), to be used for assisting System.out.print:
when vecForm is true, String return includes this.re and this.im, with first character "( ", and last character ")", in the middle has ", ".
when vecForm is false, String return includes this.re and this.im, in the middle has "+" or "-". Ending with "I".

next, write class TestComplex, in it has main,  process as follows:
1. create complex number a, the content is 2+3I.
    create complex number b, the content is 4-5I.
2. create complex number c, set it to a, b through add()
3. if true, call parameter setVecForm in Complex, print a, b, c.
4. if false, call parameter setVecForm in Complex, print a, b, c.

the output is like this:
( 2.0 , 3.0 )
( 4.0 , -5.0 )
( 6.0 , -2.0 )
2.0 + 3.0 I
4.0 - 5.0 I
6.0 - 2.0 I

YOUR HELP IS MUCH APPRECIATED. THANK YOU SO MUCH
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a fairly detailed, step by step, description of what to produce.
Where have you got to, and where are you stuck?

Can you post the code here?
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Lovelyn Garcia
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:That's a fairly detailed, step by step, description of what to produce.
Where have you got to, and where are you stuck?

Can you post the code here?


Hi Dave, thanks for your replies.

I don't understand how to write this part:
in class Complex write constructor, it has 2 double parameter, to be used for setting re and im.
the formula is: (a+bI)+(c+dI)= ((a+c)+(b+d)I).
in class Complex write add(), it has 1 Complex type of parameter c2, it has return sum of this and c2.
in class Complex write public String toString(), to be used for assisting System.out.print:
when vecForm is true, String return includes this.re and this.im, with first character "( ", and last character ")", in the middle has ", ".
when vecForm is false, String return includes this.re and this.im, in the middle has "+" or "-". Ending with "I".

It always have some errors when I run it
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lovelyn Garcia wrote:. . . I don't understand how to write this part: . . .
That is not a part. That is six parts. You need to do one, complete it, test it, make sure it is working, and then consider the second part. Let's start with the constructor. Please show us how you have written that.
It always have some errors when I run it
Please give fuller details; we don't know what you are doing, so we can't help. But you are better off simply writing a constructor and when you have finished that you can consider the next part.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!