Question: Hi, stuck with this SCALA question, does anyone know a possible solution? Lecture Question 8 Private Objective: Practice working with OOP and private data members.
Hi, stuck with this SCALA question, does anyone know a possible solution?

Lecture Question 8 Private Objective: Practice working with OOP and private data members. Question: In a package named lecture, write an object named Change State with a method named setToZero that takes an object of type NumberProtector as a parameter and returns Unit. The setToZero method should change the input such that its _number state variable has a value of 0. Do not change the NumberProtector class. This class has a private variable _number which means you cannot change its value directly. You are given two methods, both of which have side-effects that change_number, that you can call to accomplish this task. package lecture class Number Protector (private var _number: Int) { // The value of number is private and therefore // not directly accessib. from outside this class. // The following two methods must be used // to change the _number // Decreases _number by 3 : Unit = { def reduceNumber() _number -= 3 } // Increases _number by 1 // @return the value of number after incrementing it def incNumber(): Int = { this._number += 1 this._number } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
