---
# System prepended metadata

title: Computer Programming II Lab 2 v2.0
tags: [Computer Engineering, IUG, Lab, Computer Programming II]

---

---
title: Computer Programming II Lab 2 v2.0
---
<h1 style='border: none'><center>Computer Programming II Lab 2</center></h1>
<h2 style='border: none'><center>Introduction to Object-Oriented Programming</br>Classes and Objects</center></h2>
<h5><center>The Islamic University of Gaza<br>Engineering Faculty<br>Department of Computer Engineering</center></h5>
<h6>Authors: Usama R. Al Zayan<span style="float:right">2023/02/08</span></h6>
<h6>Parts of this Lab were adapted from work done by Mohammed Nafiz ALMadhoun</h6>
---

## Lab 2: Time and Plan

| Tasks    | Timing   | 
| -------- | -------- |
| Quiz 1   | 10 min   |
| Task 1   | 10 min   |
| Task 2   | 40 min   |
| Task 3   | 20 min   |

## Lab Task

Create class called MyComplex, which models a complex number with real and imaginary parts, is designed as shown in the class diagram.

![](https://i.imgur.com/WrBMde1.png)



### Task 1 : Write the <code>MyComplex</code> class (5 marks)


<p>It contains:</p>
<ul>
<li>Two instance variable named <code>real</code> (<code>double</code>) and <code>imag</code> (<code>double</code>) which stores the real and imaginary parts of the complex number, respectively.</li>
<li>A constructor that creates a <code>MyComplex</code> instance with the given real and imaginary values.</li>
<li>A default constructor that create a MyComplex  at <code>0.0 + 0.0i</code>.</li>
<li>Getters and setters for instance variables <code>real</code> and <code>imag</code>.</li>
<li>A method <code>setValue()</code> to set the value of the complex number.</li>
<li>A <code>toString()</code> that returns "<code>(x + yi)</code>" where <code>x</code> and <code>y</code> are the real and imaginary parts, respectively.</li>
</ul>

### Task 2: Complete the <code>MyComplex</code> class (20 marks)

<ul>   
<li>Methods <code>isReal()</code> and <code>isImaginary()</code> that returns <code>true</code> if this complex number is real or imaginary, respectively. <br>
  Hints:
    <pre class="color-example"><span class="color-comment"></span>return (imag == 0);</pre></li>

<li>A method <code>equals(double real, double imag)</code> that returns <code>true</code> if <code>this</code> complex number is equal to the given complex number (real, imag).<br>
Hints:
<pre class="color-example">return (this.real == real &amp;&amp; this.imag == imag);</pre>
</li>
<li>An overloaded <code>equals(MyComplex another)</code> that returns <code>true</code> if <em>this</em> complex number is equal to the given <code>MyComplex</code> instance <code>another</code>.<br>
Hints:
<pre class="color-example">return (this.real == another.real &amp;&amp; this.imag == another.imag);</pre>
</li>
<li>A method <code>magnitude()</code> that returns the magnitude of this complex number.
<pre class="color-syntax">magnitude(x+yi) = Math.sqrt(x*x + y*y)</pre></li>
<li>Methods <code>addInto(MyComplex right)</code> that adds and subtract the given <code>MyComplex</code> instance (called <code>right</code>) into <code>this</code> instance and returns <code>this</code> instance.
  <pre class="color-syntax">(a + bi) + (c + di) = (a+c) + (b+d)i
</pre>
Hints:
<pre class="color-example">return this;  <span class="color-comment">// return "this" instance</span></pre></li>

<li>Methods <code>addNew(MyComplex right)</code> that adds <code>this</code> instance with the given <code>MyComplex</code> instance called <code>right</code>, and returns a new <code>MyComplex</code> instance containing the result.<br>
Hint:
<pre class="color-example"><span class="color-comment">// construct a new instance and return the constructed instance</span>
return new MyComplex(..., ...);</pre></li>
    

<li>Methods <code>argument()</code> that returns the argument of this complex number in radians (<code>double</code>).
  <pre class="color-syntax">arg(x+yi) = Math.atan2(y, x) (in radians)</pre>
Note: The <code>Math</code> library has two arc-tangent methods, <code>Math.atan(double)</code> and <code>Math.atan2(double, double)</code>.  We commonly use the <code>Math.atan2(y, x)</code> instead of <code>Math.atan(y/x)</code> to avoid division by zero.  Read the documentation of <code>Math</code> class in package <code>java.lang</code>.</li>
<li>The method <code>addInto()</code> is renamed <code>add()</code>. Also added <code>subtract()</code> and <code>subtractNew()</code>.</li>
<li>Methods <code>multiply(MyComplex right)</code> and <code>divide(MyComplex right)</code> that multiplies and divides <code>this</code> instance with the given <code>MyComplex</code> instance <code>right</code>, and keeps the result in <code>this</code> instance, and returns this instance.
  <pre class="color-syntax">(a + bi) * (c + di) = (ac - bd) + (ad + bc)i
(a + bi) / (c + di) = [(a + bi) * (c – di)] / (c*c + d*d)</pre>
</li>
<li>A method <code>conjugate()</code> that operates on <code>this</code> instance and returns <code>this</code> instance  containing the <em>complex conjugate</em>.
<pre class="color-syntax">conjugate(x+yi) = x - yi</pre></li>
</ul>

<p>Take note that there are a few flaws in the design of this class, which was introduced solely for teaching purpose:</p>
<ul>
<li>Comparing <code>double</code>s in <code>equal()</code> using "<code>==</code>" may produce unexpected outcome.  For example, <code>(2.2+4.4)==6.6</code> returns <code>false</code>. It is common to define a small threshold called <code>EPSILON</code> (set to about <code>10^-8</code>) for comparing floating point numbers.</li>
<li>The method <code>addNew()</code>, <code>subtractNew()</code> produce new instances, whereas <code>add()</code>, <code>subtract()</code>, <code>multiply()</code>, <code>divide()</code> and <code>conjugate()</code> modify <code>this</code> instance. There is inconsistency in the design (introduced for teaching purpose).</li>
</ul>

<p>Also take note that methods such as <code>add()</code> returns an instance of <code>MyComplex</code>.  Hence, you can place the result inside a <code>System.out.println()</code> (which implicitly invoke the <code>toString()</code>).  You can also chain the operations, e.g., <code>c1.add(c2).add(c3)</code> (same as <code>(c1.add(c2)).add(c3))</code>, or <code>c1.add(c2).subtract(c3)</code>.</p>
    
    

</ul>

### Task 3 : Test <code>MyComplex</code> class (5 marks)

Write an application called <code>MyComplexApp</code> that uses the <code>MyComplex</code> class. The application shall prompt the user for two complex numbers, print their values, check for real, imaginary and equality, and carry out all the arithmetic operations.
    

<pre class="output">Enter complex number 1 (real and imaginary part): <strong>1.1 2.2</strong>
Enter complex number 2 (real and imaginary part): <strong>3.3 4.4</strong>
  
Number 1 is: (1.1 + 2.2i)
(1.1 + 2.2i) is NOT a pure real number
(1.1 + 2.2i) is NOT a pure imaginary number
  
Number 2 is: (3.3 + 4.4i)
(3.3 + 4.4i) is NOT a pure real number
(3.3 + 4.4i) is NOT a pure imaginary number
   
(1.1 + 2.2i) is NOT equal to (3.3 + 4.4i)
(1.1 + 2.2i) + (3.3 + 4.4i) = (4.4 + 6.6000000000000005i)
(1.1 + 2.2i) - (3.3 + 4.4i) = (-2.1999999999999997 + -2.2i)
</pre></li>
</ol>


###### tags: `Computer Programming II` `Lab` `IUG` `Computer Engineering`
<center>End Of Lab 2</center>