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

The coding language is JAVA 1. Create a constant for the length of an array whic

ID: 3815704 • Letter: T

Question

The coding language is JAVA

1. Create a constant for the length of an array which is equal to 10.

2. Create a file with the name numbers, by writing to the file random integers between 1 and 1000.

3. Declare an array of integers using your constant for the length.

4. Read from the file to fill the array of integers.

5. Declare a Boolean variable that will indicate if a value is found.

6. Declare variables for your LCV, position, and value.

7. Perform the linear search.

8. At the end of the search, print the position in the array and the value found. Print “VALUE NOT FOUND” if the value was not in the array.

Explanation / Answer

//LinearSearch.java

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Random;
import java.util.Scanner;

public class LinearSearch {
static final int LENGTH = 10;;
   public static void main(String[] args) {
      
       try{
           //Random Number Instance
           Random r = new Random();
           //Creating FileWriter and BufferedWriter
           //to Write random values into file
           FileWriter fw = new FileWriter("E: umbers.txt");
           BufferedWriter bw = new BufferedWriter(fw);
           int i=0;
           //Loop for iterating to store 1000 random values
           while(i<1000){
               Integer rand = r.nextInt(1000)+1;
               //System.out.println(rand);
               bw.write(rand.toString());//writing into file
               bw.newLine();//newline into file after writing
               i++;
           }
           bw.close(); //Closing Output Streams
           fw.close();
           //Reading from file using buffer
           FileReader fr = new FileReader("E: umbers.txt");
           BufferedReader br = new BufferedReader(fr);
           //Creating integer array with length
           int arr[] = new int[LENGTH];
           String num;
           int j=0;
           //Reading from file and storing into array
           while((num=br.readLine())!=null && j<arr.length){
               arr[j] = Integer.parseInt(num);
               j++;
           }
          
           System.out.println("The Elements in the Array are:");
           for(int p=0;p<arr.length;p++){
               System.out.print(arr[p]+" ");
           }
           System.out.println(" ");
           //Declaring variables
           boolean found=false;
           int position=0,value;
          
           System.out.println("Enter the value to be searched:");
           Scanner s = new Scanner(System.in);
           value = s.nextInt();//Reading the value to be searched
          
           //Looping with LCV k to find the value in the array
           for(int k=0;k<arr.length;k++){
               if(value == arr[k]){//Coniditon for checking
                   position=k;
                   found=true;
                   break;
               }
           }
           //Printing if found
           if(found){
               System.out.println("The value "+value+" found at position "+(position+1));
           }else{
               System.out.println("Value not Found...");
           }
           br.close(); //Closing File Input Streams
           fr.close();

       }catch(Exception e){
           e.printStackTrace();
       }
   }
}


numbers.txt:

413
114
964
862
25
745
828
398
370
913
92
164
452
739
314
147
472..........  

Output:


The Elements in the Array are:
413 114 964 862 25 745 828 398 370 913

Enter the value to be searched:
999
Value not Found...

The Elements in the Array are:
106 1 158 213 209 435 815 318 346 687

Enter the value to be searched:
346
The value 346 found at position 9

The Elements in the Array are:
603 705 538 741 552 794 607 920 600 148

Enter the value to be searched:
920
The value 920 found at position 8


The Elements in the Array are:
114 941 661 708 704 3 40 967 992 309

Enter the value to be searched:
770
Value not Found...