Question: Design a C# class upDown that encapsulates a number x and: supports puzzle(y) which returns x*y if in up mode returns x/y if in down
- Design a C# class upDown that encapsulates a number x and:
- supports puzzle(y) which
returns x*y if in up mode
returns x/y if in down mode
- supports refine() which
increments x if alive and in up mode
decrements x if alive and in down mode
- switches up/down mode whenever x becomes a multiple of 10
- negates alive mode when the number of switches (in #c above) exceeds a threshold
- supports reset()
Define this class, providing all implementation details
public class upDown() {
private int x;
private bool mode;
private bool alive;
private uint switch;
private uint const SWITCH_LIMIT = 10;
public upDown(int num) {
x = num;
mode = true; // true is up, is false
alive = true;
switch = 0;
}
public int puzzle(int y) {
if (alive) {
if (mode) {
return x*y;
}
return x/y;
}
return 0;
}
public void refine() {
if (alive
if (mode) {
x++;
} else if (!mode) {
x--;
}
if (x % 10 == 0) {
mode = !mode;
switch++;
}
if (switch > SWITCH_LIMIT) {
alive = false;
}
}
}
public void revive() {
if (!alive) {
alive = true;
switch = 0;
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
