www.webdeveloper.com
Results 1 to 9 of 9

Thread: Even + method

  1. #1
    Join Date
    Oct 2004
    Posts
    47

    Even + method

    Good Afternoon all ...

    Ive beeen trying this question out for 2 days now but no improvment i might be losing it or something ... can someone help

    1- Create the class Even. It should contain two methods:

    isEven: The method should take an integer argument and return true if the integer in even and false otherwise. The method should use the reminder operator to determine if the integer is even or not.

    The main method should ask the user to input a number and it will call the isEven method to check if it is even or not. If the number is even the maid method will display "YOU ENTERED AN EVEN NUMBER" If the number is odd the mmain method should continue to ask the user to input numbers until he enters "0".



    This is what i did

    Code:
    import javax.swing.JOptionPane;
    
    class Even
    {
        public static void main (String args[])
    {
    
        String Num1;
        int number1, num;
    
        Num1= JOptionPane.showInputDialog("Enter a number: ");
        number1 = Integer.parseInt(Num1);
        
        isEven(num);
        
        System.exit( 0 );
    
    }
    
        static void isEven(int a)
            {
            
            if ( a % 2 == 1)
        
                {
                    JOptionPane.showMessageDialog(null, "You entered an even number " + a);
                }
           
            else if ( a % 2 == 0)
                {
                     JOptionPane.showMessageDialog(null, "You entered an odd number " + a);
                }
            
            
            }//isEven()
       }//end main

    Please tell me if am on the wrong track!!

  2. #2
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,119
    you are on right track except the following.
    the variable u are passing to isEven(num) is not asigned any values yet, it should be

    isEvent(number1);

    (a%2)==0 means its even
    (a%2)==1 means its odd

    second in your main message you need to put a condition after you get input from user where you check for input, if its zero then u don't call isEven() method but close the program

    something like this
    PHP Code:
         String Num1;
        
    int number1;
    while(
    true){//run a loop for this to go for ever unless user enters 0
        
    Num1JOptionPane.showInputDialog("Enter 0 (zero) to quit\nOr  Enter a number: ");
        
    number1 Integer.parseInt(Num1);
        if(
    number<=0){
        
    //exit program here
        
    System.exit(0);
        }else{
    //process data
         
    isEven(number1);
        }

    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

  3. #3
    Join Date
    Oct 2004
    Posts
    47
    Thank You bro ... Ill go with ur advice and reply 2 u soon

  4. #4
    Join Date
    Oct 2004
    Posts
    47
    Hi ... i tried this

    Code:
    import javax.swing.JOptionPane;
    
    class Even
    {
        public static void main (String args[])
    {
        
        String Num1; 
        int number1; 
        
        //run a loop for this to go for ever unless user enters 0 
        
        while(true)
        {
    
            Num1= JOptionPane.showInputDialog("Enter 0 (zero) to quit\nOr  Enter a number: "); 
            number1 = Integer.parseInt(Num1); 
            
                if(number1<=0)
                { 
                
                System.exit(0); 
                }
        
                    else{ 
                isEven(number1); 
                } 
        }//end loop
    
        
    
        static void isEven(int a)
            {
            
            if ( a % 2 == 1)
        
                {
                    JOptionPane.showMessageDialog(null, "You entered an odd number " + a);
                }
           
            else if ( a % 2 == 0)
                {
                     JOptionPane.showMessageDialog(null, "You entered an even number " + a);
                }
            
            
            }//isEven()
       }//end main
    But the error i get is in this line

    Code:
    isEven(number1);
    ^^^ cannot resolve symbol method isEven (int)

  5. #5
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,119
    that is because you have your isEven method/function in the main function.
    bring it out of that
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

  6. #6
    Join Date
    Oct 2004
    Posts
    47
    you mean i should not include it in the loop???

  7. #7
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,119
    yep....because you have function call in the loop
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

  8. #8
    Join Date
    Oct 2004
    Posts
    47
    it worked ... letz party

    Code:
    import javax.swing.JOptionPane;
    
    class Even
    {
       public static void main (String args[])
       {
           String Num1; 
           int number1; 
    
           //run a loop for this to go for ever unless user enters 0 
           while(true)
           {
               Num1= JOptionPane.showInputDialog("Enter 0 (zero) to quit\n" +
                                                 "Or Enter a number: "); 
               number1 = Integer.parseInt(Num1); 
    
               if(number1<=0)
                   System.exit(0); 
               else
                   isEven(number1); 
           }//end loop
       }// end main
    
       static void isEven(int a)
       {
           if ( a % 2 == 1)
               JOptionPane.showMessageDialog(null, "You entered an odd number " + a);
           else if ( a % 2 == 0)
               JOptionPane.showMessageDialog(null, "You entered an even number " + a);
       }//isEven()
    }//end class
    Last edited by NichoLe; 03-23-2005 at 03:02 AM.

  9. #9
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,119
    yayyyyy...
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles