Tuesday 15 December 2015

Exception case in which explicit typecasting is not required in narrowing in Java ( Using Final Keyword )

We all know that in case of narrowing (conversion of higher range data type into lower) explicit typecasting is required. But here is an exception case in which we do not have to explicit typecast in case of narrowing.

1. Int to byte 

Without using final keyword

int i=10;
byte b=i;

Above code will give throw a compile time error as int cannot be assigned to byte. For above program to rum we need to explicitly typecast int to byte.

int i=10;
byte b=(int)i;

This time program will run without giving any error message

Using final keyword

final int i=10;
byte b=i;

In this case

Wednesday 9 December 2015

Java program to count Number of words in a entered String

This is a program to count number of Words in a Entered String.

Source Code:

import java.io.*;
class Count
{
public static void main(String args[])
{
String s;
int count=0;
System.out.print("Enter a String: ");
Console con=System.console();
s=con.readLine();
char arr[]=s.toCharArray();
for(int i=0;i<arr.length;i++)
{
if(arr[i]==' ')
count++;
}
System.out.print("There are "+(count+1)+" words in Entered String");
}
}

Logic:We know that there is one blank space in between two words. Using the same fact we have created  the program in which we count number of spaces and increase it by 1 in final result.

Output:


Java Program to check weather a Entered String is palindrome or not

Here is a java program to check weather a Entered String is palindrome or not. In this program we will ask user to enter a string and will check if it is palindrome. 
palindrome is a word, phrase, number, or other sequence of characters which reads the same backward or forward.

Source Code: 


import java.io.*;
class Palindrome
{
public static void main(String args[])
{
String s;
System.out.print("Enter a String: ");
Console con=System.console();
s=con.readLine();
char arr[];
arr=s.toCharArray();
char arr2[]=new char[arr.length];
for(int i=arr.length-1,j=0;i>=0;i--,j++)
{
arr2[j]=arr[i];
}
String s2;
s2=String.copyValueOf(arr2);
if(s.equalsIgnoreCase(s2))
{
System.out.println("Entered String is pallindrome");
}
else
{
System.out.println("Entered String isn't pallindrome");
}
}
}

Output:



Sunday 6 December 2015

A simple Vertical Histogram

This program will print a vertical histogram. This program will ask 10 values from user and will print a vertical histogram according to the values entered.

Source code: 

import java.io.*;
class VerticalHistogram
{
public static void main(String args[])
{
int max=0;
Console con=System.console();
System.out.println("Enter 10 values: ");
int val[]=new int[10];
for(int i=0;i<10;i++)
{
System.out.print("Value "+(i+1)+": ");
val[i]=Integer.parseInt(con.readLine());
System.out.print("\n");
if(val[i]>max)
{
max=val[i];
}

}

for(int i=max;i>=0;i--)
{
for(int j=0;j<10;j++)
{

if((i-val[j])>=0)
{
System.out.print(" ");
System.out.print(" ");
}
else
{
System.out.print("*");
System.out.print("*");
}
System.out.print(" ");
}
System.out.println("");
}
System.out.print("Numbers Entered\n");
System.out.print(" ");
for(int j=0;j<10;j++)
{
System.out.print(" "+val[j]);
}


}
}

Output:





Saturday 5 December 2015

A simple decryptor ( For a password encrypted from previous above encryptor)

import java.io.*;
class Encryptor
{
public static void main(String args[])
{
String in;
Console con=System.console();
int length=0;
System.out.print(" Enter your Encrypted password ");
in=con.readLine();
length=in.length();
char a[]= new char[length];
char b[]= new char[length];
for(int i=0;i<length;i++)
{
int temp;
a[i]=in.charAt(i);
}

System.out.print("\n Decrypted password is: ");
for(int i=0;i<length;i++)
{
int temp;
temp=((int)a[i])-i*i-3;
b[i]=(char)temp;
System.out.print(b[i]);
}
}
}

A very Simple Encryptor

//Simple Encryptor
import java.io.*;
class Encryptor
{
public static void main(String args[])
{
String in;
Console con=System.console();
int length=0;
System.out.print(" Enter your Password to be encrypted: ");
in=con.readLine();
length=in.length();
char a[]= new char[length];
char b[]= new char[length];
for(int i=0;i<length;i++)
{
int temp;
a[i]=in.charAt(i);
temp=((int)a[i])+i*i+3;
b[i]=(char)temp;
}
System.out.print("\n Encrypted password is: ");
for(int i=0;i<length;i++)
{
System.out.print(b[i]);
}
}
}