50 likes | 187 Views
Lab Exercise – Complex Numbers. Venkatesh Ramamoorthy (Venky). Problem statement. Write a Java class complex that implements complex numbers A complex number consists of a real part and an imaginary part. What are your instance variables?
E N D
Lab Exercise – Complex Numbers Venkatesh Ramamoorthy (Venky)
Problem statement • Write a Java class complex that implements complex numbers • A complex number consists of a real part and an imaginary part. What are your instance variables? • Implement the three constructors. What would they be? • Implement the Copy, Equals and Print methods for this class. Two complex numbers (a + ib) and (c + id) are equal if and only if a and b are equal, and also, c and d are equal. • Implement a method add that adds one complex number into another, using the following rule for addition: (a + ib) + (c + id) = (a + c) + i (b + d)
Problem statement (cont’d) • Using a driver program – the main program – test your implementation. • Initialize two complex numbers c1 with 10+20i, and c2 with 30+40i. Add c1 into c2. What will c2 now contain? • Copy c1 into a third variable c3 and add a fourth complex number 15i (=0+15i) to it. What will c3 now contain? • Check if c2 is equal to 40+60i