# java coding 11

For this assignment, make sure you handle **invalid input, including type mismatch** – input validation is a requirement! Focus on good design, good style, and, most importantly, **reuse of code**.

__Question 1 [40pts]__**^{.}** A complex (“imaginary”) number has the form a + bi, where a is called the real part, b is called the imaginary part, and i = sqrt(-1). A complex number a + bi can be expressed as the ordered pair of real numbers (a, b).

Arithmetic operations on two complex numbers (a, b) and (c, d) are as follows:

**Addition:**(a, b) + (c, d) = (a + c, b + d)

**Subtraction:**(a, b) – (c, d) = (a – c, b – d)

**Multiplication:** (a, b) * (c, d) = (a * c – b * d, a * d + b * c)

**Division:**(a, b) / (c, d) = ((a * c + b * d)/(c^{2} + d^{2}), (b * c – a * d)/(c^{2} + d^{2}))

**Absolute value:** |(a, b)| = sqrt(a^{2} + b^{2})

Design and implement a **ComplexNumber **class that represents the real and imaginary parts as double values and provides at least the following methods:

â€¢**Constructors** for default and explicit initialization.

â€¢A method to **read** a complex number. Look at the sample output screen for the design required.

â€¢A method to **print** a complex number as (a, b). Have 2 decimals for both.

â€¢A method called **getReal** that returns the real part of a complex number.

â€¢A method called **getImaginary** that returns the imaginary part of a complex number.

â€¢Methods **equal, copy, getCopy, toString**.

â€¢Arithmetic methods to **add**, **subtract**, **multiply**, and **divide** two complex numbers.

â€¢A method called **cAbs** to implement the absolute value of a complex number.

To test your class write a client that has at least a function **menu()** with options for the methods implemented and an option to exit. Your program should loop until the user chooses to exit. In this loop you are required to use a **switch** statement for all possible cases (similar design as the one used for Problem#1 in Assignment#1).

**SAMPLE OUTPUT:**

Your options for Complex arithmetic are:

—————————————-

1)Add 2 complex numbers

2)Subtract 2 complex numbers

3)Multiply 2 complex numbers

4)Divide 2 complex numbers

5)Absolute value of a complex number

6)Compare 2 complex numbers

0)EXIT

Please enter your option: 2

Enter complex number (real imaginary): 3.4 5.6

Enter complex number (real imaginary): 1.23 2.56

First complex number is: (3.40, 5.60)

Second complex number is: (1.23, 2.56)

Result: (3.40, 5.60) – (1.23, 2.56) = (2.17, 3.04)Command number 1 completed.

Your options for Complex arithmetic are:

—————————————-

1)Add 2 complex numbers

2)Subtract 2 complex numbers

3)Multiply 2 complex numbers

4)Divide 2 complex numbers

5)Absolute value of a complex number

6)Compare 2 complex numbers

0)EXIT

Please enter your option: 4

Enter complex number (real imaginary): 11.2 22.1

Enter complex number (real imaginary): 1.45 3.56

First complex number is: (11.20, 22.10)

Second complex number is: (1.45, 3.56)

Result: (11.20, 22.10) / (1.45, 3.56) = (6.42, -0.53)Command number 2 completed.

Your options for Complex arithmetic are:

—————————————-

1)Add 2 complex numbers

2)Subtract 2 complex numbers

3)Multiply 2 complex numbers

4)Divide 2 complex numbers

5)Absolute value of a complex number

6)Compare 2 complex numbers

0)EXIT

Please enter your option: 2

Enter complex number (real imaginary): 1.78 4.5

Enter complex number (real imaginary): 3.56 8.9

First complex number is: (1.78, 4.50)

Second complex number is: (3.56, 8.90)

Result: (1.78, 4.50) – (3.56, 8.90) = (-1.78, -4.40)Command number 3 completed.

Your options for Complex arithmetic are:

—————————————-

1)Add 2 complex numbers

2)Subtract 2 complex numbers

3)Multiply 2 complex numbers

4)Divide 2 complex numbers

5)Absolute value of a complex number

6)Compare 2 complex numbers

0)EXIT

Please enter your option: 3

Enter complex number (real imaginary): 2.22 3.33

Enter complex number (real imaginary): 1.24 2.45

First complex number is: (2.22, 3.33)

Second complex number is: (1.24, 2.45)Result: (2.22, 3.33) * (1.24, 2.45) = (-5.41, 9.57)Command number 4 completed.

Your options for Complex arithmetic are:

—————————————-

1)Add 2 complex numbers

2)Subtract 2 complex numbers

3)Multiply 2 complex numbers

4)Divide 2 complex numbers

5)Absolute value of a complex number

6)Compare 2 complex numbers

0)EXIT

Please enter your option: 6

Enter complex number (real imaginary): 1.11 2.22

Enter complex number (real imaginary): 1.11 2.22

First complex number is: (1.11, 2.22)Second complex number is: (1.11, 2.22)The complex numbers are equal.

Command number 5 completed.

Your options for Complex arithmetic are:

—————————————-

1)Add 2 complex numbers

2)Subtract 2 complex numbers

3)Multiply 2 complex numbers

4)Divide 2 complex numbers

5)Absolute value of a complex number

6)Compare 2 complex numbers

0)EXIT

Please enter your option: 6

Enter complex number (real imaginary): 1.2 2.3

Enter complex number (real imaginary): 11.2 2.3

First complex number is: (1.20, 2.30)Second complex number is: (11.20, 2.30)The complex numbers are NOT equal.

Command number 6 completed.

Your options for Complex arithmetic are:

—————————————-

1)Add 2 complex numbers

2)Subtract 2 complex numbers

3)Multiply 2 complex numbers

4)Divide 2 complex numbers

5)Absolute value of a complex number

6)Compare 2 complex numbers

0)EXIT

Please enter your option: 5

Enter complex number (real imaginary): 11.1 22.2

The complex number is: (11.10, 22.20)Result: |(11.1, 22.2)| = 24.82Command number 7 completed.

Your options for Complex arithmetic are:

—————————————-

1)Add 2 complex numbers

2)Subtract 2 complex numbers

3)Multiply 2 complex numbers

4)Divide 2 complex numbers

5)Absolute value of a complex number

6)Compare 2 complex numbers

0) EXIT

Please enter your option: 0Testing completed.

__Question 2 [30pts].__** (Unsorted list: array implementation) **The unsorted list

ADT discussed in class (file â€œLecture_Array_Based_Lists.pdfâ€) should be extended by the addition of two new methods:

1.A method named **merge** that concatenates 2 unordered lists into a third. Assume that list_1 and list_2 don’t have any keys in common. The resulting list should be an unsorted list that contains all of the items from list_1 and list_2 (preserve the order).

2.A method named **split** that divides a list into 2 lists according to a key. If list_1 and list_2 are the resulting lists, list_1 should contain all the items of the original list whose keys are less than or equal to the key passed and list_2 should contain all the items of the original list whose keys are larger than the key passed.

Next, create a client to test your program. The client should work with 3 sorted lists named list_1, list_2 and result. Read the data for list_1 and list_2 from the files list1.txt and list2.txt (take input from user the names of the input files, handle the FileNotFoundException exception (try/catch) and consume unwanted input.) Merge list_1 and list_2 into result and split result according to a key (input from the user). Make sure you handle **all **possible errors.

**SAMPLE OUTPUT:**

Please input the name of the file to be opened for first list: list1.txtPlease input the name of the file to be opened for second list: list2.txtThe first list is:

13253467561020272514559The second list is:

732914877210020012722 151914515978The merged list is:

1325346756102027251455973291487721002001272215

1914515978

Enter key for split: 49

The first list after split is:

1325341020272514529 14221519The second list after split is:

67565973877210020012714515978**FOR input files:**

list1.txt: 13 c v b 25 34 x x 67 56 10 a a 20 27 2 a s 5 1 45 59list2.txt: 73 29 c c c 14 87 72 100 200 c c c 127 22 15 19 c v v v 145 159 78

__ __

__Question 3 [30pts].__** (Sorted list: array implementation) **Same problem for __this____ __sorted list ADT discussed in class.

**NOTE:** The merge method for the sorted list shouldnâ€™t look like the same method in the unsorted list. You should merge the two lists in ** one traversal** (do not make repeated calls to insert â€“ very inefficient!). You will not get credit for the same method in both classes.

**SAMPLE OUTPUT:**

Please input the name of the file to be opened for first list: list1.txtPlease input the name of the file to be opened for second list: list2.txtThe first list is:

2581114293343455155657075The second list is:

147910153549575967The merged list is:

124578910111415293335434549515557596567707 5

Enter key for split: 13

The first list after split is:

12457891011

The second list after split is:

14 152933354345495155575965677075**FOR input files:**list1.txt: 2 5 8 m m b 11 14 29 x 33 43 45 51 z z 55 65 70 b 75list2.txt: 1 ccc 4 bb 7 mm 9 10 15 35 x x x 49 57 59 67