Thursday, 9 April 2015

Simple Java Programs

One of the ways is to use the Scanner class


Scanner class is present in java.util package so we import this package in our program. We first create an object of Scanner class and then we use the methods of Scanner class. Consider the statement
 Scanner a = new Scanner(System.in);
Here Scanner is the class name, a is the name of object, new keyword is used to allocate the memory and System.in is the input stream. Following methods of Scanner class are used in the program below :-
1) nextInt to input an integer
2) nextFloat to input a float
3) nextLine to input a string

Points to remember :

>>Please import the package using the below command in your code
import java.util.Scanner;

>>In case you are using Jdeveloper and still you are not getting an option to enter user's input after you did everything correct ,please check the following setting.
Project properties->Run/Debug configuration->Edit button->Tools settings -> Allow Program Inputs.
by doing this you should have a small input box a the bottom of  the Run window  @ runtime

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Sample Code

import java.util.Scanner;
 
class GetInputFromUser
{
   public static void main(String args[])
   {
      int a;
      float b;
      String s;
 
      Scanner input = new Scanner(System.in);
 
      System.out.println("Enter a string");
      s = input .nextLine();
      System.out.println("You entered string "+s);
 
      System.out.println("Enter an integer");
      a = input .nextInt();
      System.out.println("You entered integer "+a);
 
      System.out.println("Enter a float");
      b = input .nextFloat();
      System.out.println("You entered float "+b);   
   }
}


++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

PRIME NUMBER PROGRAM IN JAVA

import java.util.Scanner;
public class Prime{

     public static void main(String []args){
        System.out.println("Hello World");
        Scanner input = new Scanner(System.in);
        double a;
        int flag=1;
        double srnum;
        System.out.println("Enter the Integer");
        a = input.nextInt();
        srnum=Math.sqrt(a);
        for(int i=2;i<=srnum;i++)
    {
         if((a%i)!=0){continue;}
         else{flag=0;System.out.println("Number not prime"); break;}
     }
    if (flag==1){System.out.println("Number"+ a +"is prime");} 
    }
     

}


+++++++++++++++++++++++++++++++++++++++++++++


PALINDROME  NUMBER PROGRAM IN JAVA



import java.util.Scanner;
public class Palindrom{
    
     public static void main(String []args){
     int a[] =new int[3];
     int b[] =new int[3];
     System.out.println("Enter the number ");
     Scanner input = new Scanner(System.in);
     int j=a.length-1;
     for (int i=0 ;i<=a.length-1;i++)
        {  a[i]=input.nextInt();}
    for (int i=0 ;i<=a.length-1;i++){
         b[i]=a[j];
           j--;
        }
      int  flag=1;
     for (int i=0 ;i<a.length;i++){if (a[i]==b[i]) continue; else {flag=0; break;}}       
     if(flag==1) System.out.println("This Number is a palindrome");
     else System.out.println("This Number is NOT a palindrome");
}
}


+++++++++++++++++++++++++++++++++++++++++++++++++++++++++


Write a program that will read a character and display its numeric value.


import java.util.Scanner;
public class chartoint{

     public static void main(String []args){
     // String c=args[0];
    Scanner input=new Scanner(System.in);
     String c1=input.nextLine();
     char ch=c1.charAt(0);
     int ctoint=(int)ch;
     System.out.println(ctoint);
     }
}


+++++++++++++++++++++++++++++++++++++++++++++++++++++++++


Reverse a given integer.

import java.util.Scanner;
public class RevNumber{

     public static void main(String []args){
     // String c=args[0];
    Scanner input=new Scanner(System.in);
     int num=input.nextInt();
     System.out.println("The number is  "+num);
     System.out.print("The rev number is  ");
     //int i=0;
     while (num>0){
        int rem=num%10;
        System.out.print(rem);
        num=num/10;
        //i++;
     }
     System.out.println();
     //if(i==2)
     //System.out.println("Its a "+ i + " digit number");
     //else System.out.println("Its NOT  2 digit number");
     }

}

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


Reverse a given string.

 import java.util.Scanner;
public class RevString{

     public static void main(String []args){
     // String c=args[0];
      Scanner input=new Scanner(System.in);
     System.out.println("Enter a string");
      String s = input.nextLine();
      System.out.println("You entered string "+s);
      System.out.println("You Reversed string is :  ");
      for (int i=s.length()-1;i>=0;i--){
          System.out.print(s.charAt(i));
      }
      System.out.println();
      
     
     }
      
     }


+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


Check the given character is lowercase or not.


import java.util.Scanner;
public class Lower{

     public static void main(String []args){
     String c=args[0];char c1=c.charAt(0);
       int ctoi =(int)c1;
       if(ctoi>=97 && ctoi <=122 )System.out.println("The character entered is lower case");
      else      System.out.println("The character entered is NOT lower case");
      
     
     }
      //As per ASCII table Lower case is from 97=122
      //Upper case is from 65-90

     }

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


Swap the values of two integers without a temporary variable.

import java.util.Scanner;
public class Swap{

     public static void main(String []args){
        System.out.println("Hello World");
        Scanner input= new Scanner(System.in);
        int a;int b;
        a=input.nextInt();
        b=input.nextInt();
        System.out.println("The numbers are " + a + " " + b);
        a=a+b;//Stire the sum in one of the variable
        b=a-b;//Deduct the second var from the sum so we get the first var value
        a=a-b;//Since nw the second var has the previous val of the  first var.
        //So deduct it from the sum againto get the previous val of second var.
        System.out.println("The exchanged numbers are " +a +" " + b);
     }

}

NOTE:
Please refer to the following link for details regarding this swapping without using 3rd variable:
http://www.geeksforgeeks.org/swap-two-numbers-without-using-temporary-variable/



++++++++++++++++++++++++++++++++++++++++++++++++++++++++


Reverse the order of 1D array without a temporary array.


import java.util.Scanner;
public class ReverseArray{

     public static void main(String []args){
        int[] a= new int[5];
        int k;int b;
        Scanner input=new Scanner(System.in);
        for (int i=0;i<=a.length-1;i++){
            a[i]=input.nextInt();
        }
       //below is to check whether the array has odd or even elements.here its  5,odd
        if((a.length)%2==0) 
       {
        k=0;b=a.length-1;int t;
        while(k<b){t=a[k];a[k]=a[b];a[b]=t;k++;b--;}
        }
        else
       {
        k=0;b=a.length-1;int t;
        while(k<=b){t=a[k];a[k]=a[b];a[b]=t;k++;b--;}
        }
        System.out.print("The reversed Array is   ");
        for (int i=0;i<=a.length-1;i++){
        System.out.print(a[i]+ " ");}
     }

}


+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Find the most frequent element in a given 1D array.



import java.util.Scanner;
public class MostFrequentElement{
     static final int n=5;
     public static void main(String []args){
        int[] a= new int[n];
        int k;//this is used to hold the element being compared
        int cmax;//this is to hold the maximum count for an element
        int element;//this is the counter for element being compared in one pass
        int count;//for counting the element occurences in one pass
        int f=0;//storing the element which has maximmum occurences so far.must be initalise
        Scanner input=new Scanner(System.in);
        for (int i=0;i<=a.length-1;i++){
            a[i]=input.nextInt();
        }
        cmax=0;element=0;
        while(element<=a.length-1)
        {count=1; 
         k=a[element];
        for (int i=0;i<=a.length-1;i++){
            if (i==element) continue;
            if (a[i]==k) count++;
        } 
        if (cmax<count) {f=a[element];cmax=count;}
        element++;
            
        }
        System.out.println("The element " +f+" has come a maximium of "+cmax+" times");
        
     }
}

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++