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.





MultiQuote




|