Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that will do the following. It should ask the user how-many numb

ID: 3682689 • Letter: W

Question

Write a program that will do the following. It should ask the user how-many numbers it will read in It will then create an array of that size. Next, it will read in the numbers and fill the array. After the numbers have been read, the array will be searched to find the subscript of the largest number That subscript will be returned. That largest number will be printed, along with the subscript for that number. Then main method will ask for a number to be searched for in the array. The number which the user enters will be passed to a method as one parameter and the array will be passed as another parameter. The array will be searched to find the subscript of that number The method will either return the subscript where the number was found in the array or -1 if it was not found The main method will then print the message telling where the number was found, or it will print "Not found" if the result is -1. Finally the entire array will be printed. The program should include methods for each of the following tasks: read numbers and fill the array find the subscript of the largest number and return the subscript find the subscript of a parameter value and return the subscript (or -1 if it is not found) print the array

Explanation / Answer

Hi, Please find the below class implemented as per your requirement.

Test7.java

public class Test7 {

   /**
   * @param args
   */
   public static void main(String[] args) {
       // TODO Auto-generated method stub
       java.util.Scanner in = new java.util.Scanner(System.in);
       System.out.println("Please enter the integer array size : ");
       int n = in.nextInt();
       int a[] = new int[n];
       for(int i=0; i<n; i++){
           System.out.println("Please enter the integer number: ");
           a[i] = in.nextInt();
       }
       int subscript = findLargeNumberSubscript(a);
       System.out.println("Large Number ["+a[subscript]+"] and subscript ["+(subscript)+"]");
       System.out.println("Please Enter the search number: ");
       int num = in.nextInt();
       int searchIndex = searchElement(a, num);
       if(searchIndex < 0)
       System.out.println("Search Number "+num+" not found");
       else
       System.out.println("Search Number "+num+" Found at ["+searchIndex+"]");
      
       System.out.println("Dispaying All array Elements...");
       for(int i=0; i<a.length; i++)
           System.out.println(a[i]);
   }
   public static int findLargeNumberSubscript(int a[]){
       int max = a[0];
       int subscript = 0;
       for(int i=0; i<a.length; i++){
           if(max < a[i]){
               max = a[i];
               subscript = i;
           }
       }
       return subscript;
   }
   public static int searchElement(int a[], int num){
       int index = -1;
       for(int i=0; i<a.length; i++){
           if(a[i] == num){
               index = i;
               break;
           }
       }
       return index;
   }

}

Output:

Please enter the integer array size :
5
Please enter the integer number:
3
Please enter the integer number:
4
Please enter the integer number:
2
Please enter the integer number:
1
Please enter the integer number:
6
Large Number [6] and subscript [4]
Please Enter the search number:
9
Search Number 9 not found
Dispaying All array Elements...
3
4
2
1
6

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote