ryanbecker24
asked on
How do I display the smallest number in a collection?
I keep getting an error at public void displaySmallest()
This is my code:
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(numbe r));
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("Positi ve Numbers");
int number = 0;
while (number < numbers.size()) {
if (numbers.get(number) >= 0.0) {
System.out.println(numbers .get(numbe r));
}
number++;
}
System.out.println("Negati ve 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("Positi ve Numbers");
while (counter < length)
{
sum = sum + this.numbers.get(counter);
counter++;
}
System.out.println(sum);
//}
System.out.println("Negati ve 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("Averag e is " + (sum / count));
}
}
/**
* Display the smallest number in the collection
*/
public void displaySmallestNumber()
Double smallest = Double.MAX_VALUE;
for (Double aNumber : numbers)
{
if (aNumber < smallest)
{
smallest = aNumber;
}
}
System.out.println("value" + smallest);
}
This is my code:
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
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("Positi
int number = 0;
while (number < numbers.size()) {
if (numbers.get(number) >= 0.0) {
System.out.println(numbers
}
number++;
}
System.out.println("Negati
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("Positi
while (counter < length)
{
sum = sum + this.numbers.get(counter);
counter++;
}
System.out.println(sum);
//}
System.out.println("Negati
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("Averag
}
}
/**
* Display the smallest number in the collection
*/
public void displaySmallestNumber()
Double smallest = Double.MAX_VALUE;
for (Double aNumber : numbers)
{
if (aNumber < smallest)
{
smallest = aNumber;
}
}
System.out.println("value"
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
https://www.experts-exchange.com/questions/26553146/How-do-I-display-the-smallest-number-in-a-collection.html?anchorAnswerId=33935056#a33935056
please respond to my clarification question in that link