Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
124 views

Java Programs

The document contains 8 Java programs that demonstrate various programming concepts: 1. A simple Hello World program 2. A program that reverses a number entered as a command line argument 3. A program that displays command line arguments passed to the main method 4. A program that demonstrates working with arrays by printing array elements 5. A program that calculates the sum of array elements and sorts them in ascending order
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
124 views

Java Programs

The document contains 8 Java programs that demonstrate various programming concepts: 1. A simple Hello World program 2. A program that reverses a number entered as a command line argument 3. A program that displays command line arguments passed to the main method 4. A program that demonstrates working with arrays by printing array elements 5. A program that calculates the sum of array elements and sorts them in ascending order
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Java Programs

1) Simple.java =>
public class Simple {

public static void main(String[] args) {


// TODO Auto-generated method stub
System.out.println("Hello TYBCA(Science)");
}

2) Reverse.java =>
/*(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=Integer.parseInt(args[0]);
System.out.println("Revese of given number
"+num+"is : ");
while(num>0)
{
int r=num%10;
System.out.print(r);
num=num/10;
}
}

3) Command Line Argument Example


CmdLine.java=>
public class CmdLine {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Arguments passed in command
line are: ");
for(int i=0;i<args.length;i++)
System.out.print(args[i]+" ");
}

4) ArrayDemo.java =>
public class ArrayDemo {

public static void main(String[] args) {


// TODO Auto-generated method stub
int a[]= {2,4,6,8,10};
System.out.println("Number of elements in Array =
"+a.length);
System.out.println("Array elements are : ");
for(int i=0;i<a.length;i++)
System.out.print(a[i]+" ");
}

5) Addition.java
/* (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<num.length;i++)
sum=sum+num[i];
System.out.println("The sum of Array elements =
"+sum);

//sorting array
for(i=0;i<num.length;i++)
{
for(j=i+1;j<num.length;j++)
{
if(num[i]>num[j])
{
int temp=num[i];
num[i]=num[j];
num[j]=temp;
}
}
}

System.out.println("Array elements in ascending


order: ");
for(i=0;i<num.length;i++)
System.out.print(num[i]+" ");
}

6) MultiTable.java =>
/* (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 java.io.*;

public class MultiTable {

public static void main(String[] args) throws


Exception{
// TODO Auto-generated method stub
int num,i;
System.out.println("Enter number : ");
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
num=Integer.parseInt(br.readLine());
for(i=1;i<=10;i++)
System.out.println(num+" * "+ i +" =
"+num*i);
}

7) MyNumber.java =>
/* (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=Integer.parseInt(args[0]);
MyNumber m=new MyNumber(x);
if(m.isNegative())
System.out.println("Number is Negative");
if(m.isPositive())
System.out.println("Number is Positive");
if(m.isEven())
System.out.println("Number is Even");
if(m.isOdd())
System.out.println("Number is Odd");
}

8) Employee.java
/*(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 java.util.*;

public class Employee {

Scanner sc=new Scanner(System.in);

String name;

public float salary;

void input() {

System.out.println("Enter Employee name :");

name=sc.next();

System.out.println("Enter Salary :");

salary=sc.nextFloat();

void show() {

System.out.println("Employee name :"+name);

System.out.println("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;
}
}

System.out.println("\n\n
***********Employee Having Maximum Salary************");

e[j].show();

You might also like