Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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

Java Programming Define a class for complex numbers. A complex number is a number of the form a + b*i For our purposes, a and b are numbers of type double, and i is a number that represents the quantity sqrt{-1} . Represent a complex number as two values of type double . Name the instance variables real and imaginary . (The instance variable for the number that is multiplied by i is the one called imaginary .) Call the class Complex . Include a constructor with two parameters of type double that can be used to set the instance variables of an object to any values. Also include a constructor that has only a single parameter of type double ; call this parameter realPart 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 "); } }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote