0

I have the following program:

 class Rational(n: Int, d: Int) {
  require(d != 0)
  private val g = gcd(n.abs, d.abs)
  val numer = n / g
  val denom = d / g
  def this(n: Int) = this(n, 1)
  def this(s: String) = {
    val regex: Regex = "^([+-]?(\\d+|\\d*\\.?\\d+)|\\d*\\/?\\d+)$".r
    if (!regex.matches(s)) throw new NumberFormatException()
    val input: Array[String] = s.split("\\.|\\/")
    val num: Int = input(0).toInt
    if (input.length equals 1)
      this(num, 1) // problem here
    else
      this(num, input(1).toInt) // problem here
  }
}

I tried to create the constructor with some logic. However, I cannot due to

'Rational' does not take parameters

What's the problem?

3
  • 4
    What version of Scala are you using? It doesn't even compile the this(n: String), because it expects a this-invocation as the first expression. Also, I have the feeling that this-constructors are pretty much out of fashion, defining apply on object Rational would be much more idiomatic. Commented Nov 12, 2022 at 23:59
  • 1
    I cannot reproduce your problem. The code you posted cannot possibly lead to the error you posted. Please make sure to construct a minimal reproducible example which accurately represents your exact problem. Note that all three of those words are important: it should be an example only, you should not post your entire actual code, rather you should create a simplified example that demonstrates your problem. Also, it should be minimal, i.e. it should not contain anything that is not absolutely required to demonstrate the problem. (Most beginner problems can be demonstrated in less than 5 short simple lines … Commented Nov 13, 2022 at 8:26
  • 1
    … of code. I don't understand what your problem is, but at least the error message I am seeing with your code can be reproduced in 3 lines.) And it should be reproducible, which means that if I copy&paste and run the code, I should see the exact same problem you see. (Which is not the case with your code. I see a different error than the one you posted.) idownvotedbecau.se/toomuchcode idownvotedbecau.se/nomcve Commented Nov 13, 2022 at 8:27

1 Answer 1

5

Try to introduce a helper method

import scala.util.matching.Regex

def gcd(i: Int, i1: Int): Int = BigInt(i).gcd(BigInt(i1)).toInt

class Rational(n: Int, d: Int) {
  require(d != 0)
  private val g = gcd(n.abs, d.abs)
  val numer = n / g
  val denom = d / g

  def this(n: Int) = this(n, 1)

  def this(s: String) = {
    this(Rational.transformStr(s)._1, Rational.transformStr(s)._2)
  }
}

object Rational {
  // helper method
  def transformStr(s: String): (Int, Int) = {
    val regex: Regex = "^([+-]?(\\d+|\\d*\\.?\\d+)|\\d*\\/?\\d+)$".r
    if (!regex.matches(s)) throw new NumberFormatException()
    val input: Array[String] = s.split("\\.|\\/")
    val num: Int = input(0).toInt
    if (input.length equals 1)
      (num, 1)
    else
      (num, input(1).toInt)
  }
}

or better, factory methods (because constructors have many limitations)

class Rational(n: Int, d: Int) {
  require(d != 0)
  private val g = gcd(n.abs, d.abs)
  val numer = n / g
  val denom = d / g
}

object Rational {
  // factory methods
  def apply(n: Int) = new Rational(n, 1)

  def apply(s: String): Rational = {
    val regex: Regex = "^([+-]?(\\d+|\\d*\\.?\\d+)|\\d*\\/?\\d+)$".r
    if (!regex.matches(s)) throw new NumberFormatException()
    val input: Array[String] = s.split("\\.|\\/")
    val num: Int = input(0).toInt
    if (input.length equals 1)
      new Rational(num, 1)
    else
      new Rational(num, input(1).toInt)
  }
}

Executing code in overloaded constructor prior to calling this()

By the way, you can also use default values

class Rational(n: Int, d: Int = 1 /*default*/ ) {
  // ...
}

object Rational {
  def apply(s: String): Rational = ???
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.