How do I display the largest and smallest number using iterators?

ryanbecker24
ryanbecker24 used Ask the Experts™
on
My code is compiling but I don't get the answer I want for the largest and smallest number.
If I type in 5 and 7, the smallest number gives me 5 twice and the largest gives me 4.9E-324.

This is my code:


/**
 * Using class with iterators
 */
import java.util.ArrayList;
import java.util.Iterator;

public class Numbers {
    private ArrayList<Double> numbers;
   

    public Numbers() {
        numbers = new ArrayList<Double>();
       
    }

    /**
     *Add a number to the collection in a client specified position or at the
     * end of the collection
     */
    public void addNumber(double number) {
        numbers.add(number);
    }

    /**
     * Display all numbers in the collection
     */
    public void displayNumbers() {
        Iterator<Double> it = numbers.iterator();
        while (it.hasNext()) {
            System.out.println(it.next());
           
        }
    }

    /**
     * Display all numbers in the collection organized in 2 categories, first
     * the positive numbers, second the negative numbers
     */
    public void displayNumbersInCategories() {
        double number = 0;
        System.out.println("Positive Numbers");
       

        Iterator<Double> it = numbers.iterator();

        while (it.hasNext())
        {number = it.next();
            if (number >= 0.0)
            {
               
                System.out.println(number);
            }

           
        }

        System.out.println("Negative Numbers");
       
        it = numbers.iterator();
       

        while(it.hasNext()) {
            number = it.next();
            if (number < 0.0) {
                System.out.println(number);
            }
        }
    }

    /**
     * Display the sum of all numbers in the collection
     */
    public void displaySum() {
        double sum = 0.0;
        Iterator<Double> it = numbers.iterator();

        while (it.hasNext()) {
           sum = sum + it.next();
       
        }

        System.out.println(sum);
    }

    /**
     * Display 2 sums: the sum of all positive numbers in the collection and the
     * sum of all negative numbers in the collection
     */
    public void displaySumsInCategories() {
        double number;
        double sumPositive = 0.0;
        double sumNegative = 0.0;
       
        Iterator<Double> it = numbers.iterator();

        while (it.hasNext()) {
            number = it.next();
            if(number>0) {
                sumPositive = sumPositive + number;
            } else {
                sumNegative = sumNegative + number;
            }
        }

        System.out.println("Positive Numbers: " + sumPositive);
        System.out.println("Negative Numbers: " + sumNegative);
    }
/**
 * Display the positive average
 */
   
    public void displayPositiveAvg() {
        double sum = 0;
        double number = 0;
        int count = 0;
        Iterator<Double> it = numbers.iterator();
       
        while(it.hasNext())
        {
            number = it.next();
            {
                if(number > 0)
                {
                sum += number;
                count++;
            }
           else{
               //do nothing
               
            }
        }

        System.out.println("Average is" + (sum / count));
    }
}
/**
* Display the smallest number in the collection
*/

 public void displaySmallestNumber()
    {
       
        Double smallest = Double.MAX_VALUE;
        double number = 0;
        Iterator<Double> it = numbers.iterator();
       
        while(it.hasNext())
        {
            number = it.next();
            {  
            if (number < smallest)
            {
                smallest = number;
            }
        }
        System.out.println("Smallest number:" + smallest);  
    }
}
 
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
public void displaySmallestNumber()
    {
       
        Double smallest = Double.MAX_VALUE;
        double number = 0;
        Iterator<Double> it = numbers.iterator();
       
        while(it.hasNext())
        {
            number = it.next();
            {  
            if (number < smallest)
            {
                smallest = number;
            }
        }
        System.out.println("Smallest number:" + smallest);  
    }

In above function, you not considering the first number,i ie, it.next() returns the next number,
Dirk HaestProject manager

Commented:
This question has been classified as abandoned and is being closed as part of the Cleanup Program.  See my comment at the end of the question for more details.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial