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

Name your code ”Homework4Problem2.java” Create a program that takes a file of pr

ID: 3829230 • Letter: N

Question

Name your code ”Homework4Problem2.java”

Create a program that takes a file of prime numbers generated from Problem 1 and stores them in any way of your choosing. The user may query the program as many times as they would like and it will output whether or not the number given is prime. The queries must run in log(n) time where n is the amount of prime numbers in the file. If the number that is entered is negative stop the program. If the number is greater than the largest in the file inform the user that they need a file which contains more prime numbers. Example input in ‘‘file.txt’’: 2 3 5 7 Example command line interaction with user: Enter a number. 3 3 is a prime number. Enter another number. 4 4 is not a prime number. Enter another number. 8 Current file not large enough for 8. Enter another number. -1 Bye.

//Should look very similar to this

Explanation / Answer

Hi, Please find my implementation.

Please let me know in case of any issue.

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;

public class Homework4Problem2 {

   //Pre-condition: a is a valid array of integers

   //Post-condition: returns whether or not e is in a

   public static boolean linSearch(int[] a, int e)

   {

       for(int i = 0; i < a.length; i++)

       {

           if(a[i] == e) //Found it!

               return true;

           if(a[i] > e)

               return false;

       }

       //Element is bigger than everything in the list

       return false;

   }

   public static boolean binSearch(int[] a, int e)

   {

       return binSearchHelper(a,e,0,a.length-1);

   }

   public static boolean binSearchHelper(int[] a, int e, int st, int end)

   {

       if(end < st)

       {

           return false;

       }

       if(st == end)

       {

           return e == a[st];

       }

       else

       {

           int mid = (st + end) / 2;

           if(a[mid] == e)

               return true;

           else if(a[mid] > e)

               return binSearchHelper(a,e,st, mid-1);

           else

               return binSearchHelper(a,e,mid+1,end);

       }

   }

   public static void main(String[] args) throws FileNotFoundException {

       Scanner sc = new Scanner(System.in);

       // reading file to get count of numbers in file

       Scanner fileScanner = new Scanner(new File("file.txt"));

       int count = 0;

       while(fileScanner.hasNextInt()){

           count++;

           fileScanner.nextInt();

       }

       fileScanner.close();

       // creating array to store

       int[] arr = new int[count];

       // reading file to get all numbers from file to array

       Scanner fileScanner1 = new Scanner(new File("file.txt"));

       int i = 0;

       while(fileScanner1.hasNextInt()){

           arr[i] = fileScanner1.nextInt();

           i++;

       }

       fileScanner1.close();

       int num = 0;

       System.out.print("Enter a number(negative number to stop): ");

       num = sc.nextInt();

       while(num >= 0){

           if(binSearch(arr, num)){

               System.out.println(num+" is a prime number");

           }else{

               System.out.println(num+" is NOT a prime number");

           }

           num = sc.nextInt();

       }

       sc.close();

   }

}

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