Write a java program called TestPrimaryArray that asks a user, in its main() met
ID: 675461 • Letter: W
Question
Write a java program called TestPrimaryArray that asks a user, in its main() method, how many integer values they will enter to test for primes. Then, in your main() method, you will read in the values into an array and pass the array to the following method:
// Determines which values in the array are primes
public static boolean[] primeArray(int[] array)
The primeArray() method returns an array of boolean values the same size the integer array. Each values in the boolean array is true if the corresponding number in the integer array is a, and otherwise its value is false.
Then with this information, your main() will print out each number in the original array in order on separate lines, while also indicating on each line whether or not the number is a prime number.
Design the main() method of your program such that it allows the user to re-run the program with different inputs (i.e., use a loop structure). Here is a sample output:
How many possibly prime integer values will you enter: 4
Enter your 4 values: 4 23 16 19
Value #1 was 4: That is not a prime number
Value #2 was 23: That is a prime number
Value #3 was 16: That is not a prime number
Value #4 was 19: That is a prime number Would you like to go again? no
Explanation / Answer
import java.util.*;
class TestPrimaryArray
{
public static void main(String args[])
{
int a[]=new int[1000];
int i,n;
System.out.print("How many possibly prime integer values will you enter: ");
Scanner o = new Scanner(System.in);
n=Integer.parseInt(o.nextLine());
System.out.print("Enter "+n+" values");
for(i=0;i<n;i++)
{
a[i]=Integer.parseInt(o.nextLine());
}
boolean b[] =primeArray(a);
for(i=0;i<n;i++)
{
if(b[i])
{
System.out.println("Value #"+i+" was "+a[i]+": This is prime number");
}
else
{
System.out.println("Value #"+i+" was "+a[i]+": This is not prime number");
}
}
}
public static boolean[] primeArray(int[] array)
{
int i,j,c;
boolean b[] = new boolean[array.length];
for(i=0;i<array.length;i++)
{
for(j=2;j<array[i];j++)
{
c=array[i]%j;
if(c==0)
{
b[i]=false;
break;
}
}
if(i==array[i])
{
b[i]=true;
}
}
return (b);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.