Java – Activity1.1

Activity1.1            : Command line programming – Method Overloading

Program File         : MethodOverload.java

Procedures            : 1. Type the program file.     

                               : 2. Save/Compile the program.                       

                               : 3. Using the Command Prompt (C:\>), execute the following:

                                    Path = C:\jdk\bin (or look for the equivalent directory)                                   

                                    Javac MethodOverload.java          (compiling the program)                  

                               :  4. Execute the program with test data.                            

                                     Scenario1:       Java MethodOverload 5

                                    Output:            The area is : 78.54                             

                                    Scenario2:       Java MethodOverload 5 3                             

                                    Output:            The area is : 12.5 

                                     Scenario3:       Java MethodOverload                                      

                                     Output:            error 

                                     Scenario4:        Java MethodOverload 1 2 3                                     

                                     Output:            error

 

public class MethodOverload

{ static double n1,n2,a;

            static double area(double n1)

            { a = 3.1416 * n1*n1;

               return a;

            }

static double area(double n1, double n2)

{ a = 1/2.0 * n1*n1;

   return a;

}

public static void main(String[] args)

{

               if (args.length>2 || args.length<1)

                        System.out.println(“error”);

                        else

                        {

                            n1 = Double.parseDouble(args[0]);

                             if (args.length==1)

                                       System.out.println(“The area is: “+ area(n1));

                                      else

                                      {

                                            n2 = Double.parseDouble(args[1]);

                                         System.out.println(“The area is: “+ area(n1,n2));

                                      }

                        }

            }

}

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.