Java Programs
1) [Link] =>
public class Simple {
public static void main(String[] args) {
// TODO Auto-generated method stub
[Link]("Hello TYBCA(Science)");
}
2) [Link] =>
/*(Practical Question) Write a Java Program to Reverse a
Number. Accept number using command line argument. */
public class Reverse {
public static void main(String[] args) {
// TODO Auto-generated method stub
int num=[Link](args[0]);
[Link]("Revese of given number
"+num+"is : ");
while(num>0)
{
int r=num%10;
[Link](r);
num=num/10;
}
}
3) Command Line Argument Example
[Link]=>
public class CmdLine {
public static void main(String[] args) {
// TODO Auto-generated method stub
[Link]("Arguments passed in command
line are: ");
for(int i=0;i<[Link];i++)
[Link](args[i]+" ");
}
4) [Link] =>
public class ArrayDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
int a[]= {2,4,6,8,10};
[Link]("Number of elements in Array =
"+[Link]);
[Link]("Array elements are : ");
for(int i=0;i<[Link];i++)
[Link](a[i]+" ");
}
5) [Link]
/* (Practical Question)
* Write a Java program to print the sum of elements of
the array.
* Also display array elements in ascending order */
public class Addition {
public static void main(String[] args) {
// TODO Auto-generated method stub
int num[]={15,10,20,12,50};
int i,j,sum=0;
//calculating sum
for(i=0;i<[Link];i++)
sum=sum+num[i];
[Link]("The sum of Array elements =
"+sum);
//sorting array
for(i=0;i<[Link];i++)
{
for(j=i+1;j<[Link];j++)
{
if(num[i]>num[j])
{
int temp=num[i];
num[i]=num[j];
num[j]=temp;
}
}
}
[Link]("Array elements in ascending
order: ");
for(i=0;i<[Link];i++)
[Link](num[i]+" ");
}
6) [Link] =>
/* (Practical Question) Write a Java program to accept a
number from command prompt
* and generate multiplication table of a number.
* Accept number using BufferedReader class.*/
import [Link].*;
public class MultiTable {
public static void main(String[] args) throws
Exception{
// TODO Auto-generated method stub
int num,i;
[Link]("Enter number : ");
BufferedReader br=new BufferedReader(new
InputStreamReader([Link]));
num=[Link]([Link]());
for(i=1;i<=10;i++)
[Link](num+" * "+ i +" =
"+num*i);
}
7) [Link] =>
/* (Practical Question) Define a class MyNumber having one
private integer data member.
* Write a default constructor initialize it to 0 and
another constructor to initialize it to a value.
* Write methods isNegative, isPositive, isOdd, iseven.
* Use command line argument to pass a value to the object
and perform the above tests. */
public class MyNumber {
private int x;
public MyNumber(){
x=0;
}
public MyNumber(int x){
this.x=x;
}
public boolean isNegative(){
if(x<0)
return true;
else return false;
}
public boolean isPositive(){
if(x>0)
return true;
else return false;
}
public boolean isOdd(){
if(x%2==1)
return true;
else return false;
}
public boolean isEven(){
if(x%2==0)
return true;
else return false;
}
public static void main(String [] args) throws
ArrayIndexOutOfBoundsException
{
int x=[Link](args[0]);
MyNumber m=new MyNumber(x);
if([Link]())
[Link]("Number is Negative");
if([Link]())
[Link]("Number is Positive");
if([Link]())
[Link]("Number is Even");
if([Link]())
[Link]("Number is Odd");
}
8) [Link]
/*(Practical Question) Write a java program which define
class Employee with data member as name and salary.
* program store the information of 5 Employees.
* display the name who earn maximum salary.(Use arrary of
object)*/
import [Link].*;
public class Employee {
Scanner sc=new Scanner([Link]);
String name;
public float salary;
void input() {
[Link]("Enter Employee name :");
name=[Link]();
[Link]("Enter Salary :");
salary=[Link]();
void show() {
[Link]("Employee name :"+name);
[Link]("Salary is :"+salary);
public static void main(String a[]) {
Employee e[]=new Employee[5];
float sal[]=new float[5];
for(int i=0;i<5;i++)
{
e[i]=new Employee();
e[i].input();
sal[i]=e[i].salary;
for(int i=0;i<5;i++)
{
e[i].show();
float max=sal[0];
int j=0;
for(int i=0;i<5;i++)
{
if(max<sal[i])
{
max=sal[i];
j=i;
}
}
[Link]("\n\n
***********Employee Having Maximum Salary************");
e[j].show();