Skip to content

4.1.4.3 Shaping Up 2 (Da Streets) exercise's solution gives wrong result #79

@ibanezang

Description

@ibanezang

I just want to give feedback regarding the "4.1.4.3 Shaping Up 2 (Da Streets)" exercise.
The answer to that exercise gives the wrong result.

sealed trait Rectangular {
  def width: Double
  def height: Double
  val sides = 4
  val perimeter = 2*width + 2*height
  val area = width*height
}

case class Square(size: Double) extends Rectangular {
  val width = size
  val height = size
}

case class Rectangle(
                      val width: Double,
                      val height: Double
                    ) extends Rectangular


val square = Square(10)
square.perimeter
square.area
square.sides

val rectangle = Rectangle(5, 6)
rectangle.perimeter
rectangle.area
rectangle.sides

I found out that the square's perimeter and area are always returning 0.0
to fix it I changed width and height into def as below.

case class Square(size: Double) extends Rectangular {
  def width = size
  def height = size
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions