Java Programming: Problem 1 (name this Lab10_Problem1) This program will do seve
ID: 3761522 • Letter: J
Question
Java Programming: Problem 1 (name this Lab10_Problem1)
This program will do several things. Implement the features as individual methods.
Use the main() method to create and populate the array with 30 random integers in the range of 0 to 500.
Pass the array to a method named fvLowVal and in the method, find and display the lowest value.
Pass the array to a method named fvHighVal and in the method, find and display the highest value.
Pass the array to a method named fdCalcMean and in the method, calculate the mean; return the mean to the main() method and display it there to two places to the right of the decimal point.
Output example (code accordingly to get this verbiage and format; your actual values will vary):
Lowest value: 3
Highest value: 197
Mean value: 47.23
Explanation / Answer
Program code:
import java.util.Random;
public class mean {
float fdCalcMean (int s[]) ////method to find mean of the array
{
int sumOfNumbers = 0;
for (int i = 0; i < s.length; i++){
sumOfNumbers += s[i];
}
return sumOfNumbers / s.length;
}
int fvHighVal (int s[])//method to find highest value in the array
{
int max = s[0] ;
for (int i = 0; i < s.length; i++){
if(s[i]>max)
{
max=s[i];
}
}
return max;
}
int fvLowVal (int s[])//method to find lowest value in the array
{
int min = s[0] ;
for (int i = 0; i < s.length; i++){
if(s[i]<min)
{
min=s[i];
}
}
return min;
}
public static final void main(String args[]){
int a[]=new int[30];
mean m1=new mean();
Random randomGenerator = new Random();
for (int i = 0; i < 30; i++){
int randomNumebr = randomGenerator.nextInt(500);//generating numbers with in the range of 500 numbers
a[i]=randomNumebr;
}
System.out.println("Random numbers in the array are:");
for (int i = 0; i < 30; i++){
System.out.print(a[i]+" ");//display randomNumebr;
}
System.out.println(" Lowest value:"+m1.fvLowVal(a));
System.out.println("Highest value:"+m1.fvHighVal(a));
float d=m1.fdCalcMean(a);
String strDouble = String.format("%.2f", d);
System.out.println("Mean value:"+strDouble);
//System.out.print(d);
}
}
Sample output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.