Write a java program that have two methods: a. dotPattern () – A void method that prints this pattern: ....5 ...4 3 _2 1 b. arrayAverage : double method that creates an array of size 10. The method will ask the user to input 10 numbers and store them in an array. The method should print the array first, then return the average of the array. (print statements for the average must be in main method)
The required Java code:
import java.util.Scanner;
public class TriangleArray
{
public static void dotPattern()
{
//inner loop
for (int i= 5; i>0 ; i--)
{
//outer loop
for (int j=1; j<=i; j++)
{
if(i==j)
System.out.print(i); // print number
else
//prints dot
System.out.print(".");
}
//put the next line after printing each line
System.out.println();
}
}
public static double arrayAverage()
{
double[] array = new double[10];
double sum=0;
Scanner in = new Scanner(System.in);
for(int i=0; i<10; i++) // read array
{
System.out.print("Input a number ("+(i+1)+"/10): ");
array[i]= in.nextDouble();
sum +=array[i];
}
System.out.print("Your array is = [");
for(int i=0; i<9; i++)
System.out.print(array[i]+","); // print array
System.out.println(array[9]+"]");
return (sum/10); // return average
}
public static void main(String[] args) {
dotPattern(); // call method to print dots
System.out.println("The average of the numbers in the array is "+arrayAverage());
}
}
Step by step
Solved in 2 steps with 1 images