Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Scala中构造矩形_Scala - Fatal编程技术网

在Scala中构造矩形

在Scala中构造矩形,scala,Scala,我是Scala新手,正在通过下面的练习来理解case类和方法。任何输入或帮助都将不胜感激,因为我仍在习惯语法 到目前为止,我尝试了以下步骤: 创建一个名为Point的case类,该类由两个整数x和y组成 b创建leftOf和above方法,以确定给定点是否位于另一点的左侧/上方 #part a case class Point(x: Integer, y: Integer) { #part b def leftOf(point: Point): Boolean = { if(x < poi

我是Scala新手,正在通过下面的练习来理解case类和方法。任何输入或帮助都将不胜感激,因为我仍在习惯语法

到目前为止,我尝试了以下步骤:

创建一个名为Point的case类,该类由两个整数x和y组成

b创建leftOf和above方法,以确定给定点是否位于另一点的左侧/上方

#part a
case class Point(x: Integer, y: Integer) {
#part b
def leftOf(point: Point): Boolean = {
if(x < point.x) return true}
def above(point: Point): Boolean = {
if(y > point.y) return true}
}
c创建一个由两个点(左上角和右下角)构成的案例类矩形

d创建另一个名为contains的方法,该方法接受点p,如果该点位于矩形内,则返回true

#part c
case class Rectangle(topLeft: Point, bottomRight: Point) {
val topLeft: Point = leftOf.x && above.y
val bottomRight: Point = !(leftOf.x && above.y)
val bottomLeft: Point = topLeft.x && bottomRight.y
val topRight: Point = bottomRight.x && topLeft.y

#part d
def contains(p: Point): Boolean = {
if (p.topLeft.x < x < p.bottomRight.x) && (p.topLeft.y < y < p.bottomRight.y) return true}

#part e
def overlaps(r1: Rectangle, r2: Rectangle): Boolean = {
if (r2.topLeft(r1) || r2.topRight(r1) || r2.bottomLeft(r1) || r2.bottomRight(r1)) return true}

e创建另一个名为overlaps的方法,该方法接受一个矩形r,如果它与另一个矩形重叠,则返回true

#part c
case class Rectangle(topLeft: Point, bottomRight: Point) {
val topLeft: Point = leftOf.x && above.y
val bottomRight: Point = !(leftOf.x && above.y)
val bottomLeft: Point = topLeft.x && bottomRight.y
val topRight: Point = bottomRight.x && topLeft.y

#part d
def contains(p: Point): Boolean = {
if (p.topLeft.x < x < p.bottomRight.x) && (p.topLeft.y < y < p.bottomRight.y) return true}

#part e
def overlaps(r1: Rectangle, r2: Rectangle): Boolean = {
if (r2.topLeft(r1) || r2.topRight(r1) || r2.bottomLeft(r1) || r2.bottomRight(r1)) return true}


下面是前四个需求的实现,试图展示如何编写惯用的Scala代码。 最后一个要求留给读者作为练习

// Part a.
final case class Point(x: Int, y: Int) {
  // Part b.
  def isLeftOf(that: Point): Boolean =
    this.x < that.x

  def isAboveOf(that: Point): Boolean =
    this.y > that.y

  def isRightOf(that: Point): Boolean =
    !isLeftOf(that)

  def isBelowOf(that: Point): Boolean =
    !isAboveOf(that)
}

// Part c.
final case class Rectangle(topLeft: Point, bottomRight: Point) {
  // Part d.
  def contains(point: Point): Boolean =
    (point isBelowOf this.topLeft) &&
    (point isRightOf this.topLeft) &&
    (point isAboveOf this.bottomRight) &&
    (point isLeftOf this.bottomRight)
}

Rectangle(Point(0, 0), Point(5, 5)) contains Point(3, 3)
// res: Boolean = true

_注意:此解决方案包含一些关于isRightOf和isBelowOf的实现的bug,可传递地包含。它的解决方案留给读者作为练习。

任何输入或帮助都将不胜感激-编写编译代码。如果你不理解编译器的错误,就把它贴在上面。对于d部分:';'期望值,但找到“return”。@同样重要的问题是什么?有什么问题?什么有效,什么无效?我想验证我目前所得到的是否有意义,因为我还是Scalay的新手。你有很多语法错误和错误做法。举几个例子,使用return——如果没有其他。整数不存在,您的意思是Int.Scala或我知道的任何其他编程语言都不允许您检查类似于p.topLeft.x