Skip to main content

Posts

Showing posts from 2011

Java Sample: Working with TextField

import java.awt.*; import java.awt.event.*; import java.applet.*; public class JTextFieldTest extends Applet implements ActionListener { TextField name,pass; public void init() { Label nameL=new Label("User Name :: ",Label.RIGHT); Label passL=new Label("Password  :: ",Label.RIGHT); name=new TextField(10); pass=new TextField(10); pass.setEchoChar('*'); add(nameL); add(name); add(passL); add(pass); name.addActionListener(this); pass.addActionListener(this); } public void actionPerformed(ActionEvent ae) { repaint(); } public void paint(Graphics g) { g.drawString("USER NAME :: "+name.getText(),6,60); g.drawString("PASSWORD IS :: "+pass.getText(),6,80); } }

Java Sample : Applet with Checkbox control

//Java ItemListener  import java.awt.*; import java.applet.*; import java.awt.event.*; public class JCheckBox extends Applet implements ItemListener { String msg=""; Checkbox winXP,winVista,Solaris,Mac; public void init() { winXP=new Checkbox("Windows XP"); winVista=new Checkbox("Windows Vista"); Solaris=new Checkbox("Solaris"); Mac=new Checkbox("Mac"); add(winXP); add(winVista); add(Solaris); add(Mac); winXP.addItemListener(this); winVista.addItemListener(this); Solaris.addItemListener(this); Mac.addItemListener(this); } public void itemStateChanged(ItemEvent ie) { repaint(); } public void paint(Graphics g) { msg="Current State :: "; g.drawString(msg,6,80); msg="Windows XP :: "+winXP.getState(); g.drawString(msg,6,100); msg="Windows Vista :: "+winVista.getState(); g.drawString(msg,6,120); msg="Solaris ::

C Sample: Bubble, Selection & Insertion Sort [o(n2)]

#include<stdio.h> #include<conio.h> int list[8]={82,42,49,8,92,25,59,52}; void declare() { int list[8]={82,42,49,8,92,25,59,52}; printf("\n"); } void BubbleSort() { int i,j,k; declare(); printf("\n\t\tBUBBLE SORT\n"); for(i=0;i<=7;i++) { for(j=0;j<=7;j++) { if(list[i]<list[j]) { k=list[j]; list[j]=list[i]; list[i]=k; } } printf("\nPhase :: %d",i); display(); } } void SelectionSort() { int i,j,f; declare(); printf("\n\t\tSELECTION SORT\n"); for(i=0;i<=7;i++) { for(j=i+1;j<=7;j++) { if(list[i]>list[j]) { f=list[j]; list[j]=list[i]; list[i]=f; } } printf("\nPhase :: %d",i); display(); } } void InsertionSort() { int i,j,k; declare(); printf("\n\t\tINSERTION SORT\n"); for(i=0;i<=7;i++) { for(j=0;j<=i;j++) { if(list[j]>list[i]) { k=list[i]

C Sample: String Comparison In C Language

#include<stdio.h> #include<conio.h> #include<string.h> main() { char str1[30],str2[30]; clrscr(); printf("\nEnter 1st String"); gets(str1); printf("\nEnter 2nd String"); gets(str2); if(compare(*str1,*str2)==1) { printf("\nStrings are not same"); }else { printf("\nStrings are same"); } getch(); } int compare(char *str1,char *str2) { while(*str1!='\0' || *str2!='\0') { if(*str1==*str2) { str1++;str2++; }else{return(1);} } return(0); }

C Sample: String Concatenation In C Language

#include<stdio.h> #include<conio.h> #include<string.h> main() { char str1[30],str2[30]; printf("\nEnter 1st String"); gets(str1); printf("\nEnter 2nd String"); gets(str2); concat(str1,str2); printf("Output :: %s",str1); getch(); } concat(char *str1,char *str2) { while(*str1!='\0') str1++;      //Points to end of line while(*str2!='\0') { *str1=*str2; str1++; str2++; }*str1='\0'; }

Java Sample: Super keyword in java

class Jsuper { public void show() { System.out.println("\nThis is from SUPER class"); } } class Jsub extends Jsuper { public void show() { System.out.println("\nThis is from SUB class"); super.show(); // calling the function from parent class } } class JsuperTest { public static void main(String args[]) { Jsub ob=new Jsub(); ob.show(); } }

Java Sample: Method Overriding, Overloading and Constructor, this keyword

import java.io.*; class A { public int sum(int i,int j) { return(i+j); } } class B extends A { public float sum(float i,float j) // Method Overriding { return(i+j); } B() // Constructor is using to run SUM method by THIS keyword { System.out.println(this.sum(22.95F,66.52F)); } } class Jtest { public static void main(String args[]) { A oA=new A(); System.out.println(oA.sum(6,5)); B oB=new B(); System.out.println(oB.sum(90.50F,66.52F)); // Overring Is Calling... System.out.println(oB.sum(22,44)); // Method is Overloading.... //oB.goAction(); } }

Java Sample: Concatenation of two file's content into another

import java.io.*; class jConcatFiles {     public static void main(String args[])     {         try         {                     FileInputStream file1=null;             FileInputStream file2=null;             SequenceInputStream file3=null;             file1=new FileInputStream("City.txt");             file2=new FileInputStream("Output.txt");             file3=new SequenceInputStream(file1,file2);             BufferedInputStream inBuffer=new BufferedInputStream(file3);             BufferedOutputStream outBuffer=new BufferedOutputStream(System.out);             int ch;             while((ch=inBuffer.read())!=-1)             {                 outBuffer.write((char)ch);             }        }catch(Exception e){}              } }

Java Sample: Reading & Writing Operation using a single file

import java.io.*; import java.math.*; class jReadWrite {   public static void main(String args[])     {         DataInputStream dis=null;         DataOutputStream dos=null;         File inFile=new File("rand.txt");         try //Writing Content into File         {             dos=new DataOutputStream(new FileOutputStream(inFile));             for(int i=0;i<=20;i++)             {                 dos.writeInt((int)i);             }         }catch(Exception e){}                  try //Reading Content from File         {             dis=new DataInputStream(new FileInputStream(inFile));             for(int i=0;i<=20;i++)             {                 int n=dis.readInt();                 System.out.print(n+" ");             }         }catch(Exception ex){}                  try // Closing File         {             dis.close();             dos.close();         }catch(Exception ey){}     } }

Sample: File Operation In JAVA

1. Transfer File Content One to Another Code>> import java.io.*; class jFile {     public static void main(String args[])     {         File inFile=new File("Input.txt");//Existing File         File outFile=new File("Output.txt");         FileReader ins=null;         FileWriter outs=null;         try         {             ins=new FileReader(inFile);             outs=new FileWriter(outFile);             int ch;             while((ch=ins.read())!=-1)             {                 outs.write(ch);             }                     }catch(Exception e)         {             System.out.println(e.getMessage());         }finally         {             try{                 ins.close();                 outs.close();             }catch(Exception ex)             {}         }     } } 2. Writing Bytes to FILE Code>> import java.io.*; class jBytes {     public static void main(String args[])     {         try{         byte

Manage Threading with PRIORITY in java

class A extends Thread { public void run() { System.out.println("Thread A Started"); for(int i=1;i<=4;i++) { System.out.println("\tForm Thread A : i="+i); } System.out.println("Exit From A"); } } class B extends Thread { public void run() { System.out.println("Thread B Started"); for(int j=1;j<=4;j++) { System.out.println("\tForm Thread B : j="+j); } System.out.println("Exit From B"); } } class C extends Thread { public void run() { System.out.println("Thread K Started"); for(int k=1;k<=4;k++) { System.out.println("\tForm Thread C : k="+k); } System.out.println("Exit From C"); } } class testThread_1 { public static void main(String args[]) { A t1=new A(); B t2=new B(); C t3=new C(); t3.setPriority(Thread.MAX_PRIORITY); t2.setPriority(1); t1.setPriority(6); //System.out.printl

Implementation of Hybrid inheritance in Java

Implementation of Hybrid inheritance in Java with the help of INTERFACE CODE>> class Student {     int rollNumber;     void getNumber(int n)     {         rollNumber=n;     }     void putNumber()     {         System.out.println("Roll No :: "+rollNumber);     }   } class Test extends Student {     float part1,part2;     void getMarks(float m1,float m2)     {         part1=m1;         part2=m2;     }          void putMarks()     {         System.out.println("Marks Obtained\n");         System.out.println("PART1: "+part1);         System.out.println("PART2: "+part2);     } } interface Sports {     float sportWt=6.0F;     void putwt(); } class Results extends Test implements Sports {     float total;     public void putwt()     {         System.out.println("Sports Wt="+sportWt);     }     void display()     {         total=part1+part2+sportWt;         putNumber();         putMarks(

Sample: An applet program with button class & ActionEvent

import java.awt.*; import java.awt.event.*; import java.applet.*; public class jevent extends Applet implements ActionListener {         String msg="";         Button yes,no,maybe;         public void init()         {                 yes=new Button("YES");                 no=new Button("NO");                 maybe=new Button("UNDECIDED");                 add(yes);add(no);add(maybe);                 yes.addActionListener(this);                 no.addActionListener(this);                 maybe.addActionListener(this);         }         public void actionPerformed(ActionEvent ae)         {                 String str=ae.getActionCommand();                 if(str.equals("YES"))                 {                         msg="YOU've Clicked On YES";                 }else if(str.equals("NO"))                 {                         msg="YOU've Clicked On NO";              

Sample: Java Applet {Popup menu creation}

import java.awt.*;     import java.applet.*;     import java.awt.event.*;     public class pms extends Applet implements ActionListener { PopupMenu popup; public void init() {                 MenuItem mi;    popup = new PopupMenu("Edit");             mi = new MenuItem("Cut");             mi.addActionListener(this);    popup.add(mi);             mi = new MenuItem("Copy");             mi.addActionListener(this);    popup.add(mi);    popup.addSeparator();             mi = new MenuItem("Paste");             mi.addActionListener(this);    popup.add(mi);    add(popup); // add popup menu to applet                         enableEvents(AWTEvent.MOUSE_EVENT_MASK);     resize(200, 200);         } public void processMouseEvent(MouseEvent e) {    if (e.isPopupTrigger()) {         popup.show(e.getComponent(), e.getX(), e.getY());    }    super.processMouseEvent(e);         }         public

Symmetric Multiprocessing

SMP lets multiple CPUs in a networking device share the same board, memory, I/O and operating system. Nonetheless, each CPU in an SMP system can act independently. While one CPU handles a database lookup, others can update the database and perform other tasks, dramatically increasing the ability of the device to handle today's increasingly complex networking tasks. SMP costs relatively little. When scaling from one CPU to two, only one processor board is needed. Processing power is doubled without paying for additional support chips and without taking up an additional slot in the chassis. Despite its benefits, SMP can only scale so far. Bottlenecks can occur when several CPUs on a board share a single memory bus. Rather than put too many CPUs on the same SMP board, designers of high-end network elements can distribute applications across a networked cluster of SMP boards, where each board has its own memory array, I/O and operating system. This approach has its challenges.

Windows network risks - NetBIOS, SMB and null sessions

Some of the biggest security risks through information leakage, affecting Windows computers is the Windows network itself, and the protocols used to share files and printers amongst local computers in a local Windows network, particularly SMB and NetBIOS, taking advantage of default hidden shares. Here we describe a discovery technique also known as null sessions, that can provide additional in-depth information about a remote computer, that can further be used to compromise it and take control over it. The Windows network implementation has a specific interface that can be queried via TCP-IP ports 139 and 445. The problem exists on many Windows installations due to the fact that default security configurations are allowing it. The first step an attacker would take, is to perform a TCP scan on your Windows computer. If improperly protected, your computer may have these ports open and they may expose a lot of information, through using the so called null sessions technique. What

Sample: Java Socket Programming

//The server import java.io.*; import java.net.*; public class Provider{ ServerSocket providerSocket; Socket connection = null; ObjectOutputStream out; ObjectInputStream in; String message; Provider(){} void run() { try{ //1. creating a server socket providerSocket = new ServerSocket(2004, 10); //2. Wait for connection System.out.println("Waiting for connection"); connection = providerSocket.accept(); System.out.println("Connection received from " + connection.getInetAddress().getHostName()); //3. get Input and Output streams out = new ObjectOutputStream(connection.getOutputStream()); out.flush(); in = new ObjectInputStream(connection.getInputStream()); sendMessage("Connection successful"); //4. The two parts communicate via the input and output streams do{ try{ message = (String)in.readObject(); System.out.println("client>" + message); if (mess