BSC IT PRACTICALS: JAVA PROGRAMS
Showing posts with label JAVA PROGRAMS. Show all posts
Showing posts with label JAVA PROGRAMS. Show all posts

Friday, February 9, 2018

Designed a class SortData that contains the method asec() and desc() in java

February 09, 2018 3
Designed a class SortData that contains the method asec() and desc() in java
import java.util.*;
class prac4A
{
Scanner input=new Scanner(System.in);
int num,i;
int arr[];
int temp=0;
public void getdata()
{
System.out.print("Enter the size of array: ");
num=input.nextInt();
arr=new int[num];
System.out.print("Enter the number: ");
for( i=0;i<num;i++)
{
arr[i]=input.nextInt();
}
}
void putdata()
{
System.out.print("Given numbers are: ");
for(i=0;i<num;i++)
{
System.out.println(arr[i]);
}
}
void asce()
{
for(i=0;i<num;i++)
{
for(int j=i+1;j<num;j++)
{
if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
System.out.print("Ascending order of number are: ");
for(int i=0;i<num;i++)
{
System.out.println(arr[i]);
}
}
void desc()
{
for(i=0;i<num;i++)
{
for(int j=i+1;j<num;j++)
{
if(arr[i]<arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
System.out.print("Descending order of number are: ");
for(int i=0;i<num;i++)
{
System.out.println(arr[i]);
}
}
public static void main(String args[])
{
prac4A ob=new prac4A();
ob.getdata();
ob.putdata();
ob.asce();
ob.desc();
}
}

Write a java program to demonstrate the implementation of abstract class.

February 09, 2018 1
 Write a java program to demonstrate the implementation of abstract class.

import java.util.Scanner;
abstract class test
{
abstract void get();
}
class test1 extends test
{
void get()
{
int a,b;
Scanner ob=new Scanner(System.in);
System.out.print("Enter 1st Number: ");
a=ob.nextInt();
System.out.println("Enter 2st Number: ");
b=ob.nextInt();
System.out.println("Addition is: "+(a+b));
}
}
class prac4C
{
public static void main(String args[])
{
test1 obj=new test1();
obj.get();
}
}

Designed a class that demonstrates the use of constructor and destructor in java

February 09, 2018 32
Designed a class that demonstrates the use of constructor and destructor in java

class xyz
{
xyz()
{
System.out.println("Constructor method........");
}
protected void finalize()
{
System.out.print("Garbage Collected.....");
}
}
class prac4B
{
public static void main(String args[])
{
xyz ob=new xyz();
ob=null;
System.gc();
}
}

Friday, January 19, 2018

Java function that calculates the sum of digits for a given char array consisting of the digits '0' to '9'. The function should return the digit sum as a long value

January 19, 2018 0
Java function that calculates the sum of digits for a given char array consisting of the digits '0' to '9'. The function should return the digit sum as a long value

CODE:

import java.util.*;
class prac3B
{
public static void main(String args[])
{
Scanner ob=new Scanner(System.in);
System.out.print("Enter the any string: ");
String s=ob.nextLine();
count(s);
}

public static void count(String str)
{
int sum=0;
int d=0;
char ch[]=str.toCharArray();
for(int i=0;i<str.length();i++)
{
if(Character.isDigit(ch[i]))
{
sum+=Character.getNumericValue(ch[i]);
}

}
System.out.println("Your addtion is: "+sum);
}
}

Find the smallest and largest element from the array in java

January 19, 2018 0
 Find the smallest and largest element from the array in java

CODE:

class prac3C
{
  public static void main(String args[])
  {
  
     int num[]=new int[]{10,20,30,5};
int s=num[0];
int l=num[0];
for(int i=0;i<num.length;i++)
{
    if(l<num[i])
{
  l=num[i];
  }
  else
  {
    s=num[i];
}
}
System.out.println("Largest Number are:"+l);
System.out.println("Smallest number are: "+s);

}}

Java program to count the letters, spaces, numbers and other characters of an input string

January 19, 2018 1
Java program to count the letters, spaces, numbers and other characters of an input string

CODE:
import java.util.*;
class prac3A
{
public static void main(String args[])
{
    Scanner input=new Scanner(System.in);
System.out.print("Enter A String: ");
String str=input.nextLine();
     int letter=0,space=0,digit=0,other=0;
   char ch[]=str.toCharArray();
for(int i=0;i<str.length();i++)
{
   if(Character.isLetter(ch[i]))
   {
       letter++;
  }
  else if(Character.isDigit(ch[i]))
  {
     digit++;
}
else if(Character.isSpaceChar(ch[i]))
           {
      space++;
  }
  else{
   other++;
   }
   }
System.out.println("Letter are: "+letter);
System.out.println("Space are: "+space);
System.out.println("Digit are: "+digit);
System.out.println("Other: "+other);    
}

}

Monday, January 15, 2018

Java program to reverse a string.

January 15, 2018 0
Java program to reverse a string.

CODE:

import java.util.*;
class prac2C
{
String rev="";
public String reversestring(String str)
{
if(str.length()==1)
{
return str;
}
else
{
rev+=str.charAt(str.length()-1)+reversestring(str.substring(0,str.length()-1));
return rev;
}
}
public static void main(String args[])
{
Scanner input=new Scanner(System.in);
prac2C ob=new prac2C();
System.out.print("Enter the String ");
String str=input.nextLine();
System.out.print("Result is "+ob.reversestring(str));
}}

Java program to convert a decimal number to binary number and vice versa.

January 15, 2018 0
Java program to convert a decimal number to binary number and vice versa.

CODE:    
 
import java.util.*;
class prac2B
{
public static void main(String args[])
{
Scanner input=new Scanner(System.in);
System.out.print("Enter the Decimal number ");
int no=input.nextInt();
int i=0;
int arr[]=new int[100];
while(no>0)
{
arr[i++]=no%2;
no=no/2;
}
System.out.print("Binary number are: ");
for(int j=i-1;j>=0;j--)
{
System.out.print(arr[j]);
}
}

}

Java program to add two binary numbers.

January 15, 2018 0
Java program to add two binary numbers.

CODE:

import java.util.Scanner;
class prac2A
{
public static void main(String[] args)
{
long binary1, binary2;
int i = 0, remainder = 0;
int[] sum = new int[20];
Scanner in = new Scanner(System.in);
System.out.print("Input first binary number: ");
binary1 = in.nextLong();
System.out.print("Input second binary number: ");
binary2 = in.nextLong();
while (binary1 != 0 || binary2 != 0)
{
sum[i++] = (int)((binary1 % 10 + binary2 % 10 + remainder) % 2);
remainder = (int)((binary1 % 10 + binary2 % 10 + remainder) / 2);
binary1 = binary1 / 10;
binary2 = binary2 / 10;
}
if (remainder != 0)
{
sum[i++] = remainder;
}
--i;
System.out.print("Sum of two binary numbers: ");
while (i >= 0)
{
System.out.print(sum[i--]);
}
System.out.print("\n");
}

}

Java program to print the area and perimeter of a circle.

January 15, 2018 0
Java program to print the area and perimeter of a circle.

CODE:

import java.util.*;
import java.lang.Math.*;
class prac1C
{
public static void main(String args[])
{
double r,perimeter,Area;
Scanner ob=new Scanner(System.in);
System.out.print("Enter the Radius of Circle: ");
r=ob.nextDouble();
perimeter =2*Math.PI*r;
Area=Math.PI*r*r;
System.out.println("Perimeter of Circle is: "+perimeter);
System.out.println("Area of Circle is: "+Area);
}

}

Java program to display the following pattern.

January 15, 2018 0
Java program to display the following pattern.

CODE:

class prac1B
{ public static void main(String args[])
{
int i,j;
for(i=5;i>=1;i--)
{
for(j=i;j>=1;j--)
{
System.out.print("*");
}
System.out.println(" ");
}
}

}

Java program that takes a number as input and prints its multiplication table upto 10.

January 15, 2018 0
Java program that takes a number as input and prints its multiplication table upto 10.

CODE:     
                                                         
import java.util.*;
class prac1A
{
public static void main(String ar[])
{
int no,s=0,i;
Scanner ob=new Scanner(System.in);
System.out.print("Enter the Number: ");
no=ob.nextInt();
for(i=1;i<=10;i++)
{
s=no*i;
System.out.println(no+"*"+i+"= "+s);
}
}  }