geekiestGeeks.com Some of the geekiest stuff on the web… :)

3Nov/090

CS Lab Homework…

Okay so my computer science lab homework this week reads as follows:

Write a recursive method that returns the smallest integer in an array of integers.

Pretty simple! lol... below is the code,

public class SmallerInt
{
	public static void main(String[] args)
	{
		int smallest = 0;
		try
		{smallest = Integer.parseInt(args[0]);}
		catch(NumberFormatException e){}
		for(int i=0; i<args.length; i++)
		{
			try
			{
				if(smallest > Integer.parseInt(args[i]))
                                { 
                                      smallest = Integer.parseInt(args[i]); 
                                 }
			}
			catch(NumberFormatException e)
			{
				System.out.println("All parameters must be Integer, PROGRAM ABORTED"); 
				break;
			}
		}
		System.out.println("The smallest number is: " + smallest);
	}	
}


Bookmark and Share

About Jay

Webmaster! :)
Filed under: General, Java Leave a comment
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


No trackbacks yet.