Not able to find why it is giving error

Hi all,

I have written simple program to calculate sum, subtract, max. But it is not working.
can someone help me to fix this issue.

case class AritmeticOps(var x: Int, var y: Int)
def add(x: Int, y: Int) = x +y
def sub(x: Int, y: Int) = x - y
def large(x: Int, y: Int)= if (x > y) x else y
def isEven(x: Int) = x % 2 == 0

object AritmeticOps {
def main(args: Array[String]): Unit = {
val ap = new AritmeticOps(1,2)**
ap.add(1,2)
}
}


Thanks in advance.

How do you know it’s not working? Does the compiler give any clues as to what is wrong?

If you indent it properly, you can often see problems such as missing braces, brackets etc.

Hi Stairs,

Thanks for your reply.
For all methods in class it is gving same error only change at method name.
Error:(2, 1) expected class or object definition
def add(x: Int, y: Int) = x +y

You can’t have top-level methods, they have to be part of some object (or class).

More importantly, your code makes no sense.

You have a case class AritmeticOps with two variables, and a bunch of functions that take one or two Int's as parameters, and you try to call those as a method on AritmeticOps That makes no sense at all.

What are you trying to encode, and what did you expect the outcome of your program to be?

The error message make it clear that you are missing the curly braces for the case class. All those methods need to be in that case class. I will also note that here you are using var when you don’t need it. You never change x or y so they should be vals and arguments to a case class are vals by default.

The is the broader question of what you are trying to do and why you are trying to do it this way, but just putting in curly braces for the case class will fix the syntax error. The discussion of approach would be a much larger one.

hi Kumar,
you don’t need second parametr list in your method of case class:

case class Operand(x:Int, y:Int){
def sum = x + y
}

Operand(1, 2).sum //  just call like this

also you don’t need new keyword to create new case class