Name of program: LNFI_Array Purpose: To write the methods and the rest program.
ID: 3569370 • Letter: N
Question
Name of program: LNFI_Array
Purpose: To write the methods and the rest program. The program should fill the 20 elements of an array with random numbers between 100 and 200. The program should then determine and print the largest and smallest values within the array using two Methods Largest and Smallest. The program should then determine and print the number of values within the array that are even using a function called Even. The program should also enter a loop that will only terminate when the user inputs a -1 as a choice. The loop should continue to ask the user to guess a number that was randomly generated. The program should call the Findit function to determine if the number was in the array. The program should print out the values in the array when the user selects a -1 and then terminate.
The Method headers for the program are:
public static int Largest( int [ ] numbers ) The program will take the array as a parameter and the return value is the largest value in the array.
public static int Smallest(int [ ] numbers ) The program will take the array as a parameter and the return value is the smallest value in the array.
public static int Even(int [ ] numbers ) The program will take the array as a parameter and the return value is the count of even values in the array. Not the sum.
public static boolean Findit(int [ ] numbers, int guess ) The program will take the array as a parameter and the users guess. The return value is either true or false.
public static void PrintThem( int [ ] numbers ) The function should be used to print out the values in the array.
You should use JavaDoc tags in the comments for the program, such as @param and @return.
Explanation / Answer
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static int Largest( int [][] numbers ) {
int max=a[0][0];
for(int i=0;i<4;i++)
for(j=0;j<4;j++){
if(a[i][j]>max)
max=a[i][j];
}
return max;
}
public static int Smallest(int [][]numbers )
{int min=a[0][0];
for(int i=0;i<4;i++)
for(j=0;j<4;j++){
if(a[i][j]<max)
min=a[i][j];
}
return min;}
public static int Even(int [][]numbers){
int count=0;
System.out.println("Even numbers are ");
for(int i=0;i<4;i++)
for(j=0;j<4;j++){
if(a[i][j]%2==0){
System.out,println(""+a[i][j]+" ");
count++;
}
}
return count;
}
public static boolean Findit(int [] numbers, int guess){
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
{
if(a[i][j]==guess)
return true;
}
return false;
}
public static void PrintThem( int []numbers ){
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
{
System.out,println(""+a[i][j]+" ");
}
}
public static void main (String[] args) throws java.lang.Exception
{
int numbers[4][4];
final java.util.Random generator = new java.util.Random();
final int MIN = 100;
final int MAX = 200;
for(int i=0;i<4;i++)
for(j=0;j<4;j++){
int randomNumber = generator.nextInt(MAX - MIN) + MIN;
numbers[i][j]=randomNumber;
}
}
int largest=Largest(numbers);
System.out,println("largest number is "+largest+" ");
int smallest=Smallest(numbers);
System.out,println("smallest number is "+smallest+" ");
int ev=Even(numbers);
Scanner in = new Scanner(System.in);
int num = in.nextInt();
boolean result;
while(num!=-1){
result=Findit(numbers,num);
if(result==true)
System.out.println("number is in array ");
else
System.out.println("number not in array ");
}
PrintThem(numbers);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.