Call methods in main method

Posted by Niloo on Stack Overflow See other posts from Stack Overflow or by Niloo
Published on 2012-10-18T04:41:05Z Indexed on 2012/10/18 5:02 UTC
Read the original article Hit count: 382

Filed under:
|
|

this is my main method that gets 3 integers from command line and I parse then in my validating method.

However I have one operation method that calls 3 other methods, but i don't know what type of data and howmany I have to put in my operatinMethod() " cuase switch only gets one); AND also in my mainMethod() for calling the operationMehod(); itself?

please let me know if i'm not clear? Thanx!

main method:

public class test {

// Global Constants
final static int MIN_NUMBER = 1;
final static int MAX_PRIME = 10000;
final static int MAX_FACTORIAL = 12;
final static int MAX_LEAPYEAR = 4000;

//Global Variables
static int a,b,c;

public static void main (String[] args) {

    for(int i =0; i< args.length; i++){}
        if(validateInput(args[0],args[1],args[2])){
            performOperations();


}

}

//Validate User Input
public static boolean validateInput(String num1,String num2,String num3){
    boolean isValid = false;

    try{
        try{
            try{
        a = Integer.parseInt(num1);
            if(!withinRange(a,MIN_NUMBER, MAX_PRIME)) {
                 System.out.println("The entered value " + num1 +" is out of range [1 TO 10000].");
            }
            isValid = true;         
            }
        catch(Exception ex) { 
             System.out.println("The entered value " + num1 + " is not a valid integer.  Please try again.");
        }   

        b = Integer.parseInt(num2);
            if(!withinRange(b,MIN_NUMBER, MAX_FACTORIAL)) {
                System.out.println("The entered value " + num2 +" is out of range [1 TO 12].");
            }
            isValid = true; 
            }
        catch(Exception ex) { 
            System.out.println("The entered value " + num2 + " is not a valid integer.  Please try again.");
        }

        c = Integer.parseInt(num3);
            if(!withinRange(c,MIN_NUMBER, MAX_LEAPYEAR)) {
                System.out.println("The entered value " + num3 +" is out of range [1 TO 4000].");
            }
            isValid = true; 
            }
        catch(Exception ex) { 
            System.out.println("The entered value " + num3 + " is not a valid integer.  Please try again.");
        }   

        return isValid;     
}

//Check the value within the specified range
private static boolean withinRange(int userInput ,int min, int max){
    boolean isInRange = true; 
        if(userInput < min || userInput > max){
          isInRange = false;
        }
     return isInRange;
}

//Perform operations
    private static void performOperations(int userInput) {
        switch(userInput) {
      case 1: // count Prime numbers
          countPrimes(a);
          break;
      case 2: // Calculate factorial
          getFactorial(b);
          break;

      case 3: // find Leap year
          isLeapYear(c);
          break;

       } 
    }


// Verify Prime Number
    private static boolean isPrime(int prime) {
        for(int i = 2; i <= Math.sqrt(prime) ; i++) {
            if ((prime % i) == 0) {
                return false;
            }
        }
        return true;
     }


// Calculate Prime
    private static int countPrimes(int userInput){
        int count =0;
            for(int i=userInput; i<=MAX_PRIME; i++) {
                if(isPrime(i)){
                    count++;
                }
            }
    System.out.println("Exactly "+  count + " prime numbers exist between "+ a + " and 10,000.");
        return count;   
    }


// Calculate the factorial value
    private static int getFactorial(int userInput){
        int ans = userInput;
        if(userInput >1 ){

        ans*= (getFactorial(userInput-1));

        //System.out.println("The value of "+ b +"! is "+ getFactorial(userInput));
        }
        return ans;

    }


// Determine whether the integer represents a leap year
    private static boolean isLeapYear(int userInput){
        if (userInput % 4 == 0 && userInput % 400 == 0 && userInput % 100 ==0){
            System.out.println("The year "+ c +" is a leap year"); 
        }
        else {
            System.out.println("The year "+ c +"  is a not leap year"); 
    }
        return false;
}





}

© Stack Overflow or respective owner

Related posts about java

Related posts about methods