zam
zam

Reputation: 461

Cannot compile example

I have this class:

 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 add(that:Rational): Rational = new Rational(numer * that.denom + that.numer * denom, denom * that.denom)

  override def toString = numer+"/"+denom;

  private def gcd(a: Int, b: Int): Int = if(b==0) a else gcd(b, a % b)
}

And this test class:

import Rational

object Test extends App {
  val x = new Rational(1/2)
  println("hello");
}

I'm trying to compile them using

scalac Test.scala Rational.scala

but I get the following error:

Test.scala:3: error: '.' expected but ';' found.
object Test extends App {
^
one error found

Can someone guide me to why its not compiling. This is a basic error

Upvotes: 0

Views: 42

Answers (2)

Raphael Roth
Raphael Roth

Reputation: 27373

import Rational is not valid syntax (because it's a class)

As you are in the default package, you don't need an import anyway

Upvotes: 2

Nagarjuna Pamu
Nagarjuna Pamu

Reputation: 14825

Remove import Rational.

Rational is neither a package nor an Scala object

Why do you have this when you do not have a package declared or Rational declared as object.

Upvotes: 1

Related Questions