Question

Create a method based program to find if a number is prime and then print all...

Create a method based program to find if a number is prime and then print all the prime numbers from 1 through 500

Method Name: isPrime(int num) and returns a boolean

Use for loop to capture all the prime numbers (1 through 500)

Create a file to add the list of prime numbers

Working Files:

public class IsPrimeMethod
{
   public static void main(String[] args)
   {
      String input;        // To hold keyboard input
      String message;      // Message to display
      int number;          // Number to check for prime
    
      // Get the number.
    
    
    
    
      // Determine whether it is prime or not.
      if (isPrime(number))
        // print the message the number is prime
        //enter code here
      
      
      else
      // print the message the number is not a prime
      //enter code here
      
      
         
      // Display a message.
    
      JOptionPane.showMessageDialog(null, message);
                    
      System.exit(0);
   }

   /**
      The isPrime method determines whether a number is prime.
      @param num The number to check.
      @return true if the number is prime, false otherwise.
   */

   //create a method called isPrime() that takes and integer variable and returns a boolean
   public static boolean isPrime(int num)
   {
      boolean ___________________ = false;
      int ___________ = 2;
    
      while(_________ < num && !_______________)
      {
         if ((_______ % div) == 0)
            _______________ = true;
         ____________++;
      }
    
      return _________________;
   }
}

import java.util.Scanner;
import java.io.*;

/**
   This program demonstrates a solution to the
   Prime Number List programming challenge.
*/

public class PrimeNumberList
{
   public static void main(String[] args) throws IOException
   {
      int number;          // Number to check for prime
    
      // Open a file.
      PrintWriter outFile = ___________________("PrimeList.txt");
    
      // Write the prime number list.
      for (int i = 1; i <= 100; i++)
      {
         if (______________))
            outFile.println(________);
      }
    
      // Close the file.
      outFile.close();
   }

   /**
      The isPrime method determines whether a number is prime.
      @param num The number to check.
      @return true if the number is prime, false otherwise.
   */
   public static boolean isPrime(int num)
   {
     //Create the method created in problem 1
   }
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/**********************IsPrimeMethod.java**************************/

import java.util.Scanner;

public class IsPrimeMethod

{

public static void main(String[] args)

{

String input; // To hold keyboard input

String message; // Message to display

int number; // Number to check for prime

  

// Get the number.

System.out.print("Enter the number to check for prime: ");

Scanner sc = new Scanner(System.in);

number = sc.nextInt();

  

  

// Determine whether it is prime or not.

if (isPrime(number))

// print the message the number is prime

//enter code here

System.out.print("The number is prime");

  

else

// print the message the number is not a prime

//enter code here

System.out.print("The number is not a prime");

// Display a message.

message = "Thank you.";

// JOptionPane.showMessageDialog(null, message);

  

System.exit(0);

}

/**

The isPrime method determines whether a number is prime.

@param num The number to check.

@return true if the number is prime, false otherwise.

*/

//create a method called isPrime() that takes and integer variable and returns a boolean

public static boolean isPrime(int num)

{

boolean notPrime = false;

int div= 2;

  

while(div< num && !notPrime)

{

if ((num % div) == 0)

notPrime = true;

div++;

}

  

return !notPrime;

}

}

OUTPUT:

terminated> IsPrimeMethod Java Application] C:Program FilesJ Enter the number to check for prime: 37 The number is prime

/**************************************************************************************************/

/*******PrimeNumberList.java************************/

import java.util.Scanner;

import java.io.*;

/**

This program demonstrates a solution to the

Prime Number List programming challenge.

*/

public class PrimeNumberList

{

public static void main(String[] args) throws IOException

{

int number; // Number to check for prime

  

// Open a file.

PrintWriter outFile = new PrintWriter("PrimeList.txt");

  

// Write the prime number list.

for (int i = 1; i <= 500; i++)

{

if (isPrime(i))

outFile.println(i);

}

  

// Close the file.

outFile.close();

}

/**

The isPrime method determines whether a number is prime.

@param num The number to check.

@return true if the number is prime, false otherwise.

*/

public static boolean isPrime(int num)

{

boolean notPrime = false;

int div= 2;

  

while(div< num && !notPrime)

{

if ((num % div) == 0)

notPrime = true;

div++;

}

  

return !notPrime;

}

}

/*********************OUTPUT FILE**************************/

PrimeList - Notepad File Edit Format View Help 2 7 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 10

13791379391173917-337137-77939739397|99-1393 9999|22233455667788901113344556778890012333 111122222222222222223333333333333333

39397|9911393971379719 778890012333 333334444444 56667899

/*If this helps you, please let me know by giving a positive thumbs up. In case you have any queries, do let me know. I will revert back to you. Thank you!!*/

Add a comment
Know the answer?
Add Answer to:
Create a method based program to find if a number is prime and then print all...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I am given an input file, P1input.txt and I have to write code to find the...

    I am given an input file, P1input.txt and I have to write code to find the min and max, as well as prime and perfect numbers from the input file. P1input.txt contains a hundred integers. Why doesn't my code compile properly to show me all the numbers? It just stops and displays usage: C:\> java Project1 P1input.txt 1 30 import java.io.*; // BufferedReader import java.util.*; // Scanner to read from a text file public class Project1 {    public static...

  • Modify the library program as follows: Create a method in the LibraryMaterial class that accepts a...

    Modify the library program as follows: Create a method in the LibraryMaterial class that accepts a string s as a parameter and returns true if the title of the string is equal to s. Create the same method for your library material copies. Note that it will need to be abstract in the LibraryMaterialCopy class MY CODE **************************************************************** LibraryCard: import java.util.List; import java.util.ArrayList; import java.time.LocalDate; import    java.time.temporal.ChronoUnit; public class LibraryCard {    private String id;    private String cardholderName;   ...

  • Make a FLOWCHART for the following JAVA Prime Number Guessing Game. import java.util.Random; import java.util.Scanner; public...

    Make a FLOWCHART for the following JAVA Prime Number Guessing Game. import java.util.Random; import java.util.Scanner; public class Project2 { //Creating an random class object static Random r = new Random(); public static void main(String[] args) {    char compAns,userAns,ans; int cntUser=0,cntComp=0; /* * Creating an Scanner class object which is used to get the inputs * entered by the user */ Scanner sc = new Scanner(System.in);       System.out.println("*************************************"); System.out.println("Prime Number Guessing Game"); System.out.println("Y = Yes , N = No...

  • Here is the assignment: Fibonacci or Prime number iterator: Design a menu driven program that performs...

    Here is the assignment: Fibonacci or Prime number iterator: Design a menu driven program that performs the user’s choice of the following functions the program exits should also be a user’s choice. Menu Item 1: Fibonacci number iterator, here you are to define an iterator class named FibonacciIterator for iterating Fibonacci numbers. The constructor takes an argument that specifies the limit of the maximum Fibonacci number. For example, prompt the user for size, use the size to call FibonacciIterator(“user input”)...

  • Create a DataEntryException class whose getMessage() method returns information about invalid integer data. Write a program...

    Create a DataEntryException class whose getMessage() method returns information about invalid integer data. Write a program named GetIDAndAge that continually prompts the user for an ID number and an age until a terminal 0 is entered for both. If the ID and age are both valid, display the message ID and Age OK. Throw a DataEntryException if the ID is not in the range of valid ID numbers (0 through 999), or if the age is not in the range...

  • Java: Create the skeleton. Create a new file called ‘BasicJava4.java’ Create a class in the file...

    Java: Create the skeleton. Create a new file called ‘BasicJava4.java’ Create a class in the file with the appropriate name. public class BasicJava4 { Add four methods to the class that return a default value. public static boolean isAlphabetic(char aChar) public static int round(double num) public static boolean useSameChars(String str1, String str2) public static int reverse(int num) Implement the methods. public static boolean isAlphabetic(char aChar): Returns true if the argument is an alphabetic character, return false otherwise. Do NOT use...

  • JAVA HELP: Directions Write a program that will create an array of random numbers and output...

    JAVA HELP: Directions Write a program that will create an array of random numbers and output the values. Then output the values in the array backwards. Here is my code, I am having a problem with the second method. import java.util.Scanner; import java.util.Random; public class ArrayBackwards { public static void main(String[] args) { genrate(); print(); } public static void generate() { Scanner scanner = new Scanner(System.in);    System.out.println("Seed:"); int seed = scanner.nextInt();    System.out.println("Length"); int length = scanner.nextInt(); Random random...

  • How can I change the following program and Implement a print() method for Percolation that prints...

    How can I change the following program and Implement a print() method for Percolation that prints 1 for blocked site, 0 for open sites and * for full sites? public class Visualizeheimadaemi { public static void main(String[] args) { int N = Integer.parseInt(args[0]); double p = Double.parseDouble(args[1]); int M = Integer.parseInt(args[2]); // repeatedly created N-by-N matrices and display them using standard draw for (int i = 0; i < M; i++) { boolean[][] open = Percolation.random(N, p); StdDraw.clear(); StdDraw.setPenColor(StdDraw.BLACK); Percolation.show(open,...

  • I am unsure how to add the following methods onto this code?? please help - rowValuesIncrease(int[][]...

    I am unsure how to add the following methods onto this code?? please help - rowValuesIncrease(int[][] t) A method that returns true if from left to right in any row, the integers are increasing, otherwise false. - columnValuesIncrease(int[][] t) A method that returns true if from top to bottom in any column, the integers are increasing, otherwise false. - isSetOf1toN(int[][] t) A method that returns true if the set of integers used is {1, 2, . . . , n}...

  • Homework Assignment on Mimir: Read in a file "numbers.txt" into a Java program. Sum up all...

    Homework Assignment on Mimir: Read in a file "numbers.txt" into a Java program. Sum up all the even numbers over 50. Print the result to the console using System.out.println(); The file will only have numbers. Code: import java.io.FileNotFoundException; import java.io.FileReader; import java.util.Scanner; /** * * @author David */ public class ReadingFiles {     /**      * @param args the command line arguments      */     public static void main(String[] args) throws FileNotFoundException {         // TODO code application logic here...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT