0

enter image description hereWe were given a problem in scala to define a procedure that that takes a line segment as an argument and returns its midpoint(the point whose coordinates are the average of the coordinates of the end points.) When I try to compile the program it gives me two errors, namely type mismatch errors in my midpointSegment method. I don't get why it requires a String. Can anyone point out my mistake? Below is my code.

class Point(x: Int, y: Int) {
  def xCoord = x
  def yCoord = y    

  def makeString(m: Point) = "Point" + "(" + x + "," + y + ")" 
}

class LineSegment(x: Point, y: Point) {
  def startSeg = x
  def endSeg = y

  def midpointSegment(m: LineSegment) = ((startSeg + m.startSeg) / 2, 
    (endSeg + m.endSeg) / 2)

  def makeString(m: LineSegment) = 
    "LineSegment" + "(" + x.makeString(x) + "," + y.makeString(y) + ")"
}

object Mp5 {
  def main(args: Array[String]): Unit = {   
    val aLine1 = new Point(1, 2)
    val aLine2 = new Point(5, 4)
    val aLineSegment1 = new LineSegment(aLine1, aLine2)
    val aLineSegment2 = new LineSegment(new Point(-3, 5), new Point(8, -1))

    println(aLine1.makeString(aLine1))
    println(aLine2.makeString(aLine2))
    println(aLineSegment1.makeString(aLineSegment1)) 
    println(aLineSegment2.makeString(aLineSegment2))

    println(aLineSegment1.midpointSegment(aLineSegment2))
  }
}

1 Answer 1

2

You are trying to add two points, since startSeg and m.startSeg are points. You haven't defined how to do this, so the compiler seems to think you are adding strings (since anything can be added to a string, as in definition of toString). To be honest, I wouldn't expect this error if that's the entire code, and instead something about a missing + method.

For future reference: 1. provide the actual error message and stack trace; 2. you don't need to define methods like def xCoord = x in Scala, just write val x instead of simply x in class parameters; 3. read about case classes.

Sign up to request clarification or add additional context in comments.

9 Comments

Is it possible that I can make a separate midPoint method to calculate the midpoint for two points then call that method for my midpointSegment method? Like this: def midPoint(m: Point) = ((xCoord + m.xCoord) / 2, (yCoord + m.yCoord)/ 2) def midpointSegment(m: Point) = midPoint(endSeg, m.endSeg) although it gives me an error is there anyway to implement this correctly?
You need to write def midPoint(m: Point) = new Point((xCoord + m.xCoord) / 2, (yCoord + m.yCoord) / 2). You are returning just a pair of Ints.
when I tried that, the compiler says it can't find the values xCoord and yCoord
Is this definition inside class Point?
Nope, If I placed that in class Point I would still need another midpoint function which I think should be placed in class LineSegment to get the midpoints of the endpoints of two linesegments
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.