Results 1 to 5 of 5
  1. #1
    Valerie10b is offline Member
    Join Date
    Mar 2013
    Posts
    18
    Rep Power
    0

    Default How to call a class into the main method 5 times?

    How do i call it into the main method 5 times?

    this is my class:
    Java Code:
    public class Lab6B {
    
    //
    // Problem 1
    // 
    
    public static boolean isPrime(int n) {
    
       for(int i = 2; i * i <= n; i++) {
          if(0 == n % i)
             return false;
       }
       return true;
       
    }//end


    this is my main(driver):
    Java Code:
       public static void main (String args[]){
    //
    // Problem 1
    // 
    
       Scanner scan = new Scanner(System.in);
          System.out.println ("\nThis is problem # 1\n");
          System.out.println("Enter an integer: ");
       int num = scan.nextInt();
    
       boolean is = Lab6B.isPrime( num );
          System.out.println( num + " is " + (is ? "true" : "false"));
    // end of Problem 1

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,934
    Rep Power
    4

    Default Re: How to call a class into the main method 5 times?

    I am not certain what you are asking for. Do you want to call isPrime from main five times with a new number each time?

    Also, here is a suggestion for some efficiency. Your isPrime code will work the way it is written. But each time you have to square i to see if it s less than n. Why not compute the square root of n once and then just compare i to that? You can also just check for evenness once, and then start your loop with 3 and check every other value.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    Valerie10b is offline Member
    Join Date
    Mar 2013
    Posts
    18
    Rep Power
    0

    Default Re: How to call a class into the main method 5 times?

    Quote Originally Posted by jim829 View Post
    I am not certain what you are asking for. Do you want to call isPrime from main five times with a new number each time?

    Also, here is a suggestion for some efficiency. Your isPrime code will work the way it is written. But each time you have to square i to see if it s less than n. Why not compute the square root of n once and then just compare i to that? You can also just check for evenness once, and then start your loop with 3 and check every other value.

    Regards,
    Jim
    oh okay will do!
    but yes i would like to call isPrime from main 5 times with a new number eachtime

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,934
    Rep Power
    4

    Default Re: How to call a class into the main method 5 times?

    The just put it in a loop and call it. You can generate the argument by using the Math.random function. Or look at the java.util.Random class. Since it is a static method, you don't need an instance of Lab6B to call it.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    tamilarasi is offline Member
    Join Date
    Nov 2012
    Location
    India
    Posts
    70
    Rep Power
    0

    Default Re: How to call a class into the main method 5 times?

    Quote Originally Posted by Valerie10b View Post
    How do i call it into the main method 5 times?
    you call your method like this

    Java Code:
    for(int i=0;i<5;i++){
     boolean is = Lab6B.isPrime( num );
    }
    Regards
    Android developer at Trinay Technology Solutions,http://www.trinaytech.com,5705750475

Similar Threads

  1. Include a call in the main method
    By Andyroxxx in forum New To Java
    Replies: 4
    Last Post: 03-08-2013, 09:38 AM
  2. Replies: 4
    Last Post: 05-25-2012, 03:54 PM
  3. Running main method class from another main class
    By tlrocketman in forum New To Java
    Replies: 3
    Last Post: 12-06-2010, 08:30 AM
  4. passing hashmap from a method and call it within main
    By rajuchacha007 in forum New To Java
    Replies: 2
    Last Post: 04-06-2010, 09:41 AM
  5. Replies: 1
    Last Post: 08-07-2007, 06:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •