public class exercises { public static void main (string[] args) {//int[] data={
ID: 662229 • Letter: P
Question
public class exercises { public static void main (string[] args) {//int[] data={50,20,45}; //int [] data2={-4,-1,-3,-10,-2}; //system.out,println(" min element in data is:" +find min (data)); //system.out.println("min element data2 is :" +find min(data2)); //todo:use the code examples above to solve for the following //"max element of data is: ?"; //"max element of data2 is : ?"; //"range of the data is : ?"; //range of data2 is: ?";} //to do:find min(int [] input) //todo:find max(int[] input) //todo :find range (int[] input) //must reuse find min() and find max() //todo:sum(int[]input }Explanation / Answer
/*The java program Exercise3 that calls the methods findMin,findMax,findRange and sum
* on two integer data arrays and print the resutls on console.
* */
import java.util.Arrays;
//Exercise3.java
public class Exercise3
{
public static void main(String[] args)
{
//declare and intialize the arrays,data and date2
int[] data={50,100,20,45};
int[] data2={-4,-1,-3,-10,-2};
//To display the elements in the array , use toString method of Arrays class
System.out.println("date : "+Arrays.toString(data));
System.out.println("date2 : "+Arrays.toString(data2));
System.out.println("Min element in data is:"+findMin(data));
System.out.println("Min element in data2 is:"+findMin(data2));
System.out.println("Max element in data is:"+findMax(data));
System.out.println("Max element in data2 is:"+findMax(data2));
System.out.println("Range of data is:"+findRange(data));
System.out.println("Range of data2 is:"+findRange(data2));
System.out.println("Sum of values in data is:"+sum(data));
System.out.println("Sum of values in data2 is:"+sum(data2));
}
/*The method sum that takes an array of integer values and
returns the sum of the values in the input data array.*/
private static int sum(int[] data)
{
//initilaize total variable of integer type to zero
int total=0;
for (int index = 0; index < data.length; index++)
{
//add elements at index of array ,data to total variable
total=total+data[index];
}
//returns total value
return total;
}
/*The method findRange that takes an array of integer values and
returns the range of the values in the input data array.*/
private static int findRange(int[] data)
{
//call findMin and store in min varibale
int min=findMin(data);
//call findMax and store in max varibale
int max=findMax(data);
//range=max-min
//return the range value
return max-min;
}
/*The method findMax that takes the input argument data as
* input array and returns the maximum element as return value
* */
private static int findMax(int[] data)
{
//assume that the starting element at index =0 is maximum element
int max=data[0];
//repeat for loop starting from index=1
for (int index = 1; index < data.length; index++)
{
//check if value at index is less than max element
if(data[index]>max)
max=data[index];
}
//return maximum value
return max;
}
/*The method findMin that takes the input argument data as
* input array and returns the minimum element as return value
* */
private static int findMin(int[] data)
{
//assume that the starting element at index =0 is minimum element
int min=data[0];
//repeat for loop starting from index=1
for (int index = 1; index < data.length; index++)
{
//check if value at index is less than min element
if(data[index]<min)
min=data[index];
}
//return minimum value
return min;
}
}
--------------------------------------------------------------------------------------------------------------------------------------------------------
Sample output:
date : [50, 100, 20, 45]
date2 : [-4, -1, -3, -10, -2]
Min element in data is:20
Min element in data2 is:-10
Max element in data is:100
Max element in data2 is:-1
Range of data is:80
Range of data2 is:9
Sum of values in data is:215
Sum of values in data2 is:-20
Note : If you dont need to display elements in the array, remove the lines that contains Arrays.toString
Hope this helps you....
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.