Write static methods as follows: • double sum(double[] a) – returns the sum of t
ID: 3677594 • Letter: W
Question
Write static methods as follows:
• double sum(double[] a) – returns the sum of the entries in the array a.
• double[] fill(int n, double v) – returns an array of n doubles, all of whose values are v.
• double[] random (int n) – return an array of n random doubles.
• double min(double[] a) – returns the minimum element of the array a.
• double max(double[] a) – returns the maximum element of the array a.
• double average(double[] a) – returns the average (or mean) of the elements of the array a.
• double variance(double[] a) – returns the variance of the elements of the array a(note: this
method should call your average method).
• double stdDev(double[] a) – returns the standard deviation of the elements of the array a (note:
this method should call your variance method).
• double[] read() – reads an array from the keyboard and returns it (read the number of elements
first, and then that many numbers).
• void print(double[] a) – prints an array, a, on a single line with commas between elements (do
NOT use Arrays.toString()).
Write a Main class with a main method that tests your methods appropriately (i.e. by calling each method at least once with various inputs), and printing the results.
Explanation / Answer
program:
import java.io.*;
import java.util.Scanner;
import java.util.Random;
import java.lang.*;
public class Main {
public static void main(String []args)throws IOException
{
double []a={4.5,5.6,8.9,56.8,96.3,100.5,1236.56};
System.out.println("{4.5,5.6,8.9,56.8,96.3,100.5,1236.56} array sum is:"+Main.sum(a));
double []b={9.5,5.6,10.9,56.8,96.3,100.5,136.56};
System.out.println("{9.5,5.6,10.9,56.8,96.3,100.5,136.56} array average is:"+Main.average(b));
double []c={1.0,5.6,10.19,456.8,96.34,100.5,136.56};
System.out.println("{1.0,5.6,10.19,456.8,96.34,100.5,136.56} array min is:"+Main.min(c));
double []d={49.5,541.6,10.94,56.8,946.3,4100.5,136.56};
System.out.println("{49.5,541.6,10.94,56.8,946.3,4100.5,136.56} array max is:"+Main.max(d));
double []e={4.45,5.4,8.9,562.8,926.3,200.5,136.56};
System.out.println("{4.45,5.4,8.9,562.8,926.3,200.5,136.56}; array variance is:"+Main.variance(e));
double []f={4.5,51.6,18.9,56.8,96.3,4.5,126.56};
System.out.println("{4.5,51.6,18.9,56.8,96.3,4.5,126.56}; array stadardiviation is:"+Main.stdDev(f));
Scanner scan=new Scanner(System.in);
System.out.println("Enter a value for double array");
double value=scan.nextDouble();
System.out.println("Enter a value for double array size");
int n=scan.nextInt();
Main.print(Main.fill(n, value));
System.out.println("Enter a value for random double array size");
n=scan.nextInt();
Main.print(Main.random(n));
}
public static double sum(double[] a)
{double sum=0.0;
for(int i=0;i<a.length;i++)
{
sum=sum+a[i];
}
return(sum);
}
public static double[] fill(int n, double v)
{
double[] arr=new double[n];
for(int i=0;i<n;i++)
{
arr[i]=v;
}
return(arr);
}
public static double[] random (int n)
{
double[] arr=new double[n];
Random ran=new Random();
for(int i=0;i<n;i++)
{
arr[i]=ran.nextDouble();
}
return(arr);
}
public static double min(double[] a)
{double min=a[0];
for(int i=1;i<a.length;i++)
{
if(min>a[i])
min=a[i];
}
return(min);
}
public static double max(double[] a)
{
double max=a[0];
for(int i=1;i<a.length;i++)
{
if(max<a[i])
max=a[i];
}
return(max);
}
public static double average(double[] a)
{
return(Main.sum(a)/a.length);
}
public static double variance(double[] a)
{
double avg=Main.average(a);
double var=0.0;
for(int i=0;i<a.length;i++)
{
var=var+(a[i]-avg)*(a[i]-avg);
}
return(var/a.length);
}
public static double stdDev(double[] a)
{
return(Math.sqrt(Main.variance(a)));
}
public static double[] read()
{
Scanner scan=new Scanner(System.in);
System.out.println("Array Size:");
int size=scan.nextInt();
double []a=new double[size];
for(int i=0;i<size;i++)
a[i]=scan.nextDouble();
return(a);
}
public static void print(double[] a)
{
System.out.print("Array is:");
for(int i=0;i<a.length;i++)
System.out.print(a[i]+",");
}
}
output:
{4.5,5.6,8.9,56.8,96.3,100.5,1236.56} array sum is:1509.1599999999999
{9.5,5.6,10.9,56.8,96.3,100.5,136.56} array average is:59.45142857142857
{1.0,5.6,10.19,456.8,96.34,100.5,136.56} array min is:1.0
{49.5,541.6,10.94,56.8,946.3,4100.5,136.56} array max is:4100.5
{4.45,5.4,8.9,562.8,926.3,200.5,136.56}; array variance is:106787.24886938775
{4.5,51.6,18.9,56.8,96.3,4.5,126.56}; array stadardiviation is:43.34190826722905
Enter a value for double array
5
Enter a value for double array size
9
Array is:5.0,5.0,5.0,5.0,5.0,5.0,5.0,5.0,5.0,Enter a value for random double array size
5
Array is:0.7532778806758081,0.5666831682372133,0.715611116726271,0.38303758774708574,0.45341469110725485,BUILD SUCCESSFUL (total time: 57 seconds)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.