fork download
  1. class Rational(n: Int, d: Int) {
  2. require(d != 0)
  3.  
  4. private val g = gcd(n.abs, d.abs)
  5. val numer = n / g
  6. val denom = d / g
  7.  
  8. def this(n: Int) = this(n, 1)
  9.  
  10. def + (that: Rational): Rational =
  11. new Rational(
  12. numer * that.denom + that.numer * denom,
  13. denom * that.denom
  14. )
  15.  
  16. def + (i: Int): Rational =
  17. new Rational(numer + i * denom, denom)
  18.  
  19. def - (that: Rational): Rational =
  20. new Rational(
  21. numer * that.denom that.
  22. numer * denom,
  23. denom * that.denom
  24. )
  25.  
  26. def - (i: Int): Rational =
  27. new Rational(numer i * denom, denom)
  28.  
  29. def * (that: Rational): Rational =
  30. new Rational(numer * that.numer, denom * that.denom)
  31.  
  32. def * (i: Int): Rational =
  33. new Rational(numer * i, denom)
  34.  
  35. def / (that: Rational): Rational =
  36. new Rational(numer * that.denom, denom * that.numer)
  37.  
  38. def / (i: Int): Rational =
  39. new Rational(numer, denom * i)
  40.  
  41. override def toString = numer +"/"+ denom
  42.  
  43. private def gcd(a: Int, b: Int): Int =
  44. if (b == 0) a else gcd(b, a % b)
  45. }
  46.  
  47. object Main extends App {
  48. val x = new Rational(2, 3)
  49. val y = new Rational(7)
  50.  
  51. println(x * y)
  52. println(x * 2)
  53. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.scala:21: error: ')' expected but '.' found.
      numer * that.denom that.
                             ^
one error found
Usage: scalac <options> <source files>
where possible standard options include:
  -Dproperty=value                Pass -Dproperty=value directly to the runtime system.
  -J<flag>                        Pass <flag> directly to the runtime system.
  -P:<plugin>:<opt>               Pass an option to a plugin
  -X                              Print a synopsis of advanced options.
  -bootclasspath <path>           Override location of bootstrap class files.
  -classpath <path>               Specify where to find user class files.
  -d <directory|jar>              destination for generated classfiles.
  -dependencyfile <file>          Set dependency tracking file.
  -deprecation                    Emit warning and location for usages of deprecated APIs.
  -encoding <encoding>            Specify character encoding used by source files.
  -explaintypes                   Explain type errors in more detail.
  -extdirs <path>                 Override location of installed extensions.
  -feature                        Emit warning and location for usages of features that should be imported explicitly.
  -g:<level>                      Set level of generated debugging info. (none,source,line,vars,notailcalls) default:vars
  -help                           Print a synopsis of standard options
  -javabootclasspath <path>       Override java boot classpath.
  -javaextdirs <path>             Override java extdirs classpath.
  -language:<_,feature,-feature>  Enable or disable language features: `_' for all, `-language:help' to list
  -no-specialization              Ignore @specialize annotations.
  -nobootcp                       Do not use the boot classpath for the scala jars.
  -nowarn                         Generate no warnings.
  -optimise                       Generates faster bytecode by applying optimisations to the program
  -print                          Print program with Scala-specific features removed.
  -sourcepath <path>              Specify location(s) of source files.
  -target:<target>                Target platform for object files. All JVM 1.5 targets are deprecated. (jvm-1.5,jvm-1.6,jvm-1.7,jvm-1.8) default:jvm-1.6
  -toolcp <path>                  Add to the runner classpath.
  -unchecked                      Enable additional warnings where generated code depends on assumptions.
  -uniqid                         Uniquely tag all identifiers in debugging output.
  -usejavacp                      Utilize the java.class.path in classpath resolution.
  -usemanifestcp                  Utilize the manifest in classpath resolution.
  -verbose                        Output messages about what the compiler is doing.
  -version                        Print product version and exit.
  @<file>                         A text file containing compiler arguments (options and source files)

spoj: The program compiled successfully, but Main.class was not found.
      Class Main should contain method: def main(args: Array[String]).
stdout
Standard output is empty