Given program for debugging: import java.util.Scanner; /* Filename: Debug Java.java * This program does not work...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
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 <= value; divisor++) { if (value divisor == 0) { return false; } /** * Reads a positive integer from the user and returns it. * * @return A positive integer entered by the user. private int readPositiveInt() { Scanner input = new Scanner(System.in); /* Get an initial value. */ System.out.println("Enter a positive integer: "); int value = input.nextInt(); /* If the value was nonpositive, reprompt the user. */ while (value <= 0) { 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) 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 <= value; divisor++) { if (value divisor == 0) { return false; } /** * Reads a positive integer from the user and returns it. * * @return A positive integer entered by the user. private int readPositiveInt() { Scanner input = new Scanner(System.in); /* Get an initial value. */ System.out.println("Enter a positive integer: "); int value = input.nextInt(); /* If the value was nonpositive, reprompt the user. */ while (value <= 0) { 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)
Expert Answer:
Answer rating: 100% (QA)
Solutions Step 1 Explanation To solve this problem you can implement the solution function as follow... View the full answer
Related Book For
Management Accounting Information for Decision-Making and Strategy Execution
ISBN: 978-0137024971
6th Edition
Authors: Anthony A. Atkinson, Robert S. Kaplan, Ella Mae Matsumura, S. Mark Young
Posted Date:
Students also viewed these programming questions
-
Design a Java class that represents a cache with a fixed size. It should support operations like add, retrieve, and remove, and it should evict the least recently used item when it reaches capacity.
-
Planning is one of the most important management functions in any business. A front office managers first step in planning should involve determine the departments goals. Planning also includes...
-
For some companies, onboarding and orientation are synonymous. How are they different?
-
What kind of plot is useful for deciding whether finding a regression line for a set of data points is reasonable?
-
Find the slope of the tangent line to the graph at the given point. Bifolium: (x + y) = 4xy y -2 2 - 1 -2- (1, 1) + 12 X
-
In 2001, the City of New York and the Dormitory Authority of the State of New York (DASNY) entered into an agreement to build a forensic biology laboratory in Manhattan. Per the agreement, DASNY...
-
Meadowlands Design produces head covers for golf clubs. The company expects to generate a profit next year. It anticipates fixed manufacturing costs of $200,500 and fixed general and administrative...
-
Information (feedback) from customers that use a large number of Charm Financial Bank's products or earn significantly more money annually than other customers may be useful, but particular attention...
-
Consider an abatement cost function C(e,)=(abe) 2 /2b, where is the amount of private investment to reduce abatement costs. There are J identical firms. The social damage function is D(E)=dE 2 /2....
-
chose congect anscwer The hypeg hypersensitivity expressed oith the blood ells is Aype! lysing t typelI IgE molecules invdued in hypersen, Ron reaction have become aftached red to neutrophils Mast...
-
Why is empowerment so important in engaging employees to support, enable, and facilitate change? List and explain five (5) things a good leader would do to ensure effective empowerment.
-
Exercises 1. Suppose you have European call on a stock with: *Current stock price is $50 *There is one period to go *Stock price will either go up to $75 or go down to $25 *There are no cash...
-
show answers and formula for how to get answer JTM Airlines Airport Expansion Start Phase I Phase II Phase III Success 55% PV of Revenues 300 Costs Expected Net Probability Value 25 Start 32 Success...
-
PROBLEM 2 A and B, calendar year individuals, are equal partners in a real estate partnership. In the current year, the partnership had gross rental income of $250 and deductions, including...
-
1. Why was this launch successful? What made it successful? Was it Beyonc? Was it the novelty of the medium (social media)? Was it the buzz that came after the announcement? Something else? I.e.,...
-
You are a tax adviser and you and your firm are members of a professional accountancy body. Your firm provides tax advice to HPT Ltd. You have discovered that HPT Ltd has not notified HMRC about...
-
C- Consider the following scenario:- A supermarket needs to develop the following software to encourage regular customers. For this, the customer needs to supply his/her residence address, telephone...
-
How do the scientific management, human relations, and human resource schools differ in their views on human motivation?
-
Provide three reasons why customer loyalty provides benefits to companies.
-
The Sudbury, South Carolina, plant of Saldanha Sports Company has the following standards for its soccer ball production: RequiredDetermine the following variances for October:(a) Total direct...
-
Which of the following statements is true? a. Money is a commodity as well as a medium of exchange in a conventional mode of financing. b. Money is not a commodity and is a medium of exchange and...
-
The standards-setting body of the IFRS Foundation is a. International Accounting Standards Board (IASB) b. IFRS Foundation Monitoring Board c. IFRS Interpretations Committee d. IFRS Foundation...
-
In general, financial accounting can be broken down into the following steps: a. Identifying and recording economic transactions b. Recording and communicating economic transactions c. Identifying,...
Study smarter with the SolutionInn App