Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

You will be writing a program that takes two whole number ( without a decimal point ) between 0 ( inclusive ) and 1 0

You will be writing a program that takes two whole number (without a decimal point) between 0(inclusive) and 1000,000(inclusive) on the command line and find and prints out all prime numbers between those numbers. For instance, if your program is invoked with 1 and 10, your program will print [2,3,5,7].
You must:
name your main class "PrimeFinder"
put all your program/classes in a package called "prime".
check for errors in command line arguments and handle it properly.
Example Run of the program
The red color indicates the program's output. The dollar sign ($) indicates your command line prompt and what follows is what the user types on the command line (terminal).
Example 1(positive - no error):
$ java -cp . prime.PrimeFinder 115
[2,3,5,7,11,13]
Example 2(positive - no error):
$ java -cp . prime.PrimeFinder 77
[7]
Example 3(positive - no error):
$ java -cp . prime.PrimeFinder 2426
[]
Example 4(Negative - bad input):
$ java -cp . prime.PrimeFinder 10050
Error: Max must not be smaller than Min
Example 5(Negative - bad input):
$ java -cp . prime.PrimeFinder 1 abc
Error: Invalid argument(s)
Example 6(Negative - bad input):
$ java -cp . prime.PrimeFinder -1100
Error: Min out of range. Minimum must not be smaller than 0.
Example 7(Negative - bad input):
$ java -cp . prime.PrimeFinder 0100000000
Error: Max out of range. Maximum must not be greater than 1,000,000
Example 8(Negative - bad input):
$ java -cp . prime.PrimeFinder 1.422
Error: Min and Max must be whole numbers.
Example 9(Negative - missing an argument):
$ java -cp . prime.PrimeFinder 10
Usage: PrimeFinder
Additional Instructions and Hints
You must validate the command line arguments passed in by the users. The examples above cover some of the error cases that you need to handle but there may be more. Big part of your grade will be about error detection and handling.
Command line arguments appear in your main method's argument as an array of Strings (String[]). You can check the number of command line arguments (which is equal to the length of this array) and read each of them as a String, and then perform validations on them or convert them to numbers or integers. You can use the method Long.ParseLong(String s) to convert a String to a long number.
A number is a prime if and only if it's divisible by 1 and itself. To confirm that a number is prime, you need to make sure it is not divisible by any prime numbers that are smaller than or equal to the quare root of that number. For instance, to recognize if 13 is prime, you keep dividing it by all prime numbers that are smaller than or equal to the square root of 13, i.e.3, which are 2 and 3. If the number is divisible by any of those primes, then it is not a prime. To test 101 for primality, you must make sure it's not divisible by divide it by any prime smaller than or equal to the square root of 101, i.e.,2,3,5 and 7.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions