Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a Java Expert!

Join 307,003 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,961 people online right now. Registration is fast and FREE... Join Now!




boolean functions part 1(working with int)

 
Reply to this topicStart new topic

> boolean functions part 1(working with int)

computerfox
*****



post 28 Feb, 2009 - 03:01 PM
Post #1


before going into deep detail, first we should understand a few things.

boolean: just referring to the cases the statement is true or false for a certain object. they can either return a number if it's true or return the words that would indicate that is is true or false, which we will take a look in part 2.

function or more known in java as method: is just a certain process that you would write outside of the "main". any function or method is classified with a void because in any language, when the program sees void[/v], it stops and it yields to what that function does according to the programmer. these methods are usually done so that a programmer can use the same process in different parts of the main method without having to "waste" extra lines of code.

now let's look at the general structure for a boolean function or boolean:

a boolean (or any method in java) starts with public static [type] (in this case-boolean, we will look at a couple later on, probably) [name of method] (paramaters)

so let's take a look at a look at the constructor to a basic int boolean:

CODE

public static boolean greaterThanFive(int number)


okay, so how are going to write when it would be true for a number and return the numbers that are true:

CODE


{
         //any variables you would need
         boolean greater=true;
        
         for(int i=0;i<=number;i++)
         {
                if(number<5)
                {
                        greater=false;
                        break;   //usually used to exit the program
                 }
          }

          return greater;
}



now how are we going to manipulate this into our main method?

well now we can just write the main method as any other time, using this neat method we wrote outside of the main method.

CODE


public static void main (String args[])
    {
           int MAX;
       Scanner input=new Scanner(System.in);
      
       System.out.print("what is the highest number you want to test: ");
       MAX=input.nextInt();
      
       for(int i=0;i<=MAX;i++)
       {
           if(greaterThanFive(i))
           {
              System.out.println(i);
           }
        }  
      
    }


notice how we can immediately use it within an [i]if
or a loop.

so all together the code would look like this:

CODE


import java.util.*;

public class largeNumber {
    public static boolean greaterThanFive(int number)
    {
        
         //any variables you would need
         boolean greater=true;
        
         for(int i=0;i<=number;i++)
         {
                if(number<5)
                {
                        greater=false;
                        break;   //usually used to exit the program
                 }
          }

          return greater;
      }  

    public static void main (String args[])
    {
           int MAX;
       Scanner input=new Scanner(System.in);
      
       System.out.print("what is the highest number you want to test: ");
       MAX=input.nextInt();
      
       for(int i=0;i<=MAX;i++)
       {
           if(greaterThanFive(i))
           {
              System.out.println(i);
           }
        }  
      
    }
}



well, i hope to see you in part 2. any questions just ask.
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

KYA
Group Icon



post 1 Mar, 2009 - 09:36 AM
Post #2
CODE



{
         //any variables you would need
         boolean greater=true;
        
         for(int i=0;i<=number;i++)
         {
                if(number<5)
                {
                        greater=false;
                        break;   //usually used to exit the program
                 }
          }

          return greater;
}



There's no need for another variable. (i.e. greater):

CODE

{

        
         for(int i=0;i<=number;i++)
         {
                if(number<5)
                {
                        return false;
                 }
          }

          return true;
}
Go to the top of the page
+Quote Post

computerfox
*****



post 1 Mar, 2009 - 02:47 PM
Post #3
thanks k, but i was just referring to if you would ever need another variable, but usually you wouldn't need one.

This post has been edited by computerfox: 1 Mar, 2009 - 02:55 PM
Go to the top of the page
+Quote Post


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/21/09 06:43AM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month