Need help is scala code

We need to perform below task

Define a Class Rectangle . Class Rectangle has a constructor taking two integer parameters, length and breadth . It has two functions area and perimeter which calculate the respective parameters for the Rectangle object created. Next, the object RectangleDemo takes two inputs which are the length and breadth of the rectangle. Create a rectangle object with these two methods and call the two functions to print the area and perimeter of rectangle.

for which written below code , please let us know issue with it as it throwing error -

class Rectangle{
int a = input1;
int b = input2;
def Area() = a* b
def Perimeter() = (a + b) / 2

}
object RectangleDemo extends App{
val input1 :Int = args(0).toInt
val input2 :Int = args(1).toInt
var variable = new Variables(input1, input2)
println(variable.Area())
println(variable.Perimeter())
}

Issue had been fixed by my self, code will be

class Rectangle (val x: Int, val y: Int) {
def Area() : Int = x + y
def Perimeter() : Int = (x + y) * 2

}
object RectangleDemo extends App{
val input1 = args(0).toInt
val input2 = args(1).toInt
var variable = new Rectangle (input1, input2)
println(variable.Area())
println(variable.Perimeter())
}

The formulae for area is still wrong.
Correct:
def Area() : Int = x * y

1 Like