Listing 6.7, PrimeNumberMethod.java, provides the isPrime(int number) method for testing whether a number is prime. Use this
Question:
Listing 6.7, PrimeNumberMethod.java, provides the isPrime(int number) method for testing whether a number is prime. Use this method to find the number of prime numbers less than 10000.
Transcribed Image Text:
Listing 1 public class PrimeNumberMethod { public static void main(String[] args) { System.out.printIn("The first 50 prime numbers are \n"); printPrimeNumbers(50); 2 public static void printPrimeNumbers(int numberofPrimes) { final int NUMBER_OF_PRIMES_PER_LINE - 10; // Display 10 per line int count - 0; /ī Count the number of prime numbers int number - 2; // A number to be tested for primeness 10 11 12 13 14 // Repeatedly find prime numbers while (count < numberofPrimes) { // Print the prime number and increase the count if (isPrime(number)) { count++; // Increase the count 15 16 17 if (count % NUMBER_OF_PRIMES_PER_LINE -- 0) { // Print the number and advance to the new 1line System.out.printf("%-5s\n", number); 18 19 20 21 22 23 else System.out.printf("X-5s", number); 24 25 26 27 // Check whether the next number is prime number++; 28 29 30 31 /** Check whether number is prime */ public static boolean isPrime (int number) { for (int divisor - 2; divisor <- number / 2; divisor++) { if (number % divisor -- 0) { // If true, number is not prime return false; // Number is not a prime 32 33 34 35 36 37 38 39 40 41 } isPrime method return true; // Number is prime The first 50 prime numbers are 11 47 97 139 181 191 193 13 17 59 19 23 29 43 89 31 73 127 37 79 131 137 41 61 67 101 103 107 71 53 109 113 149 151 157 163 167 173 83 179 197 199 211 223 227 229
Fantastic news! We've Found the answer you've been seeking!
Step by Step Answer:
Answer rating: 50% (4 reviews)
Program plan Step 1 Create a test class called PrimeNumbersDem...View the full answer
Answered By
Sana Rehman
I have been working on fiverr from more than 3+ years as an Academic Writer and Tutor. As far as my studies are concerned, I have done Bachelors of Software Engineering. On Professional front, I am working a Software Engineer.
0.00
0 Reviews
10+ Question Solved
Related Book For
Introduction to Java Programming, Comprehensive Version
ISBN: 978-0133761313
10th Edition
Authors: Y. Daniel Liang
Question Posted:
Students also viewed these Computer science questions
-
Give an algorithm for testing whether a relation scheme is in BCNF. The algorithm should be polynomial in the size of the set of given FDs. (The size is the sum over all FDs of the number of...
-
Programming Exercise 6.39 gives a method for testing whether three points are on the same line.Write the following method to test whether all the points in the array points are on the same...
-
a. Carlys Catering provides meals for parties and special events. In Chapter 2, you wrote an application that prompts the user for the number of guests attending an event, displays the company motto...
-
Consider J symmetric firms operating in two periods with constant over time cost functions C j (x j , e j ) = (x j b j e j ) 2 /2b j , +c j x 2 j /2 The output prices are exogenous and given by p 1...
-
Use the bubble sort to sort 3, 1, 5, 7, 4, showing the lists obtained at each step.
-
A(n) _____ exists between two entities when each row in the first entity may match many rows in the second entity, and each row in the second entity matches only one row in the first entity. a....
-
In 1970, Rose Mary Knick purchased 90 acres of land in Scott Township, Lackawanna County, Pennsylvania. In 2008, another resident of Scott Township discovered documents that suggested that one of...
-
Suppose that new entry decreased your demand elasticity from 2 to 3 (made demand more elastic). By how much should you adjust your price of $10?
-
In a random walk suppose that at t = (n - 1)At the particle is located at x = max. The assumption is that at t = nAt the particle will have moved to x = (m+1)Ax with probability 3/4 or to x = (m-3)Ax...
-
BassiCorporation had a beginning and ending fixed asset balance of $400 and $650 respectively. During the year its net capital spending was $330. Given this information, determine the company's...
-
Write a method that prints characters using the following header: public static void printChars(char ch1, char ch2, int numberPerLine) This method prints the characters between ch1 and ch2 with the...
-
Write a class that contains the following two methods:/** Convert from feet to meters */public static double?footToMeter(double?foot)/** Convert from meters to feet */public static...
-
Consider the system shown in Fig. P2.6: (i) In the absence of derivative feedback \((a=0)\) determine the damping factor and natural frequency. Also determine the steady state error resulting from a...
-
Here is a sensitivity table from Canaccord's valuation of Dreamscape Networks: Sensitivity (EV) 0.3 2.0% 2.5% 3.0% 3.5% 4.0% Source: Company Reports, Canaccord Genuity estimates Select one: Discount...
-
Instead of a single tester, you have been given a team of three testers. What adjustment should you make to the activities that the testers will work on?
-
The Optima Mutual Fund has an expected return of 18.6% and a volatility of 20.9%. Optima claims that no other portfolio offers a higher Sharpe ratio. Suppose this claim is true, and the risk-free...
-
What is the resistance of a 1.1-m-long copper wire that is 0.50 mm in diameter? What is the resistance of a 15-cm-long piece of iron with a 1.5 mm 1.5 mm square cross section?
-
Write in simplest radical form: 9 1 17 8 25. p 26. 2733.53
-
The following are three situations in which the auditor is required to develop an audit strategy. 1. The client has inventory at approximately 50 locations in four States. The inventory is difficult...
-
Create a data model for one of the processes in the end-of-chapter Exercises for Chapter 4. Explain how you would balance the data model and process model.
-
Which assignment is correct in Java? a. double money = 12; b. double money = 12.0; c. double money = 12.0d; d. All of the above are correct.
-
Which assignment is correct in Java? a. char aChar = 5.5; b. char aChar = W; c. char aChar = '*'; d. Two of these are correct.
-
What is the numeric value of each of the following expressions as evaluated by Java? a. 4 + 6 * 2 b. 10 / 5 + 8 c. 12 / 4 + 16 / 2 d. 17 / 2 e. 22 / 5 f. 39 / 10 g. 19 % (2 + 3) h. 3 + 4 * 20 / 3 i....
-
Write the Decomposition for this problem. Also include a listing of inputs and outputs. Finally, list the condition of when the loop will end and how you will determine what prints out to the user....
-
Insurance business operations are in two components, i.e. technical business and non-technical business. Briefly explain these two components and define the technical risk to which the insurance...
-
1 3 If f(x) = - 4x + 12x - 5 and the domain is the set of all x such that 0 x 9, then the absolute maximum value of the function f occurs when x is
Study smarter with the SolutionInn App