Java - I can only get the array operations below to work with an integer whole n
ID: 3761537 • Letter: J
Question
Java - I can only get the array operations below to work with an integer whole number. I need it to output total, average, highest, and lowest in all primitive data types (int[] iarray, float[] farray, double[] darray and long[] larray). When initializing with a number that has a decimal, or a floating array, it will not convert primitive data types.
I need an output that accepts all array primitive data types at once and initializes them internally and outputs their total, average, highest, and lowest in their respective categories.
public class ArrayOperations
{
public static void main(String[] args)
{
int [] numbers = {10, 4, 3, 2, 1};
getTotal(numbers);
getAverage(numbers);
getHighest(numbers);
getLowest(numbers);
}
public static int getTotal(int[] x)
{
int total = 0;
for (int index = 0; index < x.length; index++)
{
total += x [index];
}
System.out.println("The total is "+total);
return total;
}
public static double getAverage(int[] x)
{
int total = 0;
for (int index = 0; index < x.length; index++)
{
total += x [index];
}
double average = (total/2.0);
System.out.println("The average is "+average);
return average;
}
public static int getHighest (int[] x)
{
int highest = x[0];
for (int index=1; index<x.length; index++)
{
if (x[index]>highest)
highest=x[index];
}
System.out.println("The highest value is "+highest);
return highest;
}
public static int getLowest (int[] x)
{
int lowest = x[0];
for (int index=1; index<x.length; index++)
{
if (x[index]<lowest)
lowest=x[index];
}
System.out.println("The lowest value is "+lowest);
return lowest;
}
}
Explanation / Answer
Program code:
class genricOperation<E extends Number> {
E[] numbers;
genricOperation(E[] o) {
numbers = o;
}
double total() {
double sumOfNumbers = 0.0;
for (int i = 0; i < numbers.length; i++){
sumOfNumbers += numbers[i].doubleValue();
}
return sumOfNumbers ;
}
double average() {
double sumOfNumbers = 0.0;
for (int i = 0; i < numbers.length; i++){
sumOfNumbers += numbers[i].doubleValue();
}
return sumOfNumbers / numbers.length;
}
void max()
{
E max = numbers[0] ;
for (int i = 0; i < numbers.length; i++){
if(numbers[i].intValue()>max.intValue())
{
max=numbers[i];
}
}
System.out.println(max);
}
void min()
{
E min = numbers[0] ;
for (int i = 0; i < numbers.length; i++){
if(numbers[i].intValue()<min.intValue())
{
min=numbers[i];
}
}
System.out.println(min);
}
}
public class Genericdatatypes {
public static void main(String args[]) {
Integer arr[] = { 1, 2, 3, 4, 5 };
genricOperation<Integer> intObj = new genricOperation<Integer>(arr);
double avg = intObj.average();
int a=(int) intObj.total();
System.out.println("total value of integers" + a);
System.out.println("average value of the given integers is " + avg);
System.out.println("maximum value of integers");intObj.max();
System.out.println("minimum value of integers");intObj.min();
Double dnums[] = { 1.1, 2.2, 3.3, 4.4, 5.5 };
genricOperation<Double> doubleObj = new genricOperation<Double>(dnums);
double avg1 = doubleObj.average();
double a1=(double) doubleObj.total();
System.out.println("total value of double numbers" + a1);
System.out.println("average value of the given double numbers is " + avg1);
System.out.println("maximum value of double numbers");doubleObj.max();
System.out.println("minimum value of double numbers");doubleObj.min();
// System.out.println("doubleObj average is " + w);
Long dnums1[] = {(long) 3, (long) 4, (long) 5, (long) 6, (long) 7 };
genricOperation<Long> doubleObj1 = new genricOperation<Long>(dnums1);
double avg2 = doubleObj1.average();
long a2=(long) doubleObj1.total();
System.out.println("total value of long numbers" + a2);
System.out.println("average value of the given long numbers is " + avg2);
System.out.println("maximum value of long numbers");doubleObj1.max();
System.out.println("minimum value of long numbers");doubleObj1.min();
// System.out.println("lob average is " + w2);
Float dnums3[] = { (float) 1.5, (float) 2.5, (float) 3.5, (float) 4.5, (float) 5.5 };
genricOperation<Float> doubleObj5 = new genricOperation<Float>(dnums3);
double avg3 = doubleObj5.average();
float a3=(float) doubleObj5.total();
System.out.println("total value of float numbers" + a3);
System.out.println("average value of the given float numbers is " + avg3);
System.out.println("maximum value of float numbers");doubleObj5.max();
System.out.println("minimum value of float numbers");doubleObj5.min();
}
}
Sample output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.