Menu

Cloning example







class Customer implements Cloneable{

int id;
String name;
int age;
long phone;

String dno;
String street;
String town;
int pincode;

Customer(int id, String name,int age, long phone, String dno, String street, String town,int pincode)
{
this.id= id;
this.name=name;
this.age=age;
this.phone=phone;
this.dno= dno;
this.street= street;
this.town=town;
this.pincode=pincode;
}

void show(){
System.out.println(id+" "+name+" "+age+" "+phone);
System.out.println("ADDRESS :"+ dno+" "+street+" "+town+" "+pincode);
System.out.println("***********************************");
}
public Object clone() throws CloneNotSupportedException
{
if(this instanceof Cloneable){
Customer cus= new Customer(this.id, this.name,this.age,this.phone,this.dno,this.street,this.town,this.pincode);
return cus;
}
else throw new CloneNotSupportedException(getClass().getName());
}}


public class Test{
public static void main(String args[]) throws CloneNotSupportedException{
Customer cus1=new Customer(10,"sri",10,81438900L,"20/789","Ramalaya street","hyd",521001);
Customer cus2=(Customer)cus1.clone();

System.out.println(cus1==cus2);
cus1.show();
cus2.show();

cus2.name="bhavani";


cus1.show();
cus2.show();

}}






BUBBLE SORT IN JAVA









class Test{


static void bubblesort(int arr[]) {
int temp=0;

for(int i=0;i<arr.length;i++) {
for(int j=1;j<arr.length-1;j++) {

if(arr[j]> arr[j+1])
{
temp= arr[j];
arr[j]= arr[j+1];
arr[j+1] = temp;
}
}}

}

public static void main(String args[]){

int arr[]= { 2,6,10,8,150,7,99};

for(int X:arr)
System.out.print(X+" ");

bubblesort(arr);


System.out.println("\n"+"AFTER SORTING");

for(int Y:arr)
{
System.out.print(Y+" ");
}

}

}

String Reversal by word by word

import java.util.*;
class StringintoWords{
public static void main(String args[]){

Scanner sc= new Scanner(System.in);
String str= sc.nextLine();

String words[] = str.split(" ");
String reversal="";



for(int i=0;i<words.length;i++)
{
String word= words[i];


String rev_word="";
for(int j=word.length()-1; j>0;j--)
{
rev_word=rev_word+word.charAt(j);
}

reversal=reversal+rev_word+" ";
}

System.out.println(reversal);
}}

PATTERN 24




import java.util.*;
class Pattern_24{
public static void main(String args[]){
Scanner sc= new Scanner(System.in);
System.out.println("Enter no of rows");
int n= sc.nextInt();

int r,c,blnk=0;
char ch='*';

for(r=n;r>=1;r--)
{
                for(blnk=n-1;blnk>=r;blnk--)
                {
                System.out.print(" ");
                }
                for(c=1;c<=n;c++)
                {             
                if(r==1 || r==n || c==1 || c==n)               System.out.print(ch);
                else                                                                        System.out.print(" ");
                }
System.out.println();
}

}}

PATTERN 23




import java.util.*;
class Pattern_23{
public static void main(String args[]){
Scanner sc= new Scanner(System.in);
System.out.println("Enter no of rows");
int n= sc.nextInt();

int r,c,blnk=0;
char ch='*';

for(r=1;r<=n;r++)
{
                for(blnk=n-1;blnk>=r;blnk--)
                {
                System.out.print(" ");
                }
               
                for(c=1;c<=n;c++)
                {             
                if(r==1 || r==n || c==1 || c==n)                               System.out.print(ch);
                else                                                                        System.out.print(" ");
                }


System.out.println();
}

}}

PATTERN 22



import java.util.*;
class Pattern_22{
public static void main(String args[]){
Scanner sc= new Scanner(System.in);
System.out.println("Enter no of rows");
int n= sc.nextInt();

int r,c,blnk=0;
char ch='*';

for(r=n;r>=1;r--)
{

                for(blnk=n-1; blnk>=r;blnk--)
                {
                System.out.print(" ");
                }
                for(c=1;c<=2*r-1;c++)
                {
                if(r==n || c==1 || c== (2*r-1))   System.out.print(ch);
                else        System.out.print(" ");   
                }
System.out.println();
}

}}

PATTERN 21



import java.util.*;
class Pattern_21{
public static void main(String args[]){
Scanner sc= new Scanner(System.in);
System.out.println("Enter no of rows");
int n= sc.nextInt();

int r,c,blnk=0;
char ch='*';

for(r=1;r<=n;r++)
{

                for(blnk=n-1; blnk>=r;blnk--)
                {
                System.out.print(" ");
                }
                for(c=1;c<=2*r-1;c++)
                {
                if(r==n || c==1 || c== (2*r-1))   System.out.print(ch);
                else                                                        System.out.print(" ");   
                }

System.out.println();
}

}}

PATTERN 20



import java.util.*;
class Pattern_20{
public static void main(String args[]){
Scanner sc= new Scanner(System.in);
System.out.println("Enter no of rows");
int n= sc.nextInt();

int r,c,blnk=0;
char ch='*';

for(r=n;r>=1;r--)
{
for(blnk=n-1;blnk>=r;blnk--)
{
System.out.print(" ");
}
for(c=1;c<=r;c++)
{
if(c==1 ||c==r|| r==n )                 System.out.print(ch);
else                                                        System.out.print(" ");
}
System.out.println();
}

}}

PATTERN 19




import java.util.*;
class Pattern_19{
public static void main(String args[]){
Scanner sc= new Scanner(System.in);
System.out.println("Enter no of rows");
int n= sc.nextInt();

int r,c,blnk=0;
char ch='*';

for(r=1;r<=n;r++)
{
for(blnk=n-1;blnk>=r;blnk--)
{
System.out.print(" ");
}
for(c=1;c<=r;c++)
{
if(c==1 ||c==r|| r==n ) System.out.print(ch);
else                                        System.out.print(" ");
}
System.out.println();
}

}}

PATTERN 18




import java.util.*;
class Pattern_18{
public static void main(String args[]){
Scanner sc= new Scanner(System.in);
System.out.println("Enter no of rows");
int n= sc.nextInt();

int r,c=0;
char ch='*';

for(r=n;r>=1;r--)
{
for(c=1;c<=r;c++)
{
if(c==1 ||c==r|| r==n )                 System.out.print(ch);
else                                                        System.out.print(" ");
}
System.out.println();
}


}}

PATTERN 17




import java.util.*;
class Pattern_17{
public static void main(String args[]){
Scanner sc= new Scanner(System.in);
System.out.println("Enter no of rows");
int n= sc.nextInt();

int r,c=0;
char ch='*';

for(r=1;r<=n;r++)
{
for(c=1;c<=r;c++)
{
if(c==1 ||c==r|| r==n )                 System.out.print(ch);
else                                                        System.out.print(" ");
}
System.out.println();
}


}}

PATTERN 16




import java.util.*;
class Pattern_16{
public static void main(String args[]){
Scanner sc= new Scanner(System.in);
System.out.println("Enter no of rows");
int n= sc.nextInt();

int r,c=0;
char ch='*';

for(r=1;r<=n;r++)
{
for(c=1;c<=n;c++)
{
if(r==1 || r==n || c==1 || c==n)                               System.out.print(ch);
else                                                                        System.out.print(" ");
}
System.out.println();
}


}}

PATTERN 15




import java.util.*;
class Pattern_15{
public static void main(String args[]){
Scanner sc= new Scanner(System.in);
System.out.println("Enter no of rows");
int n= sc.nextInt();

int r,c,blnk=0;
char ch='*';

for(r=n;r>=1;r--)
{
                for(blnk=n-1;blnk>=r;blnk--)
                {
                System.out.print(" ");
                }
               
                for(c=1;c<=n;c++)
                {
                System.out.print(ch);
                }
System.out.println();
}

}}

PATTERN 14



import java.util.*;
class Pattern_14{
public static void main(String args[]){
Scanner sc= new Scanner(System.in);
System.out.println("Enter no of rows");
int n= sc.nextInt();

int r,c,blnk=0;
char ch='*';

for(r=1;r<=n;r++)
{
                for(blnk=n-1;blnk>=r;blnk--)
                {
                System.out.print(" ");
                }
               
                for(c=1;c<=n;c++)
                {
                System.out.print(ch);
                }
System.out.println();
}

}}

PATTERN 13





import java.util.*;
class Pattern_13{
public static void main(String args[]){
Scanner sc= new Scanner(System.in);
System.out.println("Enter no of rows");
int n= sc.nextInt();

int r,c,blnk=0;
char ch='*';

for(r=n;r>=1;r--)
{
                for(blnk=n-1;blnk>=r;blnk--)
                {
                System.out.print(" ");
                }
                for(c=1;c<=r;c++)
                {
                System.out.print(ch);
                }
System.out.println();
}

}}

PATTERN 12



import java.util.*;
class Pattern_12{
public static void main(String args[]){
Scanner sc= new Scanner(System.in);
System.out.println("Enter no of rows");
int n= sc.nextInt();

int r,c,blnk=0;
char ch='*';

for(r=1;r<=n;r++)
{
                for(blnk=n-1;blnk>=r;blnk--)
                {
                System.out.print(" ");
                }
                for(c=1;c<=r;c++)
                {
                System.out.print(ch);
                }
System.out.println();
}

}}

PATTERN 11




import java.util.*;
class Pattern_11{
public static void main(String args[]){
Scanner sc= new Scanner(System.in);
System.out.println("Enter no of rows");
int n= sc.nextInt();

int r,c,blnk=0;
char ch='*';

for(r=n;r>=1;r--)
{

                for(blnk=n-1; blnk>=r;blnk--)
                {
                System.out.print(" ");
                }

                for(c=1;c<=2*r-1;c++)
                {
                System.out.print(ch+" ");
                }
System.out.println();
}

}}

PATTERN 10




import java.util.*;
class Pattern_10{
public static void main(String args[]){
Scanner sc= new Scanner(System.in);
System.out.println("Enter no of rows");
int n= sc.nextInt();

int r,c,blnk=0;
char ch='*';

for(r=1;r<=n;r++)
{

                for(blnk=n-1; blnk>=r;blnk--)
                {
                System.out.print(" ");
                }

                for(c=1;c<=2*r-1;c++)
                {
                System.out.print(ch+" ");
                }
System.out.println();
}

}}