public boolean add(int v)
{
if (count < list.length) // if there is still an available slot in the array
{
if (v >= minValue || v <= maxValue) // if the value is within range
{
list[count] = v; // add the value to the next available slot
count++; // increment the counter
return true; // all okay ; Value added
}
else
{
System.out.println("Error: The value is out of range. Value not added");
return false;
}
}
else
{
System.out.println("Error: The list is full. Value not added.");
return false;
}
}
Asked
Active
Viewed 3,733 times
-4

Frederik.L
- 5,522
- 2
- 29
- 41

user2317726
- 1
- 1
- 2
-
4what is the question ? – Frederik.L Apr 25 '13 at 19:55
2 Answers
2
Assuming minValue is greater than zero, you should change the || to an && to check both ends of the range at the same time.
if (v >= minValue && v <= maxValue)
If minValue is not necessarily greater than zero
if (v >= minValue && v <= maxValue && v >= 0)

bfitzpatrick
- 1,513
- 11
- 13
-
1Shouldn't it be `&&` in-place of `||` to have the values in min max range? – Smit Apr 25 '13 at 19:58
-
1
It should be considering minValue
and maxValue
are positive
if (v >= minValue && v <= maxValue)
if minValue
is negative then you can add one more check
if(v >= 0)

Smit
- 4,685
- 1
- 24
- 28