Question

In: Computer Science

write the program named Lab06.java that contains the following four static methods: • public static double...

write the program named Lab06.java that contains the following four static methods:

• public static double max(double[] data) that returns the maximum value in the array data

• public static double min(double[] data) that returns the minimum value in the array data

• public static double sum(double[] data) that sums all items in the array and return the result

• public static double ave(double[] data) that call the sum() method and then return the average.

Once you have completed the methods above, write a simple main program that does the following:

1. Asks the user how many items will be entered

2. Creates an array of double of the correct size

3. Prompts the user and reads in the values

4. Calculates and prints out the max, min, sum, and average using the methods above.

Solutions

Expert Solution

Code

import java.util.Scanner;//for taking user input
public class Lab06 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner input=new Scanner(System.in);//creating the object of the scanner class
double []data;//declare the array name data type of double
int size;//variable that holds the numebr of element user want to eter in the array
System.out.print("How many items will be entered: ");
size=input.nextInt();
data=new double[size];//assign the size to the data array
//taking the numebr from the user one by one
for(int i=0;i<size;i++)//for loop till size
{
System.out.print("Enter the "+ (i+1) +"th element in array: ");
data[i]=input.nextDouble();
}
//printing the information
System.out.println(" Maximum element from the data array is: "+max(data));
System.out.println("Minimum element from the data array is: "+min(data));
System.out.println("Sum of the all element in data array is: "+sum(data));
System.out.println("Average of the data array is: "+ave(data));
  
}
//method that will return the element
public static double max(double[] data)
{
double maxElement=data[0];//assing the first value to maxElement variable
for(int i=1;i<data.length;i++)//for loop from 1 to length of the data array
{
if(maxElement<data[i])//if current element is bigger then max
maxElement=data[i];//change the value of max with current element
}
//return maxElement
return maxElement;
}
public static double min(double[] data)
{
double minElement=data[0];
for(int i=1;i<data.length;i++)
{
if(minElement>data[i])
minElement=data[i];
}
return minElement;
}
public static double sum(double[] data)
{
double sum=0;//varibale that hold the sum of all the element in the array
for(int i=1;i<data.length;i++)
sum+=data[i];
//return the sum
return sum;
}
//function that will find the average of the data array
public static double ave(double[] data)
{
double avg=sum(data)/data.length;
return avg;
}
}

output

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.


Related Solutions

JAVA program. Write two public classes (named exactly), TextBox and TextBoxTester.  TextBox contains the following overloaded static...
JAVA program. Write two public classes (named exactly), TextBox and TextBoxTester.  TextBox contains the following overloaded static methods called textBoxString. This method returns a String value. public static String textBoxString (int side) The returned String value, when printed, displays as the outline of a square of side characters. The character you use is up to you. Don't forget that '\n' will force a newline character into the returned String. For example, let's assume I want to use * as the character...
Write a java program that contains 3 overloaded static methods for calculating area of a circle,...
Write a java program that contains 3 overloaded static methods for calculating area of a circle, area of a cylinder and volume of a cylinder. Also create an output method which uses JOptionPaneto display instance field(s) and the result of the computing. Then code a driver class which will run and test calling each of these overloaded methods with hard-coded data and display the data and the result of the calculation by calling output method. Thanks!!
(Java) Create a program using 3 methods. The methods must be public and static. Ask the...
(Java) Create a program using 3 methods. The methods must be public and static. Ask the user for 3 numbers, then call the methods from main to print max, min, and average. The first method max (int x, int y, int z) returns the maximum value of 3 integer values. The second method min (int X, int y, int z) returns the minimum value of 3 integer values. And the third average (int x, int y, int z) returns the...
write program that develop a Java class Dictionary to support the following public methods of an...
write program that develop a Java class Dictionary to support the following public methods of an abstract data type: public class Dictionary { // insert a (key, value) pair into the Dictionary, key value must be unique, the value is associated with the key; only the last value inserted for a key will be kept public void insert(String key, String value); // return the value associated with the key value public String lookup(String key); // delete the (key, value) pair...
Write a generic method in java code public static double jaccard(HashSet A, HashSet B) that on...
Write a generic method in java code public static double jaccard(HashSet A, HashSet B) that on input two sets represented as hash sets, returns their Jaccard similarity. The following are a few sample runs: Input :    A=1, 2, 3, 4,   B=2, 4, 6, 8 Return:   0.3333333333333333 Input :    A=Larry, Michael, Shaq, Kobe, LeBron                    B=Steve, Kobe, Shaq, LeBron, Steph, Jeremy, Michael Return:   0.5 Your method must have time complexity On and space complexity O1, where n is the size of...
CIT 149 JAVA 1 program question? Write a program that will use static methods as described...
CIT 149 JAVA 1 program question? Write a program that will use static methods as described in the specifications below. Utilizing the if and else statements, write a program which will calculate a commission based on a two-tiered commission plan of 3% and 7% paid on amounts of $15,000 or less, or over $15,000 in monthly sales by a sales force paid on a commission basis. Use the output specifications below. ? Specifications There will be two classes (separate files)...
Create a program named MergeSort.java then copy the following code to your program: public static void...
Create a program named MergeSort.java then copy the following code to your program: public static void mergeSort(int[] arr){    mergeSortRec(arr, 0, arr.length-1); } private static void mergeSortRec(int[] arr, int first, int last){    if(first<last){     int mid=(first+last)/2; mergeSortRec(arr, first, mid);     mergeSortRec(arr, mid+1,last);     merge(arr, first, mid, mid+1, last);    } } private static void merge(int[] arr, int leftFirst,    int leftLast, int rightFirst, int rightLast){    int[] aux=new int[arr.length];    //extra space, this is downside of this algorithm   ...
Write a JAVA program that contains the following 4 void methods whic will print all subtrings...
Write a JAVA program that contains the following 4 void methods whic will print all subtrings of s in specific orders: printSub1(String s), printSub2(String s), printSub3(String s), and printSub4(String s) For example, if S = "abcd": printSub1 will print "a", "ab", "abc", "abcd", "b", "bc", "bcd", "c", "cd", "d" printSub2 will print "a", "ab", "b", "abc", "bc", "c", "abcd", "bcd", "cd", "d" printSub3 will print "d", "c", "b", "a", "cd", "bc", "ab", "bcd", "abc", "abcd" printSub4 will print "abcd", "bcd",...
Question 3 A java source module contains the following class with the static methods main and...
Question 3 A java source module contains the following class with the static methods main and procedure1, and the instance method procedure2 (assume given the bodies of procedure1 and procedure2): public class TestQuestion3             {                         static int result, num1 = 10;                         public static void Main( String [ ] args )                         {                                     int [ ] list1 =   { 2, 4, 6, 8, 10}, list2;                                     .    .    .                         }                         static void procedure1( void )                         {                                     .   .   .                         } void procedure2( void )...
write JAVA program have a public class named GeometricShapes that has the main() method. In the...
write JAVA program have a public class named GeometricShapes that has the main() method. In the main() method the user needs to select if he want 2D shapes or 3D shape -if user select 2D user needs to select the shape type (Square, Circle, or Triangle) after selected shape the user needs to specify whether to find the Area or the Perimeter or to find side-length (radius for the circles), accordingly the needed constructor is used. (using Polymorphism principle) -if...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT