In: Computer Science
Math.java
import java.util.Scanner;
public class Math {
private static Scanner in = new Scanner(System.in);
public static void readValues(double[] array)
{
// the inbuilt property of array is length that returns the size of
the array
int arrayLength = array.length;
for(int i = 0 ; i < arrayLength ; i++)
{
System.out.print("Enter element " + (i+1) + ": ");
array[i] = in.nextDouble();
in.nextLine(); // clear buffer
}
System.out.println("--------------------");
}
public static double averageTemperature(double[] array)
{
double sum = 0;
for(int i = 0 ; i < array.length ; i++)
sum += array[i];
return (sum/array.length);
}
public static void printTemperatures(double[] array) {
System.out.println("Array contains: ");
for(int i = 0 ; i < array.length ; i++)
System.out.println("Temperature " + (i+1) + ": "+array[i]);
System.out.println("--------------------");
}
private static double sumOfArray(double[] array)
{
double sum = 0;
for(int i = 0 ; i < array.length ; i++)
sum += array[i];
return sum;
}
private static double returnArray(double[] array, double[] array1,
double[] array2, int choice)
{
// choice = 1, maximum size
// choice = 2, largest array
if(choice == 1)
{
int size1 = array.length;
int size2 = array1.length;
int size3 = array2.length;
if(size1 > size2 && size1 > size3)
return size1;
else if(size2 > size1 && size2 > size3)
return size2;
else
return size3;
}
else if(choice == 2)
{
// compute largest on the basis of sum
double sum1 = 0, sum2 = 0, sum3 = 0;
sum1 = sumOfArray(array);
sum2 = sumOfArray(array1);
sum3 = sumOfArray(array2);
if(sum1 > sum2 && sum1 > sum3)
return sum1;
else if(sum2 > sum1 && sum2 > sum3)
return sum2;
else
return sum3;
}
return -1;
}
public static double returnLargestArrayOrSize(int choice, double[]
array, double[] array1, double[] array2)
{
// choice = 1, maximum size
// choice = 2, largest array
double retVal = returnArray(array, array1, array2, choice);
return retVal;
}
private static double maximumValue(double num1, double num2, double
num3)
{
if(num1 > num2 && num1 > num2)
return num1;
else if(num2 > num3 && num2 > num1)
return num2;
else
return num3;
}
public static double[] maximumArray(double[] array, double[]
array1, double[] array2)
{
int size1 = array.length;
int size2 = array1.length;
int size3 = array2.length;
int maxSize = 0;
if(size1 > size2 && size1 > size3)
maxSize = size1;
else if(size2 > size1 && size2 > size3)
maxSize = size2;
else
maxSize = size3;
double maxArray[] = new double[maxSize];
// compute the largest element from each array
for(int i = 0 ; i < maxSize ; i++)
{
// check whether the current index is less than size of each
array
double num1 = 0, num2 = 0, num3 = 0;
if(i < size1)
num1 = array[i];
if(i < size2)
num2 = array1[i];
if(i < size3)
num3 = array2[i];
// if size of array is less than the current loop varaible, in that
case
// value is assumed to be 0;
maxArray[i] = maximumValue(num1, num2, num3);
}
return maxArray;
}
}
ArrayDemo.java
import java.util.Random;
public class ArrayDemo {
public static void main(String[] args) {
Random random = new Random();
// create 3 array of random size
// random.nextInt(6) will generate a random number between 0 to
5
double array[] = new double[random.nextInt(6)];
double array1[] = new double[random.nextInt(6)];
double array2[] = new double[random.nextInt(6)];
// read values in integer array
System.out.println("Array 1: \n");
Math.readValues(array);
System.out.println("Array 2: \n");
Math.readValues(array1);
System.out.println("Array 3: \n");
Math.readValues(array2);
double averageTemp = Math.averageTemperature(array);
double averageTemp1 = Math.averageTemperature(array1);
double averageTemp2 = Math.averageTemperature(array2);
// print array
Math.printTemperatures(array);
Math.printTemperatures(array1);
Math.printTemperatures(array2);
double max = Math.returnLargestArrayOrSize(1, array, array1,
array2);
System.out.println("Maximum size: " + max);
max = Math.returnLargestArrayOrSize(2, array, array1,
array2);
System.out.println("Largest array(on sum basis): " + max);
System.out.println("\nAverage temperature of array 1: " +
averageTemp);
System.out.println("\nAverage temperature of array 2: " +
averageTemp1);
System.out.println("\nAverage temperature of array 3: " +
averageTemp2);
}
}
Output: