How do I display the smallest number in a collection?

ryanbecker24
ryanbecker24 used Ask the Experts™
on
I am new to Java. I want to display the smallest number in the collection?

This is what I have so far:

import java.util.ArrayList;


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()
    {
        int number = 0;

        while (number < numbers.size()) {
            System.out.println(numbers.get(number));
            number++;
        }
    }

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

        int number = 0;

        while (number < numbers.size()) {
            if (numbers.get(number) >= 0.0) {
                System.out.println(numbers.get(number));
            }

            number++;
        }

        System.out.println("Negative Numbers");

        for (Double aNumber : numbers) {
            if (aNumber < 0.0) {
                System.out.println(aNumber);
            }
        }
    }

    /**
    * Display the sum of all numbers in the collection
    */
    public void displaySum()
    {
        double sum = 0.0;
        int length = this.numbers.size();
        int counter = 0;

        while (counter < length) {
            sum = sum + this.numbers.get(counter);
            counter++;
        }

        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 sum = 0.0;
        int length = this.numbers.size();
        int counter = 0;
        System.out.println("Positive Numbers");

        while (counter < length)
        {
            sum = sum + this.numbers.get(counter);
            counter++;
        }
       

        System.out.println(sum);
        //}
        System.out.println("Negative Numbers");

        for (Double aSum : numbers)
        {
            if (aSum < 0.0)
            {
                System.out.println(aSum);
            }
        }
    }


/**
 * Display the average of all the positive numbers
 */

public void displayPositiveAvg()
{
  double sum = 0.0;
  int count = 0;
  for(Double number : numbers)
  {
    if(number > 0.0)
    {
      sum += number;
      count++;
    }
  }

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

public void displaySmallestNumber()
for(count = 0; count < max; count++)
{
if(value < smallest)
value = smallest
}
else{
//do nothing
}
System.out.println("value" + smallest)
}
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Expert of the Quarter 2010
Expert of the Year 2010

Commented:

/**
* Display the smallest number in the collection
*/

public void displaySmallestNumber()
Double smallest = Double.MAX_VALUE;
        for (Double aNumber : numbers) {
            if (smallest < aNumber) {
                smallest = aNumber;
            }
        }
System.out.println("value " + smallest)
}

Open in new window

Expert of the Quarter 2010
Expert of the Year 2010

Commented:

/**
* Display the smallest number in the collection
*/

public void displaySmallestNumber()
Double smallest = Double.MAX_VALUE;
        for (Double aNumber : numbers) {
            if (aNumber < smallest) {  // wrong in last post
                smallest = aNumber;
            }
        }
System.out.println("value " + smallest)
}

Open in new window

Author

Commented:
Thanks but for some reason mine is not compiling
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

Commented:
You could also modify the addNumber method so that it inserts the numbers into a sorted arrayList. Then you would just return the first index for the getSmallest.
public void addNumber(double number)
    {
    	for(int count = 0; count < numbers.size(); count++){
    		if(number < numbers.get(count)){
    			numbers.add(count, number);
    			return;
    		}
    	}
    	numbers.add(number);
    }

Open in new window

Commented:
This is my entire chunk of code that i got to compile.
import java.util.ArrayList;

public class Numbers {
    private ArrayList<Double> numbers;

    public static void main(String[] args) {
    	Numbers n = new Numbers();
    	n.addNumber(5);
    	n.addNumber(6);
    	n.addNumber(4);
    	n.displayNumbers();
    	
    }

    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)
    {
    	for(int count = 0; count < numbers.size(); count++){
    		if(number < numbers.get(count)){
    			numbers.add(count, number);
    			return;
    		}
    	}
    	numbers.add(number);
    }

    /**
     * Display all numbers in the collection
     */
    public void displayNumbers()
    {
        int number = 0;

        while (number < numbers.size()) {
            System.out.println(numbers.get(number));
            number++;
        }
    }

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

        int number = 0;

        while (number < numbers.size()) {
            if (numbers.get(number) >= 0.0) {
                System.out.println(numbers.get(number));
            }

            number++;
        }

        System.out.println("Negative Numbers");

        for (Double aNumber : numbers) {
            if (aNumber < 0.0) {
                System.out.println(aNumber);
            }
        }
    }

    /**
    * Display the sum of all numbers in the collection
    */
    public void displaySum()
    {
        double sum = 0.0;
        int length = this.numbers.size();
        int counter = 0;

        while (counter < length) {
            sum = sum + this.numbers.get(counter);
            counter++;
        }

        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 sum = 0.0;
        int length = this.numbers.size();
        int counter = 0;
        System.out.println("Positive Numbers");

        while (counter < length)
        {
            sum = sum + this.numbers.get(counter);
            counter++;
        }
       

        System.out.println(sum);
        //}
        System.out.println("Negative Numbers");

        for (Double aSum : numbers)
        {
            if (aSum < 0.0)
            {
                System.out.println(aSum);
            }
        }
    }


/**
 * Display the average of all the positive numbers
 */

public void displayPositiveAvg()
{
  double sum = 0.0;
  int count = 0;
  for(Double number : numbers)
  {
    if(number > 0.0)
    {
      sum += number;
      count++;
    }
  }

  System.out.println("Average is " + (sum / count));
}

Open in new window

Expert of the Quarter 2010
Expert of the Year 2010
Commented:
> not compiling

That would be because of a missing { and ;
Sorry about that.
/**
* Display the smallest number in the collection
*/

public void displaySmallestNumber() {
        Double smallest = Double.MAX_VALUE;
        for (Double aNumber : numbers) {
            if (aNumber < smallest) {  // wrong in last post
                smallest = aNumber;
            }
        }
        System.out.println("value " + smallest);
}

Open in new window

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