Thursday, June 29, 2017

Registration Form JSwing

Code:


import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;

class form {
JFrame fr;
JPanel ptitle,pnam,peml,pmob,ppas,pgen,padr,psub,pall;
JButton btn;
JLabel ltitle,lnam,leml,lmob,lpas,lgen,ladr;
JTextField tnam,teml,tmob;
JTextArea tadr;
JPasswordField tpas;
JRadioButton male,fem;
ButtonGroup bg;
JDialog d;
Customers c=new Customers();

form(){
fr=new JFrame("Nearur Registration");

ptitle=new JPanel();
pnam=new JPanel();
peml=new JPanel();
pmob=new JPanel();
ppas=new JPanel();
pgen=new JPanel();
padr=new JPanel();
psub=new JPanel();
pall=new JPanel();

ltitle =new JLabel("Registration Form");
lnam =new JLabel("Enter Your Full Name:");
leml =new JLabel("Enter Your Email:");
lmob =new JLabel("Enter Your Mobile Number:");
lpas =new JLabel("Enter Your Password:");
lgen =new JLabel("Gender:");
ladr =new JLabel("Enter Your Address:");

tnam=new JTextField(16);
teml=new JTextField(16);
tmob=new JTextField(10);
tadr=new JTextArea(3,20);
tpas=new JPasswordField(16);

btn =new JButton("Submit");

male=new JRadioButton("Male");
fem=new JRadioButton("Female");
bg=new ButtonGroup();


d=new JDialog();
}

void generate() {

ptitle.add(ltitle);

pnam.add(lnam);
pnam.add(tnam);

peml.add(leml);
peml.add(teml);

pmob.add(lmob);
pmob.add(tmob);

ppas.add(lpas);
ppas.add(tpas);

pgen.add(lgen);
bg.add(male);
bg.add(fem);
pgen.add(male);
pgen.add(fem);

padr.add(ladr);
padr.add(tadr);

psub.add(btn);

pall.add(ptitle);
pall.add(pnam);
pall.add(peml);
pall.add(pmob);
pall.add(ppas);
pall.add(pgen);
pall.add(padr);
pall.add(psub);

GridLayout layout = new GridLayout(8, 1);
pall.setLayout(layout);

fr.add(pall);

fr.pack();
fr.setVisible(true);

btn.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {
save();
}

});

}
void save() {
c.name=tnam.getText();
c.mobile=Long.parseLong(tmob.getText());
c.password=tpas.getPassword().toString();
c.Address=tadr.getText();
c.email=teml.getText();
if(male.isSelected()) {
c.g='M';
}
else {
c.g='F';
}
File f=new File("C:/Users/mrdis/Desktop","Customers.txt");
FileOutputStream w=null;
ObjectOutputStream oos=null;
try {
w = new FileOutputStream(f);
oos=new ObjectOutputStream(w);
oos.writeObject(c);
} catch (Exception e) {

e.printStackTrace();
}finally {
try {
w.close();
oos.close();
}catch(Exception e) {


}
}


JOptionPane.showMessageDialog(fr,"Successful");
tnam.setText("");
tpas.setText("");
teml.setText("");
tadr.setText("");
tmob.setText("");
bg.clearSelection();
}
}
public class NearurRegistration {

public static void main(String[] args) {
new form().generate();
       
}

}


Output:

Wednesday, June 28, 2017

Notepad Using Java

Code:

package com.nearur;

import java.awt.FileDialog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.print.PrinterException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTextArea;

class notep{
JFrame fr;
JTextArea a;
JMenuBar mb;
JMenu File,Edit,Format,View,Help;
JMenuItem New,Exit,Open,Save,Cut,Copy,Paste,Print,Delete;
FileDialog d,op;

       notep() {
fr=new JFrame("NotePad");
d=new FileDialog(fr,"Save",FileDialog.SAVE);
op=new FileDialog(fr,"Save",FileDialog.LOAD);
a=new JTextArea();

File=new JMenu("File");
Edit=new JMenu("Edit");
Format=new JMenu("Format");
View=new JMenu("View");
Help=new JMenu("Help");

New=new JMenuItem("New");
Exit=new JMenuItem("Exit");
Open=new JMenuItem("Open");
Save=new JMenuItem("Save");
Print=new JMenuItem("Print");
Cut=new JMenuItem("Cut");
Copy=new JMenuItem("Copy");
Paste=new JMenuItem("Paste");
Delete=new JMenuItem("Delete");

mb=new JMenuBar();

File.add(New);
File.add(Open);
File.add(Save);
File.add(Print);
File.add(Exit);

Edit.add(Cut);
Edit.add(Copy);
Edit.add(Paste);
Edit.add(Delete);

mb.add(File);
mb.add(Edit);
mb.add(Format);
mb.add(View);
mb.add(Help);

fr.setJMenuBar(mb);
Cut.setEnabled(false);
Copy.setEnabled(false);
Paste.setEnabled(false);
Save.setEnabled(false);
Delete.setEnabled(false);
Print.setEnabled(false);

New.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

fr.add(a);
fr.setTitle("Untitled-NotePad");
fr.setVisible(true);
Save.setEnabled(true);
Cut.setEnabled(true);
Copy.setEnabled(true);
Paste.setEnabled(true);
Delete.setEnabled(true);
Print.setEnabled(true);
}

});

Exit.addActionListener(new ActionListener() {


public void actionPerformed(ActionEvent arg0) {

fr.dispose();

}

});

Cut.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

try {
a.print();
} catch (PrinterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

});


Cut.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

a.cut();
}

});
Copy.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {
a.copy();
}

});
Paste.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

a.paste();
}

});
Delete.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {
a.replaceSelection("");
}

});
Save.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {
String data=a.getText();
d.setVisible(true);
File f =new File(d.getDirectory(),d.getFile());
FileOutputStream r=null;
try {

r=new FileOutputStream(f,true);
char a[]=data.toCharArray();
int i=0;
while(i<a.length){
r.write((int)a[i]);
i++;
}
r.close();
                                       fr.setTitle(d.getFile()+" Notepad");
} catch (Exception e) {

e.printStackTrace();
}
}

});
Open.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {
op.setVisible(true);
File f =new File(op.getDirectory(),op.getFile());
FileInputStream in=null;
try {
in=new FileInputStream(f);
int ch=0;
StringBuffer line=new StringBuffer();
while((ch=in.read())!=-1) {
line.append((char)ch+"");
}
a.setText(line.toString());
fr.add(a);
fr.setTitle(op.getFile()+" Notepad");
fr.setVisible(true);
in.close();
}catch(Exception e ){

}
}

});


fr.pack();
fr.setSize(300,300);
fr.setVisible(true);
}
}
public class Notepad {

public static void main(String[] args) {
new notep();
}

}
Output:

CountDown Timer

Code:


class Timer implements Runnable{
Thread t;

int n;

public Timer(int x) {
t=new Thread(this);
n=x;
}


public void run() {
for(int i=n;i>0;i--) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("Error: "+e);
}
System.out.println(i);
}

}

}
public class CountDown {


public static void main(String[] args) {
System.out.println("--Main Started--");

Timer t=new Timer(10);
t.t.start();
try {
t.t.join();
}catch(Exception e) {
System.out.println(e);
}


System.out.println("Happy New Year\n--Main Finished--");
}

}

Output:

Stack Push Pop Threads

Code:

class stack {
int[] a;
int top=-1;

public stack(int size) {
a=new int[size];
}
void push(int x){
if(top==a.length-1){
System.out.println("Stack Full");
}
else{
a[++top]=x;
}
}
int pop(){
if(top<0){
System.out.println("Empty Stack");
return 0;
}
else {
return a[top--];
}
}
}
class Pop extends Thread{
stack sRef;
Pop(stack s){
sRef=s;
}
public void run(){
synchronized(sRef){

try {
sRef.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}


for(int i=0;i<sRef.a.length;i++){
System.out.println("Value: "+sRef.pop());
}

}
}
}
class Push extends Thread{

stack sRef;

public Push(stack s) {
sRef=s;
}
public void run(){
synchronized(sRef){
for(int i=0;i<sRef.a.length;i++){
sRef.push(i);
}
sRef.notify();
}
}
}
public class StackDemo {

public static void main(String[] nt) {
System.out.println("--Main Started--");
stack s=new stack(23);

Pop pRef=new Pop(s);
Push puRef=new Push(s);

pRef.start();
puRef.start();

try {

puRef.join();
pRef.join();
} catch (InterruptedException e) {
e.printStackTrace();
}

System.out.println("--Main Finished--");

}

}

Output:

Tuesday, June 27, 2017

Source Code Analysis

Following code will print the number and names of Objects,Classes,Interfaces created in java source file.

Code:


import java.io.*;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

public class SourceCode {
public static void main(String[] nt){
File src=null;
FileReader fin=null;
BufferedReader br=null;
int o=0,c=0,i=0;
try {

Date d=new Date();
long start=d.getTime();
                        src=new File("F:/Session14/src/com/auribises","CollectionsDemo.java");
fin=new FileReader(src);
br=new BufferedReader(fin);
String line="";
HashMap<String,Integer> mp=new HashMap<String,Integer>();
StringBuffer data=new StringBuffer();
StringBuffer datai=new StringBuffer();

while((line=br.readLine())!=null) {
if(line.contains("new")){
o++;
int k=line.indexOf("new");
int l=line.indexOf("(",k+3);
if(mp.containsKey(line.substring(k+3,l))){
int x=mp.get(line.substring(k+3,l));
mp.put(line.substring(k+3,l),++x);
}
else{
mp.put(line.substring(k+3,l),1);
}
}
if(line.contains("class")){
c++;
int k=line.indexOf("class");
int l=line.indexOf("{",k+6);
data.append(line.substring(k+6,l)+"\n");
}
if(line.contains("interface")){
i++;
int k=line.indexOf("interface");
int l=line.indexOf("{",k+10);
datai.append(line.substring(k+10,l)+"\n");
}
}
System.out.println(src.getName());

System.out.println("\n--------------Objects-----------------");
Set<String> k=mp.keySet();
Iterator<String> it=k.iterator();
while(it.hasNext()){
String x=it.next();
int value=mp.get(x);
System.out.println(x+" : "+value);
}
System.out.println("No. of Objects created:"+o);

System.out.println("\n--------------Classes------------------");
System.out.println(data);
System.out.println("No. of Classes created:"+c);

System.out.println("\n--------------Interfaces---------------");
System.out.println(datai);
System.out.println("No. of Interfaces created:"+i+"\n");

d=new Date();
long stop=d.getTime();
System.out.println("Time Taken:"+(stop-start)+" miliseconds");

} catch (Exception e) {
System.out.println("Some Error"+e);
}finally {
try {
fin.close();
br.close();
} catch (Exception e2) {
System.out.println("Error:"+e2);
}
}

}
}

Output:

Monday, June 26, 2017

MyScanner Class

Following Code will define a class MyScanner to take input from user using InputStreamReader ..

Code:


import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

class MyScanner {
InputStreamReader i;
BufferedReader br;
public MyScanner(InputStream x) {
i=new InputStreamReader(x);
br=new BufferedReader(i);
}
public int nextInt() {
int a=0;
try {
a =Integer.parseInt(br.readLine());
} catch (Exception e) {
System.out.println("Error:"+e);
}
return a;
}
public float nextFloat() {
float a=0;
try {
a =Float.parseFloat(br.readLine());
} catch (Exception e) {
System.out.println("Error:"+e);
}
return a;
}
public double nextDouble() {
double a=0;
try {
a =Double.parseDouble(br.readLine());
} catch (Exception e) {
System.out.println("Error:"+e);
}
return a;
}
public long nextLong() {
long a=0;
try {
a =Long.parseLong(br.readLine());
} catch (Exception e) {
System.out.println("Error:"+e);
}
return a;
}
public String nextLine() {
String a=null;
try {
a =br.readLine();
} catch (Exception e) {
System.out.println("Error:"+e);
}
return a;
}
public String next() {
String a="";
int ch=0;
try {
while(true) {
ch=br.read();
if(ch==32 || ch==10){
break;
}
a=a.concat(""+(char)ch);
}

} catch (Exception e) {
System.out.println("Error:"+e);
}
return a;
}

/*class MyScanner1 extends Scanner {
// error because Scanner is final class
}*/

}
public class MyScannerDemo {
public static void main(String[] nt) {
MyScanner in=new MyScanner(System.in);
//Character
char ch=in.next().charAt(0);
System.out.println("Entered Character: "+ch);
//Integer
int i=in.nextInt();
System.out.println("Entered Integer: "+i);


}
}

Output:

MyInteger Wrapper Class

Following Code define a Class MyInteger Which acts as a Wrapper class for integer :


Code:

class MyInteger {
int i;
MyInteger(int i){
this.i=i;
}
int intValue(){
return i;
}

public String toString() {
return String.valueOf(i);
}

}
/*class MyInteger1 extends Integer {
//error because Integer is a final class
}*/
public class MyIntegerDemo {
public static void main(String[] nt) {
MyInteger mi=new MyInteger(10);
int j = mi.intValue();
System.out.println(mi);
System.out.println(j);
}
}

Output: