java tutorial check bigger smaller numbers method, beginners exercise

this is one of the exercise i give my total beginner students. those who are new to programming.

basically its methods that get 2 numbers or more and return the bigger or the smaller number.

the main goal is to show an example of a practical way to use ifs and program decision making (in this case who is the bigger or smaller number out of 2 or more numbers). it also covers usage of methods, arrays etc.

all around its a great exercise for beginners who started to learn java. its a very simple code and shows simple concepts.

first create a project for this java tutorial in eclipse. then create a package (you can give the project or the package any name).

create a class called bigger_smaller_then, this class will hold the methods we create.

then create this method:


	public int biggest_num(int a,int b) 
	{
		if(a>b) 
		{
			return a;
		}
		else 
		{
			return b;
		}
	}//end biggest_num

biggest_num is a method that returns an int value and gets two ints in its argument (int a, int b). it checks if a is bigger then b, if it is bigger return a, else return b (if the number a is not bigger this means b is bigger).

to create a method that checks for the smallest number out of two numbers we only need to change one thing (except the method name):


	public int smalest_num(int a,int b) 
	{
		if(a<b) 
		{
			return a;
		}
		else 
		{
			return b;
		}
	}//end smalest_num
	

in the if we changed the bigger then operator (>) to a smaller then operator (<). those are

very common comparison operators and the only ones we will need for this lesson.

what if we wanted to check the bigger number out of 10 numbers or more or less? for that purpose we will overload the method, method overload basically is when you create a few methods with the same name but with a different argument and implementation.

in this case its biggest_num, we create another biggest_num but with a different argument, instead of getting two ints it will get an array of ints also the methods code have changed too:


	public int biggest_num(int a[]) 
	{
		int biggest =a[0];
		for(int n :a) 
		{
			if(n>biggest) 
			{
				biggest = n;
			}
		}
		
		return biggest;
	}//end biggest_num

as you can see now biggest_num gets an int array (a[]). in the execute (the curly brackets),

first thing we do is declare a local variable to hold the biggest number (int biggest),

its default value is the first element in the a array, if its the biggest number thats what

will be returned by the method. its the most effective way to do this as we will compere bigest

to each number in the array.

to check all the numbers we will need to loop them for that we will use a for loop:


for(int n : a) 

basically what this line of code means is that for each item in the loop use int n to represent it

every loop, this for loop will loop all the array positions inside of a  int array ,each time put  the

current position inside of n so we can manipulate it.

inside the loop we compare n to biggest if n is bigger ,set n as biggest  as its the biggest number up until this point. in the end biggest will hold the biggest number in the array and thats where we return it as we found the biggest number of the array:

 


	if(n>biggest) 
			{
				biggest = n;
			}
		}
		
		return biggest; 

to find the smallest number in the array is the same (the only change is the comperison operator):


	public int smalest_num(int a[]) 
	{
		int smalest =a[0];
		for(int n :a) 
		{
			if(n<smalest) 
			{
				smalest = n;
			}
		}
		
		return smalest;
	}//end smalest_num 

in the main method use the class we created like this:

 


	public static void main(String[] args) {
	
		int n =0;//a number variable
		int[] nr = new int[] {2,1,3,5,8,6};//an int array (number array)
		
		//initialize bigger_smaller_then
		bigger_smaller_then bst = new bigger_smaller_then();
		
		//biggest num between two numbers
		n = bst.biggest_num(1,4);
		
		System.out.println(n);
		
		
		//smaleset between two numbers
		n = bst.smalest_num(2,4);
		
		System.out.println(n);
		
		//biggest in array
	    n = bst.biggest_num(nr);
		
		System.out.println(n);
		
		//smallest in array
	    n = bst.smalest_num(nr);
		
		System.out.println(n);
		
	}

 

if anything is still unclear or you want to learn more you can always contact me, im a java tutor and the first  lesson is free. in any case you can contact me and i may add or fix this java tutorial.

One thought on “java tutorial check bigger smaller numbers method, beginners exercise

Leave a comment