Skip to main content

Posts

Showing posts from June, 2015

Open Text File and Read Words Print and store without repetition

import java.io.*; import java.util.*; class CountNumbersofEachWordInaFile {   private ArrayList<String> readFile(String filename) {   ArrayList<String> records = new ArrayList<String>();   try   {     BufferedReader reader = new BufferedReader(new FileReader(filename));     String line;    while ((line = reader.readLine()) != null)     { for (String retval: line.split("\\s+")){ if(records.contains(retval.toLowerCase())){        continue;    }    else                 records.add(retval.toLowerCase()); }     }     reader.close();     return records;   }   catch (Exception e)   {     System.err.format("Exception occurred trying to read '%s'.", filename);     e.printStackTrace();     return null;   } } public static void main(String a[]) ...

Open Text File and Read Lines and Print Lines On Console

import java.io.*; import java.util.*; class CountNumbersofEachWordInaFile {     private ArrayList<String> readFile(String filename) {   ArrayList<String> records = new ArrayList<String>();   try   {     BufferedReader reader = new BufferedReader(new FileReader(filename));     String line;     while ((line = reader.readLine()) != null)     {       records.add(line);     }     reader.close();     return records;   }   catch (Exception e)   {     System.err.format("Exception occurred trying to read '%s'.", filename);     e.printStackTrace();     return null;   } } public static void main(String a[]) throws Exception { CountNumbersofEachWordInaFile obj = new CountNumbersofEachWordInaFile(); ArrayList<String> list = new ArrayList<String>(); list=obj.readFile("txt.txt"); Iterator<String>...

Write a program to demonstrate the use of Generics ?

import java.util.*; public class UseofGenerics { public static void main(String a[]) { System.out.println("Welcome to the UseofGenerics project!"); ArrayList<String> list = new ArrayList<String>(); list.add("Sarvajeet"); list.add("Gupta"); String s = list.get(1); System.out.println("Hello "+s); //Access whole list using iterator Iterator<String> itr = list.iterator(); while(itr.hasNext()){    System.out.println(itr.next()); } } } http://www.programmr.com/node/163227

Write a program to demonstrate the use of interface ?

import java.io.*; interface Printable{      void print(); } interface Showable(){     void show(); } public class interfaceexample implements Printable,Showable {     public void print(){         System.out.println("Hello");     }         public void show(){         System.out.println("Welcome!!");     } public static void main(String a[]) throws Exception { System.out.println("Welcome to the interfaceexample project!"); interfaceexample obj = new interfaceexample(); obj.print(); obj.show(); } } http://www.programmr.com/Java/interface_example

write a program to find the biggest element in the Array ?

import java.io.*; public class BiggestElementinArray { public static void main(String a[]) throws Exception { System.out.println("Welcome to the BiggestElementinArray project!"); int [] array = {10,2,30,15,5,41}; int largest=array[0]; for(int i=1;i<array.length;i++){    if(array[i]>largest){        largest=array[i];    } } System.out.println(largest); } } http://www.programmr.com/Java/Biggest_Element_in_Array

write a program to print all the prime numbers up to a given number ?

import java.io.*; class primenumber { public static void main(String a[]) throws Exception { System.out.println("Welcome to the primenumber project!"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter Upper Bound Number"); int N = Integer.parseInt(br.readLine()); int i=0,j=0; int flag=1; System.out.print("2\t3\t"); for(i=4;i<N;i++){    for(j=2;j<=Math.sqrt(i);j++){        if(i%j==0){            flag=0;            break;        }    }    if(flag==1){        System.out.print(i+"\t");    }    flag=1; } System.out.println(); } } http://www.programmr.com/Java/prime_number-5

Write a program to check whether two strings are Anagrams or not ?

import java.io.*; import java.util.Arrays; public class Anagram { public static void main(String a[]) throws Exception { System.out.println("Welcome to the Anagram project!"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str1 = br.readLine(); String str2 = br.readLine(); String str3= new String(str1.replaceAll("\\s+","")); String str4= new String(str2.replaceAll("\\s+","")); System.out.println(str3); char [] chars1 = str3.toCharArray(); char [] chars2 = str4.toCharArray(); Arrays.sort(chars1); Arrays.sort(chars2); String sorted1 = new String(chars1); String sorted2 = new String(chars2); if(sorted1.equals(sorted2)){ System.out.println("Two Strings are Anagrams"); } else System.out.println("Strings are not Anagrams"); } }

Write a program to reverse a string without using String reverse built-in Function ?

import java.io.*; import java.util.Arrays; public class reverseanarray { public static void main(String a[])throws IOException { System.out.println("Welcome to the reverseanarray project!"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str = br.readLine(); char [] chars1 = str.toCharArray(); int length = str.length(); int temp=length; char [] chars2=new char[length]; int i=0; for(i=0;i<temp;i++){    chars2[i]=chars1[length-1];    length--; } String new1 = new String(chars2); System.out.println("Reverse of "+str+" is "+new1); } }

write a program to swap two variables without a temp variable

import java.io.*; import java.util.Scanner; public class swaptwovariables { public static void main(String args[]) throws Exception { System.out.println("Welcome to the swaptwovariables project!"); Scanner input = new Scanner(System.in); System.out.println("Enter two numbers a and b to swap"); int a= input.nextInt(); int b= input.nextInt(); System.out.println("a and b before swapping "+a+" "+b); a=a+b; b=a-b; a=a-b; System.out.println("a and b after swapping "+a+" "+b); } }

Write a program to print a matrix pattern

import java.io.*; import java.util.Scanner; public class MatrixPattern {  public static void main(String a[]) throws IOException  {   System.out.println("Welcome to the MatrixPattern project!");           double [][] arr1 = new double [3][3];         double [][] arr2 = new double [3][6];                 Scanner input = new Scanner(System.in);         int i=0,j=0,k=0;               for(i=0;i<3;i++){             for(j=0;j<3;j++){                 arr1[i][j]=input.nextDouble();             }         }               for(i=0;i<3;i++){             for(j=0;j<3;j++){                 arr2[...

Write a Program to implement stack in Java

import java.io.*; import java.util.Scanner; public class MyStack {     private long[] StackArray;     private int maxSize;     private int top;         MyStack(int s){         maxSize=s;         top=-1;         StackArray = new long[maxSize];     }         public void push(long j){         if(isFull()){             System.out.println("Stack Is Full");         }         StackArray[++top]=j;     }         public void pop(){         if(isEmpty()){             System.out.println("Stack is Empty");             return;         }         System.out.println(StackArray[top]);     ...

Write a Program to multiply two 3x3 2D matrices

import java.io.*; import java.util.Scanner; public class MatrixMultiplication { public static void main(String a[]) throws IOException { System.out.println("Welcome to the MatrixMultiplication project!");         double [][] arr1 = new double [3][3];         double [][] arr2 = new double [3][3];         double [][] arr3 = new double [3][3];         Scanner input = new Scanner(System.in);         int i=0,j=0,k=0;                 for(i=0;i<3;i++){             for(j=0;j<3;j++){                 arr1[i][j]=input.nextDouble();             }         }                 for(i=0;i<3;i++){             for(j=0;j<3;j++){     ...

Write a program to check whether a number is odd or even ?

import java.io.*; public class SumofDigitsofagivennumber { public static void main(String a[]) throws IOException { System.out.println("Welcome to the SumofDigitsofagivennumber project!"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String num0 = br.readLine(); System.out.println("Enter an Integer"); int num = Integer.parseInt(num0); int sum=0; while(num!=0){    sum+=num%10;    num=num/10; } System.out.println("The sum of digits of input is "+sum); } }

Write a program to find the sum of digits of a given Number ?

import java.io.*; public class SumofDigitsofagivennumber { public static void main(String a[]) throws IOException { System.out.println("Welcome to the SumofDigitsofagivennumber project!"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String num0 = br.readLine(); System.out.println("Enter an Integer"); int num = Integer.parseInt(num0); int sum=0; while(num!=0){    sum+=num%10;    num=num/10; } System.out.println("The sum of digits of input is "+sum); } }

Write a program to check whether a Number is palindrome or not ?

import java.io.*; public class Palindrome { public static void main(String a[]) throws Exception { System.out.println("Welcome to the Palindrome project!"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int num = Integer.parseInt(br.readLine()); int temp=num; int revnum=0; while(temp!=0){    revnum*=10;    revnum+=temp%10;    temp/=10; } if(num==revnum){    System.out.println(num+" is a Palindrone Number"); } else    System.out.println(num+" is not a Palindrone Number"); } }

Write A program to check Number is Power of two or not

import java.io.*; public class PowerOfTwo { public static void main(String a[]) throws IOException { System.out.println("Welcome to the PowerOfTwo project!"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int num = Integer.parseInt(br.readLine()); int flag=1; if(num==0){    System.out.println("Not a power of two");    System.exit(0);   } while(num!=1){    if(num%2!=0){        flag=0;        break;    }  num=num/2; } if(flag==0){    System.out.println("Not a power of two"); } else    System.out.println("Power of Two"); } }

Armstrong Number

import java.io.*; public class ArmstrongNumber { public static void main(String a[]) throws Exception { System.out.println("Welcome to the ArmstrongNumber project!"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter a Number to check Armstrong Number"); int num = Integer.parseInt(br.readLine());                 int temp = num;                 int cubesum=0; while(temp!=0){    cubesum+=Math.pow((temp%10),3);    temp/=10; } if(cubesum==num){    System.out.println("Number Is Armstrong"); } else    System.out.println("Number is not Armstrong"); } }