scalacommand-linecompilationscalac

Cannot compile and run multiple files in Scala


i am new to scala

I have two scala example files under same directory

one is:

package chap10

import Element.elem

abstract class Element {

  def contents: Array[String]

  def height: Int = contents.length

  def width: Int =
    if (height == 0) 0 else contents(0).length

  def above(that: Element): Element =
    elem(this.contents ++ that.contents)

  def beside(that: Element): Element =
    elem(
        for (
            (line1, line2) <- this.contents zip that.contents
        ) yield line1 + line2
    )

  private def widen(w: Int): Element =
    if (w <= width) this
    else {
      val left = elem(' ', (w - width) / 2, height)
      val right = elem(' ', w - width - left.width, height)
      left beside this beside right
    }

  private def heighten(h: Int): Element =
    if (h <= height) this
    else {
      val top = elem(' ', width, (h - height) / 2)
      val bottom = elem(' ', width, h - height - top.height)
      top above this above bottom
    }

  override def toString = contents mkString "\n"
}

object Element {

  private class ArrayElement(
    val contents: Array[String]
  ) extends Element

  private class LineElement(s: String) extends Element {
    val contents = Array(s)
    override def width = s.length
    override def height = 1
  }

  private class UniformElement(
    ch: Char,
    override val width: Int,
    override val height: Int
  ) extends Element {
    private val line = ch.toString * width
    def contents = Array.fill(height)(line)
  }

  def elem(contents: Array[String]): Element =
    new ArrayElement(contents)

  def elem(chr: Char, width: Int, height: Int): Element =
    new UniformElement(chr, width, height)

  def elem(line: String): Element =
    new LineElement(line)
}

and the other is

package chap10

import Element.elem

object Spiral {

  val space = elem(" ")
  val corner = elem("+")

  def spiral(nEdges: Int, direction: Int): Element = {
    if (nEdges == 1)
      elem("+")
    else {
      val sp = spiral(nEdges - 1, (direction + 3) % 4)
      def verticalBar = elem('|', 1, sp.height)
      def horizontalBar = elem('-', sp.width, 1)
      if (direction == 0)
        (corner beside horizontalBar) above (sp beside space)
      else if (direction == 1)
        (sp above space) beside (corner above verticalBar)
      else if (direction == 2)
        (space beside sp) above (horizontalBar beside corner)
      else
        (verticalBar above corner) beside (space above sp)
    }
  }

  def main(args: Array[String]) = {
    val nSides = args(0).toInt
    println(spiral(nSides, 0))
  }
}

if u want to refer to the source the link is here : https://github.com/vishallama/programming-in-scala-3rd-ed/tree/master/src/chap10

I tried to make it to byte code by typing:

scalac *.scala

it will create a chap10 folder under current directory

then I tried to use

scala chap10.main

it returns

No such file or class on classpath: chap10.main

or

scala -classpath chap10/Spiral.main

or

scala -classpath chap10.Spiral.main

all are no use to me.

I know it might be dumb question, but I have no much java or scala experience and I am still learning.

now I just want to get it to run

is there any step I missed?

Thank you all

Update: after I remove the line "package chap10" in Spiral.scala and run the command: scala Spiral.scala -classpath Element.scala it gave me this error

aaron$ scala Spiral.scala -classpath Element.scala
Spiral.scala:3: error: not found: object Element
import Element.elem
       ^
Spiral.scala:7: error: not found: value elem
  val space = elem(" ")
              ^
Spiral.scala:8: error: not found: value elem
  val corner = elem("+")
               ^
Spiral.scala:10: error: not found: type Element
  def spiral(nEdges: Int, direction: Int): Element = {
                                           ^
Spiral.scala:12: error: not found: value elem
      elem("+")
      ^
Spiral.scala:15: error: not found: value elem
      def verticalBar = elem('|', 1, sp.height)
                        ^
Spiral.scala:16: error: not found: value elem
      def horizontalBar = elem('-', sp.width, 1)
                          ^


Solution

  • Correct is scala chap10.Spiral

    $ ls -R
    .:
    Element.scala  Spiral.scala
    
    $ scalac *.scala 
    
    $ ls -R
    .:
    chap10  Element.scala  Spiral.scala    
    ./chap10:
    Element$ArrayElement.class  Element$LineElement.class     Spiral$.class
    Element.class               Element$UniformElement.class
    Element$.class              Spiral.class
    
    $ scala chap10.Spiral
    java.lang.ArrayIndexOutOfBoundsException: 0
        at chap10.Spiral$.main(Spiral.scala:29)
        ...
    
    $ scala chap10.Spiral 10
    +--
    |  
    

    All scala chap10.main, scala chap10/Spiral.main, scala chap10.Spiral.main, scala Spiral.scala are wrong.