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

Please follow instructions The program must be completed in groups of two studen

ID: 3551119 • Letter: P

Question

Please follow instructions

The program must be completed in groups of two students. You can't work with the same person you worked with for assignment 1. Your printout must names of both members of your teams. Teams of less or more than 2 students are not accepted. Turn in a printout of your program and sample output A prime number is a number that is only divisible by 1 and the number itself. For example 5, 7, 11, 19 and 123 are prime numbers but 8 is not a prime number. Write a program that reads a sequence of integer numbers (larger than 1) and displays the input numbers' prime factors. The following are a few input/output examples: Your program must contain the following methods The main method must: Prompt the user for an input number with a meaningful message. If the input value is less than or equal to 1, the user must be informed of the error in the input, and then be prompted for another input If more than three consecutive erroneous input values are entered, the program must be terminated with an appropriate error message If the input is larger than 1, then its prime factors must be displayed After displaying the prime factors, the program must ask whether or not the user wants to enter another input (Do you want to continue? (y/n)) The user must answer either yes, no, y or n using any combination of lower and upper case letters. If an invalid answer is given, the user must be informed that the answer was not acceptable, and then be prompted again for another answer. If more than three consecutive erroneous answers are given, the program must be terminated with an appropriate error message. If the answer in step 6, is Yes (in any allowed form) the program must continue from step 1. If the answer is No (in any allowed form) the program must terminate with a message informing that the program is terminated normally. All input must be done using an Scanner object i.e. Scanner inp = new Scanner (System.in); All output should be done using System.out.

Explanation / Answer

import java.util.Scanner;



public class PrimeNumbers

{

public static boolean isPrime(int m)

{

if (m <= 1)

return false;

if (m == 2)

return true;

if (m % 2 == 0)

return false;


for (int i = 3; i * i <= m; i += 2)

if (m % i == 0)

return false;


return true;

}

  

public static int nextPrime(int m)

{

m++;

while (!isPrime(m))

m++;

return m;

}


public static void main(String[] args)

{

Scanner inp = new Scanner(System.in);

boolean quit = false;

int err = 0;

int num;

String answer = "";

  

while (!quit)

{

err = 0;

  

System.out.print("Enter an integer: ");

num = inp.nextInt();

  

while (num <= 1)

{

err++;

System.out.println("Invalid input");

if (err == 3)

{

System.out.println("Invalid input 3 times. Exiting...");

return;

}

System.out.print("Try again, enter an integer(>1): ");

num = inp.nextInt();

}

  

// process

System.out.println("Factors: ");

int factor = 2;

while (num != 1)

{

if (num % factor == 0)

{

System.out.println(factor);

num = num / factor;

}

else

{

factor = nextPrime(factor);

}

}

  

  

// ask for another

System.out.println();

System.out.print("Do you want to continue? (y/n) ");

answer = inp.next().toLowerCase();


err = 0;

while (!answer.equals("y") && !answer.equals("n") &&

!answer.equals("yes") && !answer.equals("no"))

{

err++;

System.out.println("Invalid input");

if (err == 3)

{

System.out.println("Invalid input 3 times. Exiting...");

return;

}

System.out.print("Try again, enter y, yes, n or no: ");

answer = inp.next().toLowerCase();

}

  

if (answer.equals("n") || answer.equals("no"))

{

quit = true;

}

}

System.out.println("Thanks for using the program.");


}


}