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

Please write the codes in Java net beans Write a program that will do the follow

ID: 3682618 • Letter: P

Question

Please write the codes in Java net beans


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

import java.io.*;
import java.util.*;

class program{

   public int[] readArray(){
       int n,i=0;

       System.out.println("How many numbers you want to read: ");
       Scanner in = new Scanner(System.in);
       n = in.nextInt();

       int[] array=new int[n];

       for(i=0;i<n;++i){
           Scanner in1 = new Scanner(System.in);
           int temp = in1.nextInt();
           array[i]=temp;
       }

       return array;
   }

   public int search(int[] arr, int num){
       int i;

       for(i=0;i<arr.length;++i){
           if(arr[i]==num)
               return i;
       }
       return -1;
   }

   public int largestElement(int[] arr){
       int i,ans=0;

       for(i=1;i<arr.length;++i){
           if(arr[i]>arr[ans])
               ans=i;
       }
       return ans;
   }

   public void printArray(int[] arr){
       int i;
       for(i=0;i<arr.length;++i)
           System.out.println(arr[i]);
   }

   public static void main(String args[]){
      
       program p=new program();

       int[] array=p.readArray();

       System.out.println("Enter the number you want to search");
       Scanner in = new Scanner(System.in);
       int x = in.nextInt();

       int index=p.search(array,x);

       if(index==-1)
           System.out.println("Element not Found!!");
       else
           System.out.println("Entered number was found at index numer: "+index);

       System.out.println("Array elements are: ");

       p.printArray(array);
   }
}