Complete the method in the SalesData class called howMany() . It is located at t
ID: 665737 • Letter: C
Question
Complete the method in the SalesData class called howMany(). It is located at the end the class. Comments indicate where your work goes. Do not alter the code outside of this method.
The howMany() method should count the number of times a specific value appears in the array. You will be searching for the value in the method's parameter variable called searchValue.
Suppose an array is filled with the following values: {80, 60, 70, 80, 90, 80, 50}. The value 80 appears 3 times. The value 70 appears 1 time. The value 85 appears 0 times. In this example, if searchValue is 80, the method should return a 3, that is how many times 80 is in the array.
Once you have altered the SalesData class, test the class with Sales.java program. Do not alter Sales.java. Because Sales.java uses the SalesData class, anytime you make a change to SaleData.java, you will also need to recompile Sales.java before you run it.
------------------------------------------------------------------------------------------------------------------
import javax.swing.JOptionPane;
import java.text.DecimalFormat;
/**
This program gathers sales amounts for the week.
It uses the SalesData class to display the total,
average, highest, and lowest sales amounts.
*/
public class Sales
{
public static void main(String[] args)
{
final int // 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()) +
" The average sales were $" +
dollar.format(week.getAverage()) +
" The highest sales were $" +
dollar.format(week.getHighest()) +
" The lowest sales were $" +
dollar.format(week.getLowest()));
//Get a value to search for
String input = JOptionPane.showInputDialog("Enter a sales amount to search for");
double searchValue = Double.parseDouble(input);
//Dispaly the number of times the search value appeared in the sales data
JOptionPane.showMessageDialog(null, searchValue + " appears " + week.howMany(searchValue) + " times.");
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);
}
}
}
-----------------------------------------------------------------------------
/**
This class keeps the sales figures for a number of
days in an array and provides methods for getting
the total and average sales, and the highest and
lowest amounts of sales.
*/
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;
}
/**
howMany method
@returns The number of times
searchValue is stored in the sales array
*/
public int howMany(double searchValue)
{
//YOUR CODE GOES HERE
return 0; //ALSO NEED TO ALTER WHAT THE METHOD RETURNS
}
}
Explanation / Answer
public int howMany(double searchValue)
{
}