COSC 237

Assignment # 2

DUE DATE:  3/24/2020 (Tuesday)

For this assignment, you will work in teams of at most two students, but both teammates should contribute seriously (this will be very helpful for the forthcoming tests). You should submit only one copy of the homework with both your names on it. In principle you should not discuss the homework with anyone, except your partner and your instructor. If you do discuss it with someone else you should say this in the preamble of the homework. You should only submit work that is completely your own and you should be able to explain all of your homework to me. Please staple all pages together (a must!). You should also be prepared to make a demo of your programs if you are asked so.

You have 2 programming problems involving Java user-created classes. For each, turn in the printout of the source files, for the user-created class and the client (Again: from DrJava (Menu bar) File à Print). Also, turn in a printout of the output screen after running the client program (Again, NO PrintScreen! Copy/Paste final output from Console view, NOT Interactions view, in a Word document à Print. Use a Courier New font to keep the formatting.) Your output should include complete testing for all possible cases. No output means you will receive no more than half credit and usually much less. Also, make sure you handle invalid input, including type mismatch - input validation is a requirement! Focus on good design and good style. Start early and remember the rules about late assignments.


1. 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)/(c2 + d2), (b * c - a * d)/(c2 + d2))
Absolute value: |(a, b)| = sqrt(a2 + b2)

 

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

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.82
   Command 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: 0
Testing completed.


2. Design and implement a Java class for matrix arithmetic using square matrices (same number of rows and columns). Your solution will use a class called Matrix. When designing the class method members, use as a guide the program you wrote for the previous assignment (you should have all the algorithms figured out by now). Provide at least the following methods:

To make your job easier, I give you here the layout of the Matrix class (Matrix.java):

 

//ASSIGNMENT #2: MATRIX ARITHMETIC
//Class Matrix. File: Matrix.java
import java.util.Scanner;
import java.util.Random;

public class Matrix {
    public final int MAX = 20;

    private int size;
    private int[][] table = new int[MAX][MAX];

    public Matrix() {... }
    public Matrix(int s) {... }
    public int getSize() {... }
    public int getElement(int r, int c) {... }
    public void setElement(int r, int c, int value) {... }
    public void init(int low, int up) {... }
    public void print() {... }
    public Matrix add(Matrix a){... }
    public Matrix subtract(Matrix a) {... }
    public Matrix multiply(Matrix a) {... }
    public Matrix multiplyConst(int whatConst) {... }
    public Matrix transpose() {... }
    public int trace() {... }
    public boolean equals(Matrix a){... }
    public void copy(Matrix a) {... }
    public Matrix getCopy() {... }
}//close class Matrix

Write the code for class Matrix. Next, 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 in the previous program). Look at the sample output to figure out how the client should work. Again, to make your job easier, I get you started with the client program:

 
//ASSIGNMENT #2: MATRIX ARITHMETIC
//Client for class Matrix. File: MatrixClient.java
import java.util.Scanner;
import java.util.Random;

public class MatrixClient {
    public static final int MAX = 20;
    public static final int LOW = 1;
    public static final int UP = 10;

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int choice; //operation to be executed from menu
        int numCommands = 0; //display counter
        int size; //for subarray processing
        int value; //multiply matrix by this constant
        int tr; //return from trace()
        //MISSING  CODE: input size. Valid type? Valid range?

        Matrix first = new Matrix(size);
        Matrix second = new Matrix(size);

        Matrix result = new Matrix(size);
        choice = menu(); //priming read;
        while (choice != 0)  {

        ---
   }
   ---
}
---

Turn in the programs Matrix.java,and MatrixClient.java. Also, attach a copy of the output screen, including testing for all possible cases.

SAMPLE OUTPUT:

Enter the size of the square matrix: 25
INPUT ERROR!!! Invalid size. Positive and <= 20.
Enter the size of the square matrix: 5

Your options are:
-----------------
 1) Add 2 matrices
 2) Subtract 2 matrices
 3) Multiply 2 matrices
 4) Multiply matrix by a constant
 5) Transpose matrix
 6) Matrix trace
 7) Make a copy
 8) Test for equality
 0) EXIT

Please enter your option: 7
The original matrix is:
    6    7    2    9    1
   10    5    2    4   10
   10    8    6    4    3
    3    2    2    3    9
    2   10    8    8    4
The copy of this matrix is:
    6    7    2    9    1
   10    5    2    4   10
   10    8    6    4    3
    3    2    2    3    9
    2   10    8    8    4
Testing for equality. Should be equal!!
The matrices are equal!!
   Command number 1 completed.

Your options are:
-----------------
 1) Add 2 matrices
 2) Subtract 2 matrices
 3) Multiply 2 matrices
 4) Multiply matrix by a constant
 5) Transpose matrix
 6) Matrix trace
 7) Make a copy
 8) Test for equality
 0) EXIT
Please enter your option: 8
First matrix is:
    1    2   10    9   10
    7   10    5    5    9
   10    3    4    1    6
   10    6    1    9    8
    6    6    3   10    1
Second matrix is:
   10    9    4    2    3
    7    6    8    5   10
    8    5    9    7    3
    9    3    5    3    9
    6    3    8    5    7
The matrices are NOT equal!!
   Command number 2 completed.

Your options are:
-----------------
 1) Add 2 matrices
 2) Subtract 2 matrices
 3) Multiply 2 matrices
 4) Multiply matrix by a constant
 5) Transpose matrix
 6) Matrix trace
 7) Make a copy
 8) Test for equality
 0) EXIT
Please enter your option: 1
First matrix is:
    9    4   10    1   10
    8    2    9    3    1
    5    9    8    7    9
   10    7    3    4    1
    7    5    5    2    7
Second matrix is:
   10    9    4    1    1
    9    3    8    3    1
    3    4    7   10    5
    8    9    8    9    2
    1    3    8    2    7
The resulting matrix is:
   19   13   14    2   11
   17    5   17    6    2
    8   13   15   17   14
   18   16   11   13    3
    8    8   13    4   14
   Command number 3 completed.

Your options are:
-----------------
 1) Add 2 matrices
 2) Subtract 2 matrices
 3) Multiply 2 matrices
 4) Multiply matrix by a constant
 5) Transpose matrix
 6) Matrix trace
 7) Make a copy
 8) Test for equality
 0) EXIT

Please enter your option: 3
First matrix is:
    3   10    1    8    8
    6    9    4   10    7
    7    7    2    5    5
    8    6   10   10    6
    7    3    2   10    6
Second matrix is:
    9    4   10    1    1
    2    7    3    6    7
    2    9    9   10    2
   10    5    8    1    7
    4    9   10    2    4
The resulting matrix is:
  161  203  213   97  163
  208  236  273  124  175
  151  165  199   84  115
  228  268  328  166  164
  197  171  237   67  126
   Command number 4 completed.

Your options are:
-----------------
 1) Add 2 matrices
 2) Subtract 2 matrices
 3) Multiply 2 matrices
 4) Multiply matrix by a constant
 5) Transpose matrix
 6) Matrix trace
 7) Make a copy
 8) Test for equality
 0) EXIT
Please enter your option: 2
First matrix is:
    2    3    1    6    3
   10    6    3    9    2
    4    1    8    5   10
    1    7    3    6    4
    5    6    4    9    5
Second matrix is:
    4    9    7    7    9
    8    1    2    5    2
    4    1   10    7    8
    3    9    7    6    7
    4    9    7    2    9
The resulting matrix is:
   -2   -6   -6   -1   -6
    2    5    1    4    0
    0    0   -2   -2    2
   -2   -2   -4    0   -3
    1   -3   -3    7   -4
   Command number 5 completed.

Your options are:
-----------------
 1) Add 2 matrices
 2) Subtract 2 matrices
 3) Multiply 2 matrices
 4) Multiply matrix by a constant
 5) Transpose matrix
 6) Matrix trace
 7) Make a copy
 8) Test for equality
 0) EXIT
Please enter your option: 4
Enter the multiplication constant: 10
The original matrix is:
    8    8   10    4    9
    1    3    5    9    6
    9   10   10    4    5
    7    1    8    3    3
    9    1    9    9    3
The resulting matrix is:
   80   80  100   40   90
   10   30   50   90   60
   90  100  100   40   50
   70   10   80   30   30
   90   10   90   90   30
   Command number 6 completed.

Your options are:
-----------------
 1) Add 2 matrices
 2) Subtract 2 matrices
 3) Multiply 2 matrices
 4) Multiply matrix by a constant
 5) Transpose matrix
 6) Matrix trace
 7) Make a copy
 8) Test for equality
 0) EXIT
Please enter your option: 6
The original matrix is:
    8    6    9    3    1
    5    3   10    1    7
    7    1    1    8    2
   10    8    4   10    2
    5    4    2    7    9
The trace for this matrix is: 31
   Command number 7 completed.

Your options are:
-----------------
 1) Add 2 matrices
 2) Subtract 2 matrices
 3) Multiply 2 matrices
 4) Multiply matrix by a constant
 5) Transpose matrix
 6) Matrix trace
 7) Make a copy
 8) Test for equality
 0) EXIT
Please enter your option: 5
The original matrix is:
    7    5    2    1    8
    1    3    1    5   10
    6    6    3    8    5
    8    5    3   10    5
   10    7    3   10   10
The resulting matrix is:
    7    1    6    8   10
    5    3    6    5    7
    2    1    3    3    3
    1    5    8   10   10
    8   10    5    5   10
   Command number 8 completed.

Your options are:
-----------------
 1) Add 2 matrices
 2) Subtract 2 matrices
 3) Multiply 2 matrices
 4) Multiply matrix by a constant
 5) Transpose matrix
 6) Matrix trace
 7) Make a copy
 8) Test for equality
 0) EXIT
Please enter your option: 0
Testing completed.