Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 42 additions & 67 deletions src/demo/parallel/Complex.java
Original file line number Diff line number Diff line change
@@ -1,81 +1,23 @@
/*
* Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of Oracle nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package demo.parallel;


/**
* A complex number is a number that can be expressed in the form a + b * i, where
* a and b are real numbers and i is the imaginary unit, which satisfies the
* equation i ^ 2 = -1. a is the real part and b is the imaginary part of the
* complex number.
* <p><i>
* This source code is provided to illustrate the usage of a given feature
* or technique and has been deliberately simplified. Additional steps
* required for a production-quality application, such as security checks,
* input validation and proper error handling, might not be present in
* this sample code.</i>
* @author Alexander Kouznetsov, Tristan Yan
/*
* A complex number is a number that can be expressed in the form a + b * i.
*/
public class Complex {

private double re; // the real part
private double im; // the imaginary part
private double re; // real part
private double im; // imaginary part

/**
* create a new object with the given real and imaginary parts
*
* @param real a complex number real part
* @param imag a complex number imaginary part
*/
public Complex(double real, double imag) {
re = real;
im = imag;
}

/**
* Add operation.
* @param b summand
* @return this Complex object whose value is (this + b)
*/
public Complex plus(Complex b) {
re += b.re;
im += b.im;
return this;
}

/**
* Multiply operation.
* @param b multiplier
* @return this Complex object whose value is this * b
*/
public Complex times(Complex b) {
Complex a = this;
double real = a.re * b.re - a.im * b.im;
Expand All @@ -85,12 +27,45 @@ public Complex times(Complex b) {
return this;
}

/**
* Square of Complex object's length, we're using square of length to
* eliminate the computation of square root
* @return square of length
*/
public double lengthSQ() {
return re * re + im * im;
}

// 🔹 Новые методы

/* Вычитание */
public Complex minus(Complex b) {
this.re -= b.re;
this.im -= b.im;
return this;
}

/* Деление */
public Complex dividedBy(Complex b) {
double den = b.re * b.re + b.im * b.im;
if (den == 0.0) throw new ArithmeticException("Division by zero");
double newRe = (this.re * b.re + this.im * b.im) / den;
double newIm = (this.im * b.re - this.re * b.im) / den;
this.re = newRe;
this.im = newIm;
return this;
}

/* Сопряжение */
public Complex conjugate() {
this.im = -this.im;
return this;
}

/* Умножение на число */
public Complex scale(double k) {
this.re *= k;
this.im *= k;
return this;
}

/* Копия объекта (нужно, чтобы не портить исходный объект при операциях) */
public Complex copy() {
return new Complex(this.re, this.im);
}
}
72 changes: 72 additions & 0 deletions src/demo/parallel/ComplexTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package demo.parallel;

import org.junit.Test;
import static org.junit.Assert.*;

/**
* Unit-тесты для новых операций класса Complex.
*/
public class ComplexTest {

@Test
public void testMinus() {
Complex a = new Complex(3, 4);
Complex b = new Complex(1, -2);
a.minus(b); // (3+4i) - (1-2i) = (2+6i)
assertEquals(2.0, getRe(a), 1e-9);
assertEquals(6.0, getIm(a), 1e-9);
}

@Test
public void testDividedBy() {
Complex a = new Complex(3, 2);
Complex b = new Complex(1, -1);
a.dividedBy(b); // (3+2i) / (1-1i) = 0.5 + 2.5i
assertEquals(0.5, getRe(a), 1e-9);
assertEquals(2.5, getIm(a), 1e-9);
}

@Test(expected = ArithmeticException.class)
public void testDividedByZero() {
new Complex(1, 1).dividedBy(new Complex(0, 0));
}

@Test
public void testConjugate() {
Complex a = new Complex(3, -2);
a.conjugate(); // → (3+2i)
assertEquals(3.0, getRe(a), 1e-9);
assertEquals(2.0, getIm(a), 1e-9);
}

@Test
public void testScaleAndCopy() {
Complex a = new Complex(2, -3);
Complex b = a.copy().scale(2.5); // b = (5, -7.5), a не изменился
assertEquals(2.0, getRe(a), 1e-9);
assertEquals(-3.0, getIm(a), 1e-9);
assertEquals(5.0, getRe(b), 1e-9);
assertEquals(-7.5, getIm(b), 1e-9);
}

// 🔹 Вспомогательные методы (так как поля Complex приватные)
private static double getRe(Complex z) {
try {
var f = Complex.class.getDeclaredField("re");
f.setAccessible(true);
return f.getDouble(z);
} catch (Exception e) {
throw new RuntimeException(e);
}
}

private static double getIm(Complex z) {
try {
var f = Complex.class.getDeclaredField("im");
f.setAccessible(true);
return f.getDouble(z);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
Loading