Question: Given program for debugging: import java.util.Scanner; /* Filename: Debug Java.java * This program does not work as intended. It contains both * compile-time errors




Given program for debugging: import java.util.Scanner; /* Filename: Debug Java.java * This program does not work as intended. It contains both * compile-time errors (errors that prevent the compiler from even running the program) and run-time errors (errors where the program does not function as intended). Your job is to fix * this program so that it works correctly. Note that it is *not* * sufficient to simply fix the compiler errors; you will need to * update the logic as well. * This program attempts to read a positive integer from the user, then check whether that integer is prime (whether it's only * divisors are 1 and itself). If so, it prints a message saying *that the number is prime; otherwise it says that the number is * composite. public class Debug Java ( /* Reads a number from the user and reports whether or not it * is prime. } } System.out.println("Enter a positive integer: "); int value = input.nextInt(); return value; /** * Call isPrime () method multiple times to test it against common prime numbers and composite * @return no return value. *1 private void testIsPrime () { 193, 197, 199, 211, 263, 269, 271, 277, 281, 283, 293, 307, 337, 347, 349, 353, 359, 367, 373, 379, 409, 419, 421, 431, 433, 439, 443, 449, 479, 487, 491, 499, 503, int[] primelk = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 223, 227, 229, 233, 239, 241, 251, 257, 311, 313, 317, 331, 383, 389, 397, 401, 457, 461, 463, 467, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997}; 613, 617, 619, int[] composite100 = (4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 1001; for (int primeNum: primelk) { if (!isPrime (primeNum)) { System.out.println("PRIME ERROR: "+ prime Num); break; } else { System.out.println("CORRECT PRIME: "+ primeNum); } for (int composite Num: composite100) { if (isPrime (compositeNum) ) { System.out.println("COMPOSITE ERROR: "+ compositeNum); public void run() { /* Get the value from the user. */ int value = readPositiveInt(); /* Check whether or not it is prime. */ if (isPrime(value)) { System.out.println(value + " is prime.") } else { } " System.out.println (value + is composite."); testIsPrime(); } /** * Given a positive integer, returns whether that integer is * prime. * @param value The value to test. @return whether or not it is prime. private boolean isPrime (int value) { /* Try all possible divisors of the number. If any of them * cleanly divide the number, we return that the number is composite. * for (int divisor = 0; divisor 2. Reading Input and Debugging in Java In this exercise you are tasked to debug an erroneous code. The given program reads in a positive integer from the user and then checks whether that number is prime; that is, whether it has any divisors other than 1 and itself. For example, 3 is prime and 31 is prime, 67 but 54 is not (it's divisible by two) and 49 is not (it's divisible by seven). Unfortunately, the provided program has many problems: it doesn't compile due to syntax errors, and the logic itself is incorrect. Your task is to: Fix the program so that it compiles, and Correct the logic errors so that it works correctly. Note: It is not enough to just get the program to compile! You also need to find and correct any logic errors you encounter. Be sure to test your final program to make sure that it works correctly. Use the following code snippet to test your isPrime() method: Note: A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. A natural number greater than 1 that is not prime is called a composite number. ( https://en.wikipedia.org/wiki/Prime number)
Step by Step Solution
3.36 Rating (165 Votes )
There are 3 Steps involved in it
Solutions Step 1 Explanation To solve this problem you can implement the solution function as follow... View full answer
Get step-by-step solutions from verified subject matter experts
