• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Array with occurrences

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is what I'm trying to do using arrays.

gather sales amount for the week
display the total of sales
display the average of sale
display the highest sale amount
display the lowest sale amount

using occurrences determine how many times the amount occurred for the week

make a sales main
make a sales data

This is what i have so far but I'm confused on how to start the occurrence and where it would be placed in order to get the information from the array

public class SalesData
{
private double[] sales; // The sales data

/**
The constructor copies the elements in
an array to the sales array.
@param s The array to copy.
*/

public SalesData(double[] s)
{
// Create an array as large as s.
sales = new double[s.length];

// Copy the elements from s to sales.
for (int index = 0; index < s.length; index++)
sales[index] = s[index];
}

/**
getTotal method
@return The total of the elements in
the sales array.
*/

public double getTotal()
{
double total = 0.0; // Accumulator

// Accumulate the sum of the elements
// in the sales array.
for (int index = 0; index < sales.length; index++)
total += sales[index];

// Return the total.
return total;
}

/**
getAverage method
@return The average of the elements
in the sales array.
*/

public double getAverage()
{
return getTotal() / sales.length;
}

/**
getHighest method
@return The highest value stored
in the sales array.
*/

public double getHighest()
{
double highest = sales[0];

for (int index = 1; index < sales.length; index++)
{
if (sales[index] > highest)
highest = sales[index];
}

return highest;
}

/**
getLowest method
@returns The lowest value stored
in the sales array.
*/

public double getLowest()
{
double lowest = sales[0];

for (int index = 1; index < sales.length; index++)
{
if (sales[index] < lowest)
lowest = sales[index];
}

return lowest;


}
}


public class Sales
{
public static void main(String[] args)
{
final int ONE_WEEK = 7; // Number of elements

// Create an array to hold sales amounts for a week.
double[] sales = new double[ONE_WEEK];

// Get the week's sales figures.
getValues(sales);

// Create a SalesData object, initialized
// with the week's sales figures.
SalesData week = new SalesData(sales);

// Create a DecimalFormat object.
DecimalFormat dollar = new DecimalFormat("#,##0.00");

// Display the total, average, highest, and lowest
// sales amounts for the week.
JOptionPane.showMessageDialog(null,
"The total sales were $" +
dollar.format(week.getTotal()) +
"\nThe average sales were $" +
dollar.format(week.getAverage()) +
"\nThe highest sales were $" +
dollar.format(week.getHighest()) +
"\nThe lowest sales were $" +
dollar.format(week.getLowest()));

System.exit(0);
}

/**
The getValues method asks the user to enter sales
amounts for each element of an array.
@param array The array to store the values in.
*/

private static void getValues(double[] array)
{
String input; // To hold user input.

// Get sales for each day of the week.
for (int i = 0; i < array.length; i++)
{
input = JOptionPane.showInputDialog("Enter " +
"the sales for day " + (i + 1) + ".");
array[i] = Double.parseDouble(input);
}
}

}
 
Bartender
Posts: 689
17
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

brandon mac wrote:This is what i have so far but I'm confused on how to start the occurrence and where it would be placed in order to get the information from the array



What is an 'occurence'? From the way you phrased your question it sounds like you're talking about a Java type. Is it a class that you've been given or asked to develop in a previous exercise?

Or have you just been asked to count the number of times a particular value appears in the array?
 
brandon mac
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mike,

Sorry for the confusion. The occurrence that I'm referring to is getting the count or the # of times a particular value appears given the arrays that i have mentioned.
 
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To get the occurrences of the specified number in your SalesData class you should have a method that looks like the following:

public String countOccurences(double value);

The method must do the following:

1. Declare an int variable to be used as the counter
2. Iterate over the sales array and check each value in an if statement to the value passed in... if they are equal increment the counter by 1
3. Return an appropriate String message to the user informing them about the discovery
 
Humans and their filthy friendship brings nothing but trouble. My only solace is this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic