Product of Two Prime Numbers Write a method called productOfTwoPrimes, which tak
ID: 3806852 • Letter: P
Question
Product of Two Prime Numbers Write a method called productOfTwoPrimes, which takes a positive integer as input and returns true if the input is the product of exactly two prime numbers and otherwise returns false. You may assume you have access to a method whose signature is public static boolean isPrime(int x) that determines whether or not the input value, x, is a prime number. Recall that a number is prime if it is only evenly divisible by itself and 1. For this question, the numbers 0 and 1 are not considered to be prime. For example, productOfTwoPrimes(9) should return true because 9 is the product of 3 and 3. On the other hand, productOfTwoPrimes(63) should return false because it is the product of three primes (3, 3, and 7).
Second Largest int For this problem, you may not use any methods from the Java standard libraries (e.g., methods from the Math or Arrays libraries). Write a method called secondLargest, which takes an int array as input, and returns the second-largest value in the int array. You may make the following assumptions: • The input array has at least two elements. • All elements of the input array are unique (i.e., there are no duplicates). Forexample,callingthemethodonanarraycontainingthevalues{3, 7, 5, 8, 2}shouldreturn7.
Explanation / Answer
//Program to find whether the number is a product of exactly two primes or not:
package chegg.assignment.computerscience.search;
import javax.swing.JOptionPane;
public class ProductOfTwoPrimes {
public static boolean productOfTwoPrimes(int number)
{
boolean result = true;
//obtain the two numbers whose product equals the given number
double sqrtNumber = Math.sqrt(number);
int number1, number2;
int maxCheque = (int) Math.floor(sqrtNumber);
System.out.println("maxCheque = " + maxCheque);
for(int i = 2; i <= maxCheque; i++){
int modValue = number % i;
if(modValue == 0){
number1 = i;
number2 = number / number1;
System.out.println("number 1 = "+ number1 + "; number2 =" + number2);
if(!isPrime(number1) || !isPrime(number2)){
result = false;
}
}
}
return result;
}
public static boolean isPrime(int number)
{
boolean isPrimeNumber = true;
double sqrtNumber = Math.sqrt(number);
int maxCheque = (int) Math.floor(sqrtNumber);
for(int i = 2; i <= maxCheque; i++){
int modValue = number % i;
if(modValue == 0){
isPrimeNumber = false;
break;
}
}
System.out.println("number = " + number + " prime is " + isPrimeNumber);
return isPrimeNumber;
}
public static void main(String args[])
{
try{
String inputString = JOptionPane.showInputDialog("Enter the number to be identified as a product of two primes:");
int inputNumber = Integer.parseInt(inputString);
String message = "The input number " + inputNumber;
if((inputNumber == 0) || (inputNumber == 1))
{
message += " cannot be tested for product of two prime numbers";
}
else{
boolean result = ProductOfTwoPrimes.productOfTwoPrimes(inputNumber);
if(result == true){
message += " is a product of two prime numbers";
}
else{
message += " is not a product of exactly two prime numbers";
}
}
JOptionPane.showMessageDialog(null, message);
}
catch(Exception e){
e.printStackTrace();
}
}
}
//Program to find the second largest of given input arraylist of numbers:
package chegg.assignment.computerscience.search;
import java.util.ArrayList;
import javax.swing.JOptionPane;
/**
*
* @author anandan
*/
public class SecondLargest {
public static int secondLargest(ArrayList inputArray)
{
int result = 0;
int max = (int) inputArray.get(0);
result = (int) inputArray.get(1);
if(max < result){
result = max;
max = (int) inputArray.get(1);
}
for(int i = 2; i < inputArray.size(); i++){
int currentNumber = (int) inputArray.get(i);
if(max < currentNumber){
int temp = max;
max = currentNumber;
result = temp;
}
else{
if(currentNumber > result){
result = currentNumber;
}
}
}
return result;
}
public static void main(String args[])
{
int inputNumber, count = 0;
ArrayList inputArray = new ArrayList();
do{
count++;
String inputString = JOptionPane.showInputDialog("Enter the next number of input array, -1 to stop:");
inputNumber = Integer.parseInt(inputString);
if(inputNumber != -1){
inputArray.add(inputNumber);
}
}
while(inputNumber != -1);
String message = "Input array requires minimum 2 numbers";
if(count > 2)
{
message = "The given numbers are " + inputArray.get(0);
for(int i = 1; i < inputArray.size(); i++){
message += ", " + inputArray.get(i);
}
int result = SecondLargest.secondLargest(inputArray);
message += "; The second largest number in the given number is " + result;
}
JOptionPane.showMessageDialog(null, message);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.