Java Programming and define the constructor so that the object will be initializ
ID: 646000 • Letter: J
Question
Java Programming
and define the constructor so that the object will be initialized to realPart + 0*i .
Also include a no-argument constructor that initializes an object to 0 (that is, to
0 + 0*i ). Define accessor and mutator methods as well as the methods equals
and toString . Define static methods for addition, subtraction, and multiplication
of objects of your class Complex . These methods should be static and should each
have two parameters of type Complex and return a value of type Complex . For
example, Complex.add(c1, c2) will return the result of adding the two complex
numbers (two objects of the class Complex ) c1 and c2 . Also write a test program
to test your class.
Hints : To add or subtract two complex numbers, you add or subtract the two
instance variables of type double . The product of two complex numbers is given
by the following formula:
(a + b *i)*(c + d *i) = (a *c ? b *d) + (a *d + b *c)*i
Explanation / Answer
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class ComplexNumbers { public static void main(String args[]) throws IOException { BufferedReader in = new BufferedReader (new InputStreamReader(System.in)); int a, b, c, d; System.out.println("Program to add two complex numbers.."); System.out.println(" "+"Enter first number a+bi"); System.out.print("Enter value for a: "); a = Integer.parseInt(in.readLine()); System.out.print("Enter value for b: "); b = Integer.parseInt(in.readLine()); System.out.println(" "+"Enter second number c+di"); System.out.print("Enter value for c: "); c = Integer.parseInt(in.readLine()); System.out.print("Enter value for d: "); d = Integer.parseInt(in.readLine()); System.out.println(" "+"Result of addtion"); System.out.print(a+c); System.out.print((b+d)>0?"+":""); System.out.print(b+d + "i "); System.out.println(" "+"Result of subtraction"); System.out.print(a-c); System.out.print((b+d)>0?"+":""); System.out.print(b-d + "i "); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.