Question: ) In C# or C++, design a class hierarchy that defines three arithmetic generators, where an object instantiated from any of the three classes: Encapsulates
- ) In C# or C++, design a class hierarchy that defines three arithmetic generators, where an object instantiated from any of the three classes:
- Encapsulates a number, say start, upon construction
- is active or inactive
- supports getNum(), replace(int)and reset()
class simpleG: getNum() returns k*start for the kth query
class oscillateG: getNum()
returns k*start for the kth query, if k is odd
returns -k*start for the kth query, if k is even
replace(q)
replaces start only if q > start
class expoG: getNum() returns k*k*start for the kth query
replace(q)
replaces start only if the number of active/inactive transitions is < q
Define the class(es), providing all implementation details, including active/inactive state transitions
public class simpleG {
private cons int FIRST_START = 20;
protected const int QUERY_LIMIT = 10;
protected int start;
protected int k;
protected bool active;
public simpleG() {
start = FIRST_START;
k = 1;
active = true;
}
// returns 0 if inactive
public virtual int getNum() {
if (active) {
save = k * start;
k++;
if (k > QUERY_LIMIT) {
active = false;
}
return save;
}
return 0;
}
public virtual replace(int num) {
if (alive) {
start = num;
}
}
public virtual reset() {
if (!active) {
start = FIRST_START;
k = 1;
active = true;
}
}
}
public class oscillateG : simpleG {
public oscillateG : simple {
}
public override int getNum() {
if (alive) {
if (k % 2 == 0) {
int save = -k * start;
k++;
if (k > QUERY_LIMIT) [
active = false;
}
return save;
}
int save = k * start;
k++;
if (k > QUERY_LIMIT) [
active = false;
}
return save;
}
return 0;
}
public override replace(int q) {
if (active && q > start) {
start = q;
}
}
}
public class expoG : simpleG {
private uint numTransitions;
public expoG() : simpleG {
numTransitions = 0;
}
public override int getNum() {
if (active) {
save = k*k * start;
k++;
if (k > QUERY_LIMIT) {
active = false;
numTransitions++;
}
return save;
}
return 0;
}
public override replace(int q) {
if (active && numTransitions < q) {
start = q;
}
}
public override reset() {
if (!active) {
base.reset();
numTransitions++;
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
