Page 1 of 1

boolean functions part 1(working with int) Rate Topic: -----

#1 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Post icon  Posted 28 February 2009 - 04:01 PM

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:

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:


{
		 //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.


 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:


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.

Is This A Good Question/Topic? 0
  • +

Replies To: boolean functions part 1(working with int)

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3106
  • View blog
  • Posts: 19,145
  • Joined: 14-September 07

Posted 01 March 2009 - 10:36 AM



{
		 //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):

{

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

		  return true;
}


Was This Post Helpful? 0
  • +
  • -

#3 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Posted 01 March 2009 - 03:47 PM

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: 01 March 2009 - 03:55 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1