Scanner scan= new Scanner(System.in); System.out.println("Enter the size of Array :"); int size; size = scan.nextInt(); int numArray[]= new int [size+1];
for (int i=1 ; i<=size; i++ ) {
System.out.println("Enter the Number "+ i + " :");
numArray[i]= scan.nextInt(); }
Sum sumObject = new Sum();
Average averageObject = new Average(); Maximum maxObject = new Maximum();
// creating thread to calculate sum of all entered numbers
Thread thrd1 = new Thread(new Summation(numArray, sumObject));
// creating thread to calculate average of all entered numbers
Thread thrd2 = new Thread(new Averaging(numArray, averageObject));
// creating thread to calculate average of all entered numbers
Thread thrd3 = new Thread(new Maximizing(numArray, maxObject));
new java.util.Timer().schedule(new java.util.TimerTask() {
@Override public void run() { thrd1.start(); startTimer(); try { thrd1.join(); System.out.println("The sum of entered numbers is " + sumObject.getSum() + "\n and the Time Complexity of Summation is " + stopTimer() + " nanoseconds\n\n"); } catch (InterruptedException ie) { } } }, 3000); new java.util.Timer().schedule(new java.util.TimerTask() { @Override public void run() { thrd2.start(); startTimer(); try { thrd2.join(); System.out.println("The average of entered numbers is " + averageObject.getAverage() + "\n and the Time Complexity of Averaging is " + stopTimer() + " nanoseconds\n\n"); } catch (InterruptedException ie) { } } }, 6000);
new java.util.Timer().schedule(new java.util.TimerTask() {
@Override public void run() { thrd3.start(); startTimer(); try { thrd3.join(); System.out.println("The maximum of entered numbers is " + maxObject.getMaximum() + "\n and the Time Complexity of Finding Maximum is " + stopTimer() + " nanoseconds\n\n"); } catch (InterruptedException ie) { } } }, 9000);
// Sum class Sum { private int sum;
public int getSum() {
return sum; }
public void setSum(int sum) {
this.sum = sum; } }
class Summation implements Runnable {
private int[] numArray = new int[10]; private Sum sumValue;
public Summation(int[] numArray, Sum sumValue) {
this.numArray = numArray; this.sumValue = sumValue; } public void run() { int sum = 0; for (int i = 0; i < numArray.length; i++) { sum += numArray[i]; } sumValue.setSum(sum); } }
// Average class Average { private int average;
public int getAverage() {
return average; }
public void setAverage(int average) {
this.average = average; } }
class Averaging implements Runnable {
private int[] numArray = new int[10]; private Average averageValue;
public Averaging(int[] numArray, Average averageValue) {
int average = 0; for (int i = 0; i < numArray.length; i++) { average += numArray[i]; } average /= numArray.length; averageValue.setAverage(average); } }
// Maximum class Maximum { private int maximum;
public int getMaximum() {
return maximum; }
public void setMaximum(int maximum) {
this.maximum = maximum; } }
class Maximizing implements Runnable {
private int[] numArray = new int[10]; private Maximum maxValue;
public Maximizing(int[] numArray, Maximum maxValue) {