Question

In: Computer Science

Java - Firstly, write a method, using the following header, that counts the number of prime...

Java - Firstly, write a method, using the following header, that counts the number of prime numbers between from and to (inclusive)

. public static int countPrimes(int from, int to

) For example, countPrimes(11,19) returns 4 since 11, 13, 17 and 19 are primes. You can assume that someone has already written the following function to determine is an integer is prime or not.

public static boolean isPrime(int i)

// returns true if i is a prime number Secondly,

write a program that prompts the user to enter in two integers and then displays the number of prime numbers between the two integers by using the method you wrote above.

Solutions

Expert Solution

Source Code in text format (See below images of code for indentation):

import java.util.*;

/*class definition*/

public class primeCount

{

    /*main method*/

    public static void main(String[] args)

    {

        /*Scanner class to read input from the user*/

        Scanner read=new Scanner(System.in);

        /*variables*/

        int count,from,to;

        /*read two numbers from user*/

        System.out.print("Enter starting number: ");

        from=read.nextInt();

        System.out.print("Enter ending number: ");

        to=read.nextInt();

        /*method call*/

        count=countPrimes(from,to);

        /*print count*/

        System.out.println("The number of primes between "+from+" - "+to+" is: "+count);

    }

    /*method definition*/

    public static int countPrimes(int from, int to)

    {

        int count=0;

        /*using loop count prime numebers*/

        for(int i=from;i<=to;i++)

        {

            /*check for prime using method call and increment count*/

            if(isPrime(i))

                count++;

        }

        /*return count*/

        return count;

    }

    public static boolean isPrime(int i)

    {

        int k;

        /*variable to check prime or not*/

        boolean check=true;

        if(i==0||i==1)

            check=false;

        for(k=2;k<i;k++)

        {

            /*check for divisors*/

            if(i%k==0)

            {

                check=false;

                break;

            }

        }

        /*return check*/

        return check;

    }

}

Source Code:

Output:


Related Solutions

Java - Firstly, write a method, using the following header, that counts the number of prime...
Java - Firstly, write a method, using the following header, that counts the number of prime numbers between from and to (inclusive). public static int countPrimes(int from, int to) For example, countPrimes(11,19) returns 4 since 11, 13, 17 and 19 are primes. You can assume that someone has already written the following function to determine is an integer is prime or not. public static boolean isPrime(int i) // returns true if i is a prime number Secondly, write a program...
using Dr java Objective: Write a program that takes a phrase and then counts the number...
using Dr java Objective: Write a program that takes a phrase and then counts the number of vowels (case does not matter) in the phrase. It then should display all of the vowels in sorted ascending order according to their count. Only consider {AEIOU} as the vowels. Hint: It may be a good idea to keep track and sort two arrays: Has the vowels in alphabetic order Has the number of said vowels Whenever one would swap then it swaps...
Write a method in JAVA that does this: Presents the user with a header stating this...
Write a method in JAVA that does this: Presents the user with a header stating this is for assignment: Lab, and the names Bob and Bill Present the user with a menu to run a random method(just make this a dummy method) , another random method method,(just make this a dummy method) or another dummy method (just make this a dummy method) Repeat the menu until the user enters -1.
Data Structure in Java The following java method counts how many triples of integers in an...
Data Structure in Java The following java method counts how many triples of integers in an array of n distinct integers sum to zero. public static int count(int[] a) { int n = a.length; int count = 0; for (int i = 0; i < n; i++) { for (int j = i+1; j < n; j++) { for (int k = j+1; k < n; k++) { if (a[i] + a[j] + a[k] == 0) count++; } } }...
JAVA Given the header of a method public static void m1 (int[ ] max) Write down...
JAVA Given the header of a method public static void m1 (int[ ] max) Write down Java codes to invoke m1 method, declare variables as needed, (Do NOT implement the method)
In Java: Write a complete main method that does the following: 1. Takes any number, but...
In Java: Write a complete main method that does the following: 1. Takes any number, but at least two, command line arguments which are words (represented as strings) and will print to the console the number of words of that end with a digit. (Hint: loop through the args array) 2. If there are not at least two command line arguments, throw an IllegalArgumentException with an appropriate message.
Java program Prime Numbers A prime number is a natural number which has exactly two distinct...
Java program Prime Numbers A prime number is a natural number which has exactly two distinct natural number divisors: 1 and itself. For example, the first four prime numbers are: 2, 3, 5 and 7. Write a java program which reads a list of N integers and prints the number of prime numbers in the list. Input: The first line contains an integer N, the number of elements in the list. N numbers are given in the following lines. Output:...
PLEASE USE MEHODES (Print part of the string) Write a method with the following header that...
PLEASE USE MEHODES (Print part of the string) Write a method with the following header that returns a partial string: public static String getPartOfString(int n, String firstOrLast, String inWord) Write a test program that uses this method to display the first or last number of characters of a string provided by the user. The program should output an error if the number requested is larger than the string. SAMPLE RUN #1: java PartOfString Enter a string: abracadabra↵ Enter the number...
Using a Java. 2. Write a Java program calculate_fare.java to take the input for number of...
Using a Java. 2. Write a Java program calculate_fare.java to take the input for number of miles, and the class of journey (1,2, or 3, for first, second, and third class respectively), for a train journey. The program should then calculate and display the fare of journey based on the following criteria: Note: Use Switch...case and if...else construct First (1) Class Second (1) Class Third (3) Class First 100 mile $ 3 per mile $ 2 per mile $ 1.50...
Write a method with the following header: public static char calculateLetterGrade(int grade, int totalMarks) This method...
Write a method with the following header: public static char calculateLetterGrade(int grade, int totalMarks) This method calculates grade/totalMarks and returns a letter grade based on the following scale: 0-50 = F 51-60 = D 61-70 = C 71-80 = B 81-100 = A
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT