Monday, June 12, 2017

Group Name Change In ChatApps

Following Code will give u a small idea how that change takes place:

Code:


public class User {
    int i;
String name;
long mobile;
Group[] g=new Group[10];

User(){   //Default Constructor
i=0;
name="No Name";
mobile=0000000000;
}

User(String n,long m){   //Parameterized Constructor
i=0;
name=n;
mobile=m;
}

void createGroup(String gn)
{
   g[i++]=new Group(gn,this);
}
    
void changeGname(String gn,int i)
{
g[i].Gname=gn;
}
}


public class User {
    int i;
String name;
long mobile;
Group[] g=new Group[10];
User(){   //Default Constructor
i=0;
name="No Name";
mobile=0000000000;
}
User(String n,long m){   //Parameterized Constructor
i=0;
name=n;
mobile=m;
}
void createGroup(String gn)
{
   g[i++]=new Group(gn,this);
}
    
void changeGname(String gn,int i)
{
g[i].Gname=gn;
}
}


public class Main {

public static void main(String[] nt) {
User u1=new User("Dishant",9023074222l);
User u2=new User("Garvit",8284010455l);
User u3=new User("Piyush",9815445194l);
User u4=new User("Nikhil",9815215752l);
User u5=new User("Navneet",9888321686l);
u1.createGroup("Brothers");
User[] x={u2,u3,u4,u5};
u1.g[0].addUsers(x);
u4.g[0].showGname();
u1.changeGname("For", 0);
u2.g[0].showGname();
u2.changeGname("Life", 0);
u5.g[0].showGname();
u3.changeGname("Yeahhh", 0);
u1.g[0].showGname();
System.out.println("------------------");
u3.g[0].showUsers();
}

}

Output::

Caesar Cipher

Caesar Cipher:The Caesar cipher, also known as a shift cipher, is one of the simplest forms of encryption. It is a substitution cipher where each letter in the original message (called the plaintext) is replaced with a letter corresponding to a certain number of letters up or down in the alphabet.
In this way, a message that initially was quite readable, ends up in a form that can not be understood at a simple glance.
For example, here's the Caesar Cipher encryption of a message, using a right shift of 3.
Plaintext:  
THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG
Ciphertext: 
QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD


Following Code will Encrypt Plain Text Using Caesar Cipher :

Code:
import java.util.Scanner;
public class Caesar 
{
    
public static void main(String[] nt) 
{
Scanner in =new Scanner(System.in);
System.out.print("Enter Plain Text: ");
String s=in.nextLine();
char[] a=new char[s.length()];
char[] b={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
s=s.toUpperCase();
for(int i=0;i<s.length();i++)
{
a[i]=s.charAt(i);
}
for(int j=0;j<a.length;j++)
{  if(a[j]!=' ')
{for(int i=0;i<b.length;i++)
{
if(a[j]==b[i])
{
a[j]=b[(i+3)%26];
break;
}
}}
}
System.out.print("     Cipher Text: ");
for(char d: a)
System.out.print(d);
System.out.println();

for(int i=0;i<a.length;i++)
{
for(int j=0;j<b.length;j++)
{
if(a[i]==b[j])
{
if(j>=3)
{
a[i]=b[(j-3)%26];
}
else
{
a[i]=b[(23+j)%26];
}
break;
}
}
}
System.out.print("     Plain  Text:");
for(char j:a){
System.out.print(j);
}
System.out.println();
}


}

Output:

Thursday, June 8, 2017

Reading a MD array Using Enhanced For Loop

Following Code will read 2D and 3D arrays using for each loop

Code:


public class Java3 
{
public static void main(String[] nt)
{   

int[][] nti={
       {7,6,2,3},
       {6,7},
       {2,3},
       {7,6},
       {7,5},
       {7,5,6,7}
           };
System.out.println("nti :"+nti);      //address of nti[0]
System.out.println("nti length :"+nti.length);     // length of array i.e. no of arrays inside

for(int[] i : nti)
{
for(int j : i)
{
System.out.print(j+" ");
}
System.out.println();
}
System.out.println("---------------------");
int[][][] ntii={
       {
        {7,6,2,3},
        {6,7},
        {2,3},
        {7,6},
        {7,5,6,7}
       },
       { {7,6,2,3},
       {6,7},
       {2,3},
       {7,6},
       {7,5,6,7}
   }
           };
System.out.println("ntii :"+ntii);
System.out.println("ntii length :"+ntii.length);

for(int[][] i : ntii)
{
for(int[] j : i)
{
for(int k : j)
System.out.print(k+" ");
System.out.println();
}
System.out.println();
}
   }
}

Output:

Greatest of Five Numbers (Simple Approach)

Following code will find largest numbers out of five numbers 

Code:

public class Java2 
{
public static void main(String[] nt)
{
int a=7,b=6,c=2,d=3,e=7;
int[] nti=new int[]{a,b,c,d,e};
int[] temp=new int[5];
int max =nti[0];
for(int i=1;i<nti.length;i++)
{
if(max<nti[i])
{
max=nti[i];
}
}
int j=0;
for(int i=0;i<nti.length;i++)
{
if(nti[i]==max)
{
temp[j]=i;
j++;
}
}

if(j==1)
{
System.out.print("Greatest is: ");
}
else
{   System.out.print("Greatest are: ");
}
for(int i=0;i<j;i++)
{
 switch(temp[i])
 {
  case 0:
  System.out.print("A ");
  break;
  case 1:
  System.out.print("B ");
  break;
  case 2:
  System.out.print("C ");
  break;
  case 3:
  System.out.print("D ");
  break;
  case 4:
  System.out.print("E ");
  break;
 }
}
}
}

Output:

Copy Array Data

Following code will copy the Array Data after changing size with in code:


Code:


public class ArrayDemo {

public static void main(String[] args) 
{
int[] nt=new int[]{7,6,2,3};
  for(int i : nt)
  {
  System.out.print(i+" ");
  }
System.out.println();
System.out.println("--------------------");   
  int[] temp=nt;     //new reference variable temp
     nt =new int[8];
     int j=0;
      for(int i : temp)
      {
      nt[j++]=i;          //copying data
      }
      for(int i : nt)
  {
  System.out.print(i+" ");
  }
}

}

Output: 

Wednesday, June 7, 2017

Greatest of Five Numbers

Following Program will find the Greatest Number Out of Five Numbers and Consider Equality also:

Code:

public class NT {

public static void main(String[] nt)
{
int a=76,b=23,c=75,d=67,e=76;
if(a>b)
{
if(a>c)
{
if(a>d)
{
if(a>e)
{
System.out.println("A is Greatest");
}
else if(a==e)
{
System.out.println("A,E are Greatest and Equal");
}
else
{
System.out.println("E is Greatest");
}
}
else if(a==d)
{
if(a>e)
{
System.out.println("A,D are Greatest and Equal");
}
else if(a==e)
{
System.out.println("A,D,E are Greatest and Equal");
}
else
{
System.out.println("E is Greatest");
}  
}
else
{
if(d>e)
{
System.out.println("D is Greatest ");
}
else if(d==e)
{
System.out.println("D,E are Greatest and Equal");
}
else
{
System.out.println("E is Greatest");
}
}

}
else if(a==c)
{
if(a>d)
{
if(a>e)
{
System.out.println("A,C are Greatest and Equal");
}
else if(a==e)
{
System.out.println("A,C,E are Greatest and Equal");
}
else
{
System.out.println("E is Greatest");
}
}
else if(a==d)
{
if(a>e)
{
System.out.println("A,C,D are Greatest and Equal");
}
else if(a==e)
{
System.out.println("A,C,D,E are Greatest and Equal");
}
else
{
System.out.println("E is Greatest");
}  
}
else
{
if(d>e)
{
System.out.println("D is Greatest ");
}
else if(d==e)
{
System.out.println("D,E are Greatest and Equal");
}
else
{
System.out.println("e is greatest");
}
}
}
else
{
if(c>d)
{
if(c>e)
{
System.out.println("C is Greatest");
}
else if(c==e)
{
System.out.println("C,E are Greatest and Equal");
}
else
{
System.out.println("E is Greatest");
}
}
else if(c==d)
{
if(c>e)
{
System.out.println("C,D are Greatest and Equal");
}
else if(c==e)
{
System.out.println("C,D,E are Greatest and Equal");
}
else
{
System.out.println("E is Greatest");
}  
}
else
{
if(d>e)
{
System.out.println("D is Greatest ");
}
else if(d==e)
{
System.out.println("D,E is Greatest and Equal");
}
else
{
System.out.println("E is Greatest");
}
}
}
}

else if(a==b)
{
if(a>c)
{
if(a>d)
{
if(a>e)
{
System.out.println("A,B are Greatest and Equal");
}
else if(a==e)
{
System.out.println("A,B,E is Greatest and Equal");
}
else
{
System.out.println("E is Greatest");
}
}
else if(a==d)
{
if(a>e)
{
System.out.println("A,B,D is Greatest and Equal");
}
else if(a==e)
{
System.out.println("A,B,D,E are Greatest and Equal");
}
else
{
System.out.println("E is Greatest");
}  
}
else
{
if(d>e)
{
System.out.println("D is Greatest ");
}
else if(d==e)
{
System.out.println("D,E are Greatest and Equal");
}
else
{
System.out.println("E is Greatest");
}
}

}
else if(a==c)
{
if(a>d)
{
if(a>e)
{
System.out.println("A,B,C are Greatest and Equal");
}
else if(a==e)
{
System.out.println("A,B,C,E are Greatest and Equal");
}
else
{
System.out.println("E is Greatest");
}
}
else if(a==d)
{
if(a>e)
{
System.out.println("A,B,C,D are Greatest and Equal");
}
else if(a==e)
{
System.out.println("A,B,C,D,E are Greatest and Equal");
}
else
{
System.out.println("E is Greatest");
}  
}
else
{
if(d>e)
{
System.out.println("D is Greatest ");
}
else if(d==e)
{
System.out.println("D,E are Greatest and Equal");
}
else
{
System.out.println("E is Greatest");
}
}
}
else
{
if(c>d)
{
if(c>e)
{
System.out.println("C is Greatest");
}
else if(c==e)
{
System.out.println("C,E are Greatest and Equal");
}
else
{
System.out.println("E is Greatest");
}
}
else if(c==d)
{
if(c>e)
{
System.out.println("C,D are Greatest and Equal");
}
else if(c==e)
{
System.out.println("C,D,E are Greatest and Equal");
}
else
{
System.out.println("E is Greatest");
}  
}
else
{
if(d>e)
{
System.out.println("D is Greatest ");
}
else if(d==e)
{
System.out.println("D,E are Greatest and Equal");
}
else
{
System.out.println("E is Greatest");
}
}
}
}
else
{
if(b>c)
{
if(b>d)
{
if(b>e)
{
System.out.println("B is Greatest");
}
else if(b==e)
{
System.out.println("B,E are Greatest and Equal");
}
else
{
System.out.println("E is Greatest");
}
}
else if(b==d)
{
if(b>e)
{
System.out.println("B,D are Greatest and Equal");
}
else if(b==e)
{
System.out.println("B,D,E are Greatest and Equal");
}
else
{
System.out.println("E is Greatest");
}  
}
else
{
if(d>e)
{
System.out.println("D is Greatest ");
}
else if(d==e)
{
System.out.println("D,E are Greatest and Equal");
}
else
{
System.out.println("E is Greatest");
}
}

}
else if(b==c)
{
if(b>d)
{
if(b>e)
{
System.out.println("B,C are Greatest and Equal");
}
else if(b==e)
{
System.out.println("B,C,E are Greatest and Equal");
}
else
{
System.out.println("E is Greatest");
}
}
else if(b==d)
{
if(b>e)
{
System.out.println("B,C,D are Greatest and Equal");
}
else if(b==e)
{
System.out.println("B,C,D,E are Greatest and Equal");
}
else
{
System.out.println("E is Greatest");
}  
}
else
{
if(d>e)
{
System.out.println("D is Greatest ");
}
else if(d==e)
{
System.out.println("D,E are Greatest and Equal");
}
else
{
System.out.println("E is Greatest");
}
}
}
else
{
if(c>d)
{
if(c>e)
{
System.out.println("C is Greatest");
}
else if(c==e)
{
System.out.println("C,E are Greatest and Equal");
}
else
{
System.out.println("E is Greatest");
}
}
else if(c==d)
{
if(c>e)
{
System.out.println("C,D is Greatest and Equal");
}
else if(c==e)
{
System.out.println("C,D,E are Greatest and Equal");
}
else
{
System.out.println("E is Greatest");
}  
}
else
{
if(d>e)
{
System.out.println("D is Greatest ");
}
else if(d==e)
{
System.out.println("D,E are Greatest and Equal");
}
else
{
System.out.println("E is Greatest");
}
}
}
}
}
}

Output:


IT'S SIMPLEST APPROACH IS Click Here

Saturday, April 29, 2017

Registration Form Java

Code:

import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
public class Formm extends Frame
{   Label [] l=new Label[8];
    TextField[] t=new TextField[6];
    TextArea a=new TextArea();
    CheckboxGroup p=new CheckboxGroup();
    Checkbox c1=new Checkbox("Male",p,true);
    Checkbox c2=new Checkbox("Female",p,false);
    Button s=new Button("Submit");
    Button r=new Button("Reset");
    int y=30;
    Connection cp;
    Statement st;
    Formm()
    {
        setLayout(null);
        setVisible(true);
        for(int i=0;i<8;i++)
        {
            l[i]=new Label();
            l[i].setBounds(30,y,100,30);
            add(l[i]);
            y+=40;
        }
        y=30;
        for(int i=0;i<6;i++)
        {
            t[i]=new TextField();
            t[i].setBounds(130,y,100,30);
            add(t[i]);
            y+=40;
        }
        a.setBounds(130,310,150,80);
        add(a);
        s.setBounds(50,400,50,30);
        add(s);
        r.setBounds(120,400,50,30);
        add(r);
        c1.setBounds(130,270,50,30);
        add(c1);
        c2.setBounds(180,270,60,30);
        add(c2);
        l[0].setText("Name");
        l[1].setText("Email");
        l[2].setText("Password");
        l[3].setText("Pincode");
        l[4].setText("DOB");
        l[5].setText("Mobile Number");
        l[6].setText("Gender");
        l[7].setText("Address");
        Label h=new Label("City:");
        h.setBounds(300,310,50,20);
        add(h);
        Choice c=new Choice();
        c.setBounds(350,310,100,30);
        c.add("Ludhiana");
        c.add("Jalandhar");
        c.add("Bhatinda");
        c.add("Amritsar");
        c.add("Hoshiarpur");
        add(c);
        this.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e)
            {
                System.exit(0);
            }
        });
        try
        {
         
            Class.forName("com.mysql.jdbc.Driver");
            cp=DriverManager.getConnection("jdbc:mysql://localhost/college","root","password");
            st=cp.createStatement();
        }
        catch(Exception e)
        {
            System.out.println("Error"+e);
        }
        r.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                for(int i=0;i<6;i++)
                {
                    t[i].setText(" ");
                }
                a.setText(" ");
            }
        });
        s.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e)
            {
           
             try
        {
            st.executeUpdate("Insert into form values('"+t[0].getText()+"','"+t[1].getText()+"','"+t[2].getText()+"','"+t[3].getText()+"','"+t[4].getText()+"','"+a.getText()+"','"+t[5].getText()+"','"+p.getSelectedCheckbox().getLabel()+"','"+c.getSelectedItem()+",) ");
        }
        catch(Exception ex)
        {
            System.out.println("Error Occured"+ex);
        }
             JOptionPane.showMessageDialog(null,"Record Entered");
             for(int i=0;i<6;i++)
             {
                 t[i].setText("");
             }
             a.setText("");
            }
        });
        setSize(500,500);  
    }
    public static void main(String[] nt)
    {
        new Formm();
    }
}

Output::