-2

I'm trying to make a java library with a bunch of extra classes, and I was adding one for Imaginary numbers. Is there any way in java to make a custom class that is affected by mathematics operations for example

Imaginary(10) * Imaginary(50) = "500i"

syntagma
  • 23,346
  • 16
  • 78
  • 134
Sigma
  • 38
  • 6

2 Answers2

2

No, it is not possible to define custom arithmetic operators that overload default operators, but you can create methods like Imaginary.mutiply(Imaginary i).

syntagma
  • 23,346
  • 16
  • 78
  • 134
0

You cannot overload operators in Java. See this previous answer to a similar question: https://stackoverflow.com/a/5883909/1701316

Your class will need to implement its operations as methods. If you'd like, since any character is allowed in a method name, you can name them with the typical operators, but they'll still need to be called with dot-notation: Imaginary(10).*(Imaginary(50))

Miles Grimes
  • 432
  • 2
  • 16