Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Java Homework 06 B ased on the program Primes.java below, develop a Java program for writing, in a text file named primes_n.txt, the prime numbers
Java Homework 06
Based on the program Primes.java below, develop a Java program for writing, in a text file named primes_n.txt, the prime numbers smaller than n.
The syntax for the execution of the program will be;
$ java Primes n
For instance, the program execution $java Primes 10, will generate the text file primes_10.txt with the following content;
Total number of primes smaller than 10: 4
1: 2
2: 3
3: 5
4 7
Concepts in practice: Text files in java
Files to be used:
Primes.java
//Primes.java
/** * A program in Java with simple optimisation to find prime numbers. */ public class Primes{ public static boolean isPrime(int n) { if (n<=1) return false; if (n==2) return true; if (n%2==0) return false; for (int i=3; i<=Math.sqrt(n); i+=2) if (n%i==0) return false; return true; } public static void main(String[] args) { if (args.length==0){ System.out.println(" Find Prime Numbers smaller than n, n in [0..2^31-1]"); System.out.println(" Syntax required: java Primes n"); System.exit(0); } for (int n=2; n<=Integer.parseInt(args[0]); n++) if (isPrime(n)) System.out.println(n); } }
Files to deliver:
Primes.java, primes_1000.txt
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started