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
}
}
/**********************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:
/**************************************************************************************************/
/*******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**************************/
/*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!!*/
Create a method based program to find if a number is prime and then print all...
Write a method that determines if a given non-negative integer is a prime number, use the following method header: public static boolean isprime(int n) Prime numbers are integers that are divisible only by 1 and themselves; for example, 2, 5, 7, and 13 are prime numbers, while 15, 16, and 20 are not. By definition, 0 and 1 are not prime numbers. To test your method, write a simple program that asks the user to enter a number and then...
Java BNF How to write pseudocode for this snippet? Java code for finding a prime number public class Prime { public static void main(String[] args) { int num = 29; boolean flag = false; for(int i = 2; i <= num/2; ++i) { // condition for nonprime number if(num % i == 0) { flag = true; break; } } if (!flag) System.out.println(num + " is a prime number."); else System.out.println(num + " is not a prime number."); } }
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...
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...
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...
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”)...
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; ...
import java.util.Scanner; public class SieveOfEratosthenes { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter a number"); int num = sc.nextInt(); boolean[] bool = new boolean[num]; for (int i = 0; i< bool.length; i++) { bool[i] = true; } for (int i = 2; i< Math.sqrt(num); i++) { if(bool[i] == true) { for(int j = (i*i); j<num; j = j+i) { bool[j] = false;...
Hey all, if you could create a java program following these guidelines that would be much appreciated. helpful comments in the program would be appreciated :) The idea of this program is as followes : A password must meet special requirements, for instance , it has to be of specific length, contains at least 2 capital letters , 2 lowercase letters, 2 symbols and 2 digits. A customer is hiring you to create a class that can be utilized for...
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}...