import java.util.Scanner; public class HW4 { public static String pFactor(int num){ //method signature. takes long, returns string of factorization String ans=""; //creates the answer for(int i=2;i<=num ;i++){ //loops from 2 to the num if(num%i==0){ //checks if i is a divisor of num ans+=i+" * "; //writes i in prime factorization num=num/i; //since it is written down, num=num/i i--; //just in case their are multiple factors of same number. For example, 12=2*2*3 } } return(ans.substring(0,ans.length()-2)); //takes away last asterisk. Factorization of 4=2*2*=>2*2 } public static long largePrime(long num) { // largest composite factor must be smaller than sqrt long sqrt = (long)Math.ceil(Math.sqrt((double)num)); long largest = -1; for(long i = 2; i <= sqrt; i++) { if(num % i == 0) { long test = largePrime(num/i); if(test > largest) { largest = test; } } } if(largest != -1) { return largest; } // number is prime return num; } public static void main(String[] args) { System.out.print("Please enter a positive integer: "); Scanner scan = new Scanner(System.in); String str = scan.next(); char ch = str.charAt(0); while (ch != 'q') { int num=0; try { num = Integer.parseInt(str); if (num < 0) { System.out.println("Negative number."); } else { int i; //int pFactorAns = pFactor(0); boolean inside = false; for (i=2; i < num; i++) { if (num % i == 0) { inside = true; // this means num is composite int largestDivisor = num / i; System.out.println("COMPOSITE "); System.out.println("Largest number that divides " + num + " evenly is " + largestDivisor); long large; large = largePrime(num); System.out.println("Largest prime number that divides " + num + " evenly is " + large); String answer; answer = pFactor(num); System.out.println("Prime factors = " + answer); int small = num / largestDivisor; System.out.println("Largest integer factor set = " + small + " * " + largestDivisor); break; } } if (inside == false) { // if (!inside) // this means num is prime System.out.println ("PRIME"); } } } catch (NumberFormatException nfe) { System.out.println(str + " is not an integer."); } System.out.print("Please enter a positive integer: "); str = scan.next(); ch = str.charAt(0); } Return(); } private static void Return() { System.out.println("Thank you for using my program. "); } }

## 3 Replies - 4229 Views - Last Post: 14 June 2013 - 07:06 AM

### #1

# Determine Prime/Composite numbers - KEY: use methods (3 besides main)

Posted 09 June 2013 - 12:28 PM

This was a homework assignment that I finished and turned in 2 weeks ago. I think everything works (I haven't found one that hasn't) but the point was to have 3 methods other than the main method and I couldn't get a 3rd method to work for anything. I tried doing multiple things. I just wanted to know for the sake of more practice with methods - how anyone would do a third one and call it in. I am in the basic java class so I haven't worked with anything fancy - like the scanner is the only import I have used. I have only done very basic stuff. I promise this assignment is turned in already - but my teacher doesn't have a solution and I really wanted to know "the correct" way to do it - although really there isn't a correct way per say. Can anyone suggest how to do a 3rd method? Thank you for any help and suggestions you have. Also, my teacher helped me with the Try/catch but I don't really understand that cause we did't really discuss it in class. What is the point of using that? Is it similar to a while loop kinda?

##
**Replies To:** Determine Prime/Composite numbers - KEY: use methods (3 besides main)

### #2

## Re: Determine Prime/Composite numbers - KEY: use methods (3 besides main)

Posted 12 June 2013 - 11:50 PM

Someone please help - I have my midterm tomorrow and I really wanted to understand how to do Java better before then. I don't understand the inheritance stuff - and some of the methods. I really needed some help. My teacher has not been tremendously helpful.

### #4

## Re: Determine Prime/Composite numbers - KEY: use methods (3 besides main)

Posted 14 June 2013 - 07:06 AM

Aer1ekah, on 09 June 2013 - 02:28 PM, said:

This was a homework assignment that I finished and turned in 2 weeks ago. I think everything works (I haven't found one that hasn't) but the point was to have 3 methods other than the main method and I couldn't get a 3rd method to work for anything. I tried doing multiple things. I just wanted to know for the sake of more practice with methods - how anyone would do a third one and call it in. I am in the basic java class so I haven't worked with anything fancy - like the scanner is the only import I have used. I have only done very basic stuff. I promise this assignment is turned in already - but my teacher doesn't have a solution and I really wanted to know "the correct" way to do it - although really there isn't a correct way per say. Can anyone suggest how to do a 3rd method?

Your question is not clear to me. What is it you're asking for? What do you want your third method to do?

Quote

Also, my teacher helped me with the Try/catch but I don't really understand that cause we did't really discuss it in class. What is the point of using that? Is it similar to a while loop kinda?

No, it's not.

try { num = Integer.parseInt(str); } catch (NumberFormatException nfe) { System.out.println(str + " is not an integer."); }

Try/catch is java's exception-handling mechanism. When you run a method that declares that it throws an exception, you wrap it in a try block, and you handle that exception in a catch block. Here, the possible Exception is a NumberFormatException - this is what happens when you call parseInt with a String that doesn't represent an int. For example, Integer.parseInt("ham and eggs"}; would throw this exception. If this happens in your example, java will execute the code in the catch block - which should ideally help the user recover from the exceptional situation.

A try block should really encompass as little code as possible. In this case, it should cover just the call to parseInt. Once you've done that, you should be out of the try block.

public static long largePrime(long num) { // largest composite factor must be smaller than sqrt

This comment is not true. sqrt(40) = 6.3 something. Largest composite factor of 40 = 20.

Page 1 of 1