Image for post
Image for post
The fugue theme shown in a stretto here is almost just a scale.
    /**
* Addition operation.
* @param summand The number to add.
* @return result The result.
* @throws AlgebraicDegreeOverflowException When adding numbers from different domains.
* @throws ArithmeticException When either the real part or the imaginary part overflows the int primitive.
*/
public ImaginaryQuadraticInteger plus(ImaginaryQuadraticInteger summand) {
// Omitting lines in which decision to throw exceptions is made
// Omitting lines about the so-called "half-integers"
// Also omitting the lines with the actual adding
return new ImaginaryQuadraticInteger((int) sumRealPart, (int) sumImagPart, this.imagQuadRing, sumDenom);
}
  /** Adds an Int to this ImagQuadInt (overloaded operator for ImaginaryQuadraticInteger.plus()).
*
* @param summand The Int to add, a purely real integer.
* @return The result of the addition.
*/
def +(summand: Int): ImagQuadInt = {
val temp = this.plus(summand)
new ImagQuadInt(temp.realPartMult, temp.imagPartMult, temp.imagQuadRing, temp.denominator)
}
package imaginaryquadraticintegerimport org.junit.Test
import org.junit.Assert._
class ImagQuadIntTest {  val ring = new ImagQuadRing(-2)  val operand1 = new ImagQuadInt(3, 5, ring)
val operand2 = new ImagQuadInt(4, 2, ring)
/** Test of + method of class ImagQuadInt.
*/
@Test def testPlus(): Unit = {
println("operator+")
val expResult = new ImagQuadInt(7, 7, ring)
val result = operand1 + operand2
assertEquals(expResult, result)
}
  /** Verifying numbers from different rings trigger AlgebraicDegreeOverflowException 
*/
@Test(expected = classOf[AlgebraicDegreeOverflowException]) def testTimesDiffRings(): Unit = {
println("Multiplication by number from another ring")
val numFromDiffRing = new ImagQuadInt(1, 1, new ImagQuadRing(-5))
operand1 * numFromDiffRing
}
  @Test def testTimesDiffRings(): Unit = {
println("Multiplication by number from another ring")
val numFromDiffRing = new ImagQuadInt(1, 1, new ImagQuadRing(-5))
evaluating {
operand1 * numFromDiffRing
} should produce [AlgebraicDegreeOverflowException]
}
import org.scalatest.junit.JUnitSuite
import org.scalatest.junit.ShouldMatchersForJUnit
  /** Verify that division by zero causes an appropriate exception.
*/
@Test def testDivideByZero(): Unit = {
print("Division by zero")
val zero = new ImagQuadInt(0, 0, ring)
try {
operand2 / zero
} catch {
case nde: NotDivisibleException => println(" caused NotDivisibleException \"" + nde.getMessage + "\"")
case iae: IllegalArgumentException => println(" caused IllegalArgumentException \"" + iae.getMessage + "\"")
case ae: ArithmeticException => println(" caused ArithmeticException \"" + ae.getMessage + "\"")
case e: Exception => println(" caused " + e.getClass + " \"" + e.getMessage + "\"")
fail("Division by zero should not have caused " + e.getClass)
}
}

is a composer and photographer from Detroit, Michigan. He has been working on a Java program to display certain mathematical diagrams.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store