Please help to correct below code

trait ArithmeticOperations {
var x :Int
var y: Int
def add
def subtract
def multiply
def divide
}
class Variables defs (xc:Int, yc:Int) extends ArithmeticOperations {
x= xc
y= yc
def add() = x + y
def subtract() = x - y
def multiply() = x * y
def divide() = x / y
}
object TraitExample extends App{
val input1 = args(0).toInt
val input2 = args(1).toInt
var variable = new Variables(input1, input2)
println(variable.add())
println(variable.subtract())
println(variable.multiply())
println(variable.divide())
}

Are you asking for help improving working code, or help fixing non-working code?

If it’s non-working code, is there an error message, or unexpected behavior, or what?

It will be easier for us to help you if you use Markdown syntax to post properly formatted code. Code is quite hard to read without the indentation.

1 Like

When we running above code we are getting… plz let us know where is the issue in the code… i think there is small mistake only

Please help us with required issue

Your defs is an error, i.e. just class Variables (xc : Int, ... is probably what you want.

Regards
Siddhartha

Did below code after which getting error -

trait ArithmeticOperations {
var x :Int
var y: Int
def add
def subtract
def multiply
def divide
}
class Variables (xc: Int, yc: Int) extends ArithmeticOperations {
x= xc
y= yc
def add() = x + y
def subtract() = x - y
def multiply() = x * y
def divide() = x / y
}
object TraitExample extends App{
val input1 = args(0).toInt
val input2 = args(1).toInt
var variable = new Variables(input1, input2)
println(variable.add())
println(variable.subtract())
println(variable.multiply())
println(variable.divide())
}

error is -

$ scalac Traits.scala
Traits.scala:9: error: class Variables needs to be abstract, since:
it has 2 unimplemented members.
/** As seen from class Variables, the missing signatures are as follows.

  • For convenience, these are usable as stub implementations.
    */
    def x_=(x$1: Int): Unit = ???
    def y_=(x$1: Int): Unit = ???

class Variables (xc: Int, yc: Int) extends ArithmeticOperations {
^
one error found

It depends on what you want your code to mean. Why are x and y var not val? If you make them Val’s you also need to change to val x = xc for example.

Regards
Siddhartha

give below code as suggest

trait ArithmeticOperations {
val x :Int
val y: Int
def add
def subtract
def multiply
def divide
}
class Variables (xc: Int, yc: Int) extends ArithmeticOperations {
x= xc
y= yc
def add() = x + y
def subtract() = x - y
def multiply() = x * y
def divide() = x / y
}
object TraitExample extends App{
val input1 = args(0).toInt
val input2 = args(1).toInt
var variable = new Variables(input1, input2)
println(variable.add())
println(variable.subtract())
println(variable.multiply())
println(variable.divide())
}

got below error

$ scalac Traits.scala
Traits.scala:10: error: reassignment to val
x= xc
^
Traits.scala:11: error: reassignment to val
y= yc
^
two errors found

Final correct code but result not ad desired. Final code is

trait ArithmeticOperations {
val x :Int
val y: Int
def add
def subtract
def multiply
def divide
}
class Variables (xc: Int, yc: Int) extends ArithmeticOperations {
val x= xc
val y= yc
def add() = x + y
def subtract() = x - y
def multiply() = x * y
def divide() = x / y
}
object TraitExample extends App{
val input1 = args(0).toInt
val input2 = args(1).toInt
var variable = new Variables(input1, input2)
println(variable.add())
println(variable.subtract())
println(variable.multiply())
println(variable.divide())
}

Output getting as

$ scala TraitExample 5 3
()
()
()
()

It should be

$ scala TraitExample 5 3
8
2
15
1

Please help

Finally succeed code will be

trait ArithmeticOperations {
val x :Int
val y: Int
def add: Int
def subtract: Int
def multiply: Int
def divide: Int
}
class Variables (xc: Int, yc: Int) extends ArithmeticOperations {
val x= xc
val y= yc
def add() = x + y
def subtract() = x - y
def multiply() = x * y
def divide() = x / y
}
object TraitExample extends App{
val input1 = args(0).toInt
val input2 = args(1).toInt
var variable = new Variables(input1, input2)
println(variable.add())
println(variable.subtract())
println(variable.multiply())
println(variable.divide())
}

working as expected