Create a method in java that should calculate and return, as an ArrayList<Double
ID: 672413 • Letter: C
Question
Create a method in java that should calculate and return, as an ArrayList<Double>, the second column of the table of summaries. It should use the value of the "operation" parameter to determine whether the values are combined by summing, averaging, max-ing, or min-ing. (If, for any category, there are no values to sum, average, min, or max, the method should throw an IllegalArgumentException.)
The order of the numbers in the returned ArrayList<Double> should correspond to the order of the categories in the "categories" list. So far i have
public static ArrayList<Double> summarizeData (ArrayList<String> names, ArrayList<Double> values,
ArrayList<String> categories, int operation)
{
}
Explanation / Answer
/**
*
* The java program that takes array list of arguments of names, values ,
* categories and then finds the sum of the values , average of values,
* minimum and maximum values and prints the values on console.
* */
import java.util.ArrayList;
public class Summary
{
public static void main(String[] args)
{
//array list of names
ArrayList<String> names = new ArrayList<String>();
names.add("Texas");
names.add("Nevada");
names.add("NY");
names.add("Oregon");
names.add("Texas");
names.add("NY");
names.add("Nevada");
names.add("Nevada");
names.add("Oregon");
names.add("NY");
//array list of values
ArrayList<Double> values = new ArrayList<Double>();
values.add(5.0);
values.add(6.0);
values.add(12.0);
values.add(8.0);
values.add(9.0);
values.add(10.0);
values.add(4.0);
values.add(4.0);
values.add(17.0);
values.add(6.0);
//Add categories
ArrayList<String> categories = new ArrayList<String>();
categories.add("Texas");
categories.add("Nevada");
categories.add("NY");
categories.add("Oregon");
System.out.println("Table");
for (int index = 0; index < names.size(); index++)
{
System.out.printf("%-10s%-10.2f ",names.get(index),values.get(index));
}
//call the method summarizeDate with operation 1 (for sum)
ArrayList<Double>result=summarizeData(names, values, categories, 1);
System.out.println("---------------");
System.out.println("Summation ");
System.out.println("---------------");
for (int i = 0; i < categories.size(); i++)
{
System.out.printf("%-15s%-10.2f ",categories.get(i),result.get(i));
}
//call the method summarizeDate with operation 2(for average)
ArrayList<Double>average=summarizeData(names, values, categories, 2);
System.out.println("---------------");
System.out.println("Average ");
System.out.println("---------------");
for (int i = 0; i < categories.size(); i++)
{
System.out.printf("%-15s%-10.2f ",categories.get(i),average.get(i));
}
//call the method summarizeDate with operation 3(for minimum)
ArrayList<Double>minimum=summarizeData(names, values, categories, 3);
System.out.println("---------------");
System.out.println("Minimum ");
System.out.println("---------------");
for (int i = 0; i < categories.size(); i++)
{
System.out.printf("%-15s%-10.2f ",categories.get(i),minimum.get(i));
}
//call the method summarizeDate with operation 4(for maximum)
ArrayList<Double>maximum=summarizeData(names, values, categories, 4);
System.out.println("---------------");
System.out.println("Maximum ");
System.out.println("---------------");
for (int i = 0; i < categories.size(); i++)
{
System.out.printf("%-15s%-10.2f ",categories.get(i),maximum.get(i));
}
}
//Method summarizeData that takes the array list of names, values, categories
//and operation code(1-summation, 2 for average, 3 for minimum and 4 for maximum)
public static ArrayList<Double> summarizeData (ArrayList<String> names,
ArrayList<Double> values,
ArrayList<String> categories, int operation)
{
// throws an IllegalArgumentException. if first and second columns are not equal
if (names.size() != values.size())
{
throw new IllegalArgumentException("the columns have different sizes");
}
// throws an IllegalAgumentException. if negative values in second column
for (int i = 0; i < values.size(); i++)
{
if (values.get(i) <= 0)
{
throw new IllegalArgumentException
("column 2 contained a non-positive number: " + values.get(i));
}
}
//For summing
if(operation == 1)
{
ArrayList<Double>total=new ArrayList<Double>();
double sum=0;
for (int category = 0; category < categories.size(); category++)
{
String categoryName=categories.get(category);
for (int value = 0; value < values.size(); value++)
{
if(names.get(value).equals(categoryName))
sum+=values.get(value);
}
total.add(sum);
sum=0;
}
return total;
}
//for average
else if(operation == 2)
{
ArrayList<Double>average=new ArrayList<Double>();
double sum=0;
for (int category = 0; category < categories.size(); category++)
{
String categoryName=categories.get(category);
for (int value = 0; value < values.size(); value++)
{
if(names.get(value).equals(categoryName))
sum+=values.get(value);
}
average.add(sum);
sum=0;
}
double totalValue=0;
for (Double value : average)
{
totalValue+=value;
}
//create an array list to store percenatages
ArrayList<Double>percenates=new ArrayList<Double>();
for (Double value : average)
{
percenates.add(value/totalValue);
}
//return an array list of percentages
return percenates;
}
//for minimum
else if(operation == 3)
{
//Array list to store the minimum values
ArrayList<Double>minimum=new ArrayList<Double>();
//set MAX_VALUE to find the lowest for each category
double min=Double.MAX_VALUE;
for (int category = 0; category < categories.size(); category++)
{
String categoryName=categories.get(category);
for (int value = 0; value < values.size(); value++)
{
if(names.get(value).equals(categoryName))
{
double element=values.get(value);
//set min value
if(element<min)
min=element;
}
}
minimum.add(min);
}
//return an array of minimum values
return minimum;
}
//for maximum
else if(operation == 4)
{
//Array list to store the maximum values
ArrayList<Double>maximum=new ArrayList<Double>();
//set MIN_VALUE for max to find the maximum values in the category
double max=Double.MIN_VALUE;
for (int category = 0; category < categories.size(); category++)
{
String categoryName=categories.get(category);
for (int value = 0; value < values.size(); value++)
{
//check category name equality
if(names.get(value).equals(categoryName))
{
double element=values.get(value);
//set max value on comparision of each value
if(element>max)
max=element;
}
}
maximum.add(max);
}
//return array list of maximum values
return maximum;
}
//if no operation is selected then return null
return null;
}
}
---------------------------------------------------------------------------------------------------------------------------------------
Sample Output:
Table
Texas 5.00
Nevada 6.00
NY 12.00
Oregon 8.00
Texas 9.00
NY 10.00
Nevada 4.00
Nevada 4.00
Oregon 17.00
NY 6.00
---------------
Summation
---------------
Texas 14.00
Nevada 14.00
NY 28.00
Oregon 25.00
---------------
Average
---------------
Texas 0.17
Nevada 0.17
NY 0.35
Oregon 0.31
---------------
Minimum
---------------
Texas 5.00
Nevada 4.00
NY 4.00
Oregon 4.00
---------------
Maximum
---------------
Texas 9.00
Nevada 9.00
NY 12.00
Oregon 17.00
Hope this helps you
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.