(In Java) Create an application containing an array that stores 10 integers. The
ID: 3775729 • Letter: #
Question
(In Java) Create an application containing an array that stores 10 integers. The application should call five methods that in turn (1) display all the integers, (2) display all the integers in reverse order, (3) display the sum of the integers, (4) display all values less than a limiting argument, and (5) display all values that are higher than the calculated average value. Then, create another array that store 5 integers. Pass the two arrays to a method that will display the integer value(s), if any, that appear in both arrays (note that the two arrays can have no stored values in common). Save the file as ArrayMethodDemo.java.
Explanation / Answer
ArrayMethodDemo.java
import java.util.Scanner;
public class ArrayMethodDemo {
public static void main(String[] args) {
//Creating an arrays
int nos[]=new int[10];
int nosarr[]=new int[5];
//Declaring variables
int num,sum=0;
//Scanner class is used to read the inputs entered by the user
Scanner sc=new Scanner(System.in);
/* Getting the values entered by the
* user and populate them in an array
*/
for(int i=0;i<10;i++)
{
System.out.print("Enter Number#"+(i+1)+":");
nos[i]=sc.nextInt();
}
//Calling the methods
displayArr(nos);
displayInReverse(nos);
sum=sumOfInts(nos);
//Getting the number entered by the user
System.out.print("Enter a number :");
num=sc.nextInt();
//Calling the method
numsLessThanArgVal(nos,num);
numsHigherThanAvg(nos,sum);
/* Getting the numbers entered by the user
* and populate them into an array
*/
for(int i=0;i<nosarr.length;i++)
{
System.out.print(" Enter the Number#"+(i+1)+":");
nosarr[i]=sc.nextInt();
}
//Calling the method
AnyCommonVals(nos,nosarr);
}
// This method will display the common numbers in two arrays
private static void AnyCommonVals(int[] nos, int[] nosarr) {
System.out.println(" The Numbers which are common in both the arrays are :");
int count=0;
/* This method will find and display the numbers
* if any are common in both the arrays
*/
for(int i=0;i<nos.length;i++)
{
for(int j=0;j<nosarr.length;j++)
{
if(nos[i]==nosarr[j])
{
System.out.print(nos[i]+" ");
count++;
}
}
}
//If no elements in the common display the message
if(count==0)
{
System.out.println(" No Common numbers in both the arrays ");
}
}
/* This method will display the elements
* which are greater than average value
*/
private static void numsHigherThanAvg(int[] nos,int sum) {
//Calculating the average of the elements in the array
double avg=(double)sum/nos.length;
//Displaying the elements which are greater than average value
System.out.print(" The Numbers which are greater than "+avg+" is :");
for(int i=0;i<nos.length;i++)
{
if(nos[i]>avg)
System.out.print(nos[i]+" ");
}
}
/* Displaying the numbers in the array
* which are less than argument value
*/
private static void numsLessThanArgVal(int nos[],int num) {
System.out.print("The numbers in the array which are less than "+num+" are :");
for(int i=0;i<nos.length;i++)
{
if(nos[i]<num)
{
System.out.print(nos[i]+" ");
}
}
}
/* This method will calculate and display
* the sum of elements in the array
*/
private static int sumOfInts(int[] nos) {
int sum=0;
for(int i=0;i<nos.length;i++)
{
//Calculating the sum
sum+=nos[i];
}
//Displaying the sum
System.out.println(" Sum of Elements in the array :"+sum);
return sum;
}
//This method will the elements in the array in reverse order
private static void displayInReverse(int[] nos) {
System.out.print(" Displaying the Array Elements in Reverse Order :");
for(int i=nos.length-1;i>=0;i--)
{
System.out.print(nos[i]+" ");
}
}
/* Displaying the elements in the array
* in the same order the user entered
*/
private static void displayArr(int[] nos) {
System.out.print("Dispalying the numbers in the array :");
for(int i=0;i<10;i++)
{
System.out.print(nos[i]+" ");
}
}
}
______________________
Output:
Enter Number#1:11
Enter Number#2:22
Enter Number#3:33
Enter Number#4:44
Enter Number#5:55
Enter Number#6:66
Enter Number#7:77
Enter Number#8:88
Enter Number#9:99
Enter Number#10:100
Dispalying the numbers in the array :11 22 33 44 55 66 77 88 99 100
Displaying the Array Elements in Reverse Order :100 99 88 77 66 55 44 33 22 11
Sum of Elements in the array :595
Enter a number :44
The numbers in the array which are less than 44 are :11 22 33
The Numbers which are greater than 59.5 is :66 77 88 99 100
Enter the Number#1:12
Enter the Number#2:13
Enter the Number#3:14
Enter the Number#4:15
Enter the Number#5:16
The Numbers which are common in both the arrays are :
No Common numbers in both the arrays
__________Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.