Implementing Complex Numbers in Java: A Guide to Class Design and Operations
This document outlines the implementation of a Java class to represent complex numbers, consisting of a real part and an imaginary part. It covers the creation of instance variables, constructors, and methods, including copying, equality checks, and printing. Additionally, it explains how to implement an addition method for complex numbers and includes a driver program to test the implementation. Example operations with complex numbers are provided, such as initializing, adding, and comparing complex numbers.
Implementing Complex Numbers in Java: A Guide to Class Design and Operations
E N D
Presentation Transcript
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