Hello I'm trying for the correct logic for the below statements.
This is the code I tried
if (bandwidthValue > numberofusersValue + 250) {
setVpnStatus(A);
} else if (bandwidthValue >= numberofusersValue+1 && bandwidthValue <= numberofusersValue + 249) {
setVpnStatus(B);
} else if (bandwidthValue <= numberofusersValue) {
setVpnStatus(C);
}
Testing
User Entered -
BandwidthValue = 1234
NumberofUsersValue = 900
Vpn Status Expected By the user - C
PASSED
User Entered -
BandwidthValue = 1234
NumberofUsersValue = 1234
Vpn Status Expected By the user - C
PASSED
User Entered -
BandwidthValue = 1236
NumberofUsersValue = 1234
Vpn Status Expected By the user - B
Unable to set any status - FAILING
Does the code above holds good for the values of BandwidthValue = x and NumberofUsersValue = y for these statements mentioned above?
Looking at the code, I think I'd expect that to come out with A, but reading the problem description I would indeed expect C
I think the requirement has been misinterpreted - from the description, it sounds like if users is say 1000 then bandwidth 750 or lower is A, 751 to 999 is B, 1000 or more is C. It looks like you have interpreted it the other way round.
Also the middle case needs && rather than ||
NOTE that the question appears to have been significantly edited and my answer may no longer be relevant, though it was when answered.
Try to use parentheses ()
. eg (numberOfUsersValue + 250)
A potential way of going about this based on your equations:
if(bandwidthValue > numberofusersValue + 250) { //Check largest baseline
setVpnStatus(A);
} else if (bandwidthValue > numberofusersValue) { //Check second largest baseline
setVpnStatus(B);
} else { //And finally use this for anything smaller
setVpnStatus(C);
}
The following logic fulfils the requirement in your question:
if (bandwidthValue <= (numberofusersValue - 250)) {
setVpnStatus(A);
} else if (bandwidthValue != numberofusersValue && (bandwidthValue - numberofusersValue <= 250)
|| (bandwidthValue > (numberofusersValue - 250) && bandwidthValue < numberofusersValue)) {
setVpnStatus(B);
} else {
setVpnStatus(C);
}
Demo:
public class Main {
public static void main(String args[]) {
// Tests
System.out.println(getVpnStatus(1234, 900));
System.out.println(getVpnStatus(1234, 1234));
System.out.println(getVpnStatus(1236, 1234));
}
static char getVpnStatus(int bandwidthValue, int numberofusersValue) {
char vpnStatus;
if (bandwidthValue <= (numberofusersValue - 250)) {
vpnStatus = 'A';
} else if (bandwidthValue != numberofusersValue && (bandwidthValue - numberofusersValue <= 250)
|| (bandwidthValue > (numberofusersValue - 250) && bandwidthValue < numberofusersValue)) {
vpnStatus = 'B';
} else {
vpnStatus = 'C';
}
return vpnStatus;
}
}
Output:
C
C
B
Feel free to comment in case of any issue/doubt.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.