-4
 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;
           }
    }
Frederik.L
  • 5,522
  • 2
  • 29
  • 41
user2317726
  • 1
  • 1
  • 2

2 Answers2

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
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