1.
Program to assign two integer values to X and Y. Using the ‘if’ statement the
output of the program should display a message whether X is greater than Y.
class main
{ public static void main (String args[])
{ int x=20,y=20;
if(x==y)
System.out.println("Both are equal");
else if(x>y)
System.out.println(x+ "is greater ");
else
System.out.println(y+ " is greater ");
}
}
Output:
2.
Program to list the factorial of the numbers 1 to 10. To calculate the factorial
value, use while loop. (Hint Fact of 4 = 4*3*2*1) class factorial
class factorial
{ public static void main (String args[])
{ int count=1; long
factorial=1;
System.out.println("Number " + " Factorial");
while(count<=10)
{ factorial=factorial*count;
System.out.println(" "+count+" "+factorial);
count++;
}
}
}
Output:
3.
Program to add two integers and two float numbers. When no arguments are
supplied, give a default value to calculate the sum. Use function overloading.
public class overloadmethod
{ public int functionname(int x,int y)
{ return (x+y);
}
public int functionname(int x,int y,int z)
{ return (x+y+z);
}
public double functionname(double x,double y)
{ return (x+y);
}
public static void main(String args[])
{ overloadmethod s = new overloadmethod();
System.out.println(s.functionname(10,20));
System.out.println(s.functionname(10,20,30));
System.out.println(s.functionname(10.5,19.5));
}
}
Output:
4.
Program to perform mathematical operations. Create a class called AddSub
with methods to add and subtract. Create another class called MulDiv that
extends from AddSub class to use the member data of the super class. MulDiv
should have methods to multiply and divide A main function should access the
methods and perform the mathematical operations.
class addsub
{ int num1; int
num2;
addsub(int n1,int n2)
{
num1=n1;
num2=n2;
} int
add()
{
return num1+num2;
} int
sub()
{ return num1-num2;
}
public void display()
{
System.out.println("Number 1 is "+num1);
System.out.println("Number 2 is "+num2);
}
}
class multidiv extends addsub
{ public multidiv(int n1,int n2)
{ super(n1,n2);
} int
mul()
{
return num1*num2;
}
float div()
{ return num1/num2;
}
public void display()
{
System.out.println("Number 1 is "+num1);
System.out.println("Number 2 is "+num2);
}
}
public class adsb
{ public static void main(String args[])
{ addsub r1 = new addsub(50,20);
int ad = r1.add();
int sb = r1.sub();
r1.display();
System.out.println("Addition= "+ad);
System.out.println("Subtraction=
"+sb); multidiv r2 = new
multidiv(20,4); int ml = r2.mul(); float
dv = r2.div(); r2.display();
System.out.println("Multiplication= "+ml);
System.out.println("Division= "+dv);
}}
Output:
5. Program with class variable that is available for all instances of a class. Use
static variable declaration. Observe the changes that occur in the object’s member
variable values.
class mathoperator
{ static double mul(double x,double y)
{ return x*y;
}
static double div(double x,double y)
{ return x/y;
}
}
class mathapplication
{ public static void main(String args[])
{ double a = mathoperator.mul(4.0,5.0);
double b = mathoperator.div(a,2.0);
System.out.println("b = "+b);
System.out.println("a = "+a);
}
}
Output: