java programs from prasad technologies, Exercises of Java Programming

these are general examples for method overloading

Typology: Exercises

2016/2017

Uploaded on 02/11/2017

narendra_dg93
narendra_dg93 🇮🇳

1 document

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
//methods inside Main class
class Methods
{
public static void main(String []args)
{
System.out.println("\t\t Welcome to Main");
callFirst();
System.out.println("\t\t Welcome Back to Main");
}
static void callFirst()
{
System.out.println("Welcome to First ");
callSecond();
}
static void callSecond()
{
System.out.println("Welcome to Second ");
callThird();
}
static void callThird()
{
System.out.println("Welcome to Third");
}
pf3
pf4
pf5

Partial preview of the text

Download java programs from prasad technologies and more Exercises Java Programming in PDF only on Docsity!

//methods inside Main class class Methods { public static void main(String []args) { System.out.println("\t\t Welcome to Main"); callFirst(); System.out.println("\t\t Welcome Back to Main"); }

static void callFirst() { System.out.println("Welcome to First "); callSecond();

} static void callSecond() { System.out.println("Welcome to Second "); callThird();

static void callThird() { System.out.println("Welcome to Third");

}

//method with arguments : class MethodsArgs { public static void main(String []args) { int n1,n2;

n1=Integer.parseInt(args[0]); n2=Integer.parseInt(args[1]);

int res; res=getResult(n1,n2);

System.out.println("Result of " + n1 + " + " + n2 + " = " + res); }

//method with arguments and return value static int getResult(int n1,int n2) { return(n1+n2); } }

//Method overloading

//VarArgs concept

class VarArgs { public static void main(String []args) { printSum(1); printSum(1,2); printSum(1,2,3); printSum(1,2,3,4); printSum(1,2,3,4,5,6); } static void printSum(int ... list) /*var args concept */ { System.out.print("Sum of " + list.length + " Nos { "); int sum=0; for(int i : list) { System.out.print(i + " + "); sum+=i; } System.out.println("\b\b = " + sum + " ] "); } }

//concept of recursion class Recursion { public static void main(String []args) { int no; no=Integer.parseInt(args[0]);

System.out.println("Factorial of " + no + " is " + getFact(no)); } static int getFact(int x) /* recursion concept */ { if(x==1) return(1); else return(x * getFact(x-1)); } }

/* nesting of methods */

class NestedMethods { public static void main(String []args)

return("First"); else if(avg>60) return("Second"); else if(avg>50) return("Third"); else return("Fail"); } }