Skip to main content

Posts

Showing posts from August 30, 2015

Java for Beginners

What is Object in Java? Objects, in the real world, describe things that have both a state and behaviour. For example, your phone's state can be: turned on or turned off while its behaviour can be: displaying a game or playing music. Objects, in programming, are similar. They too have a state and behaviour, storing state in fields, also called variables, and behaving via use of methods.  What is Class in Java? A class, is the "blueprint from which individual objects are created" -- a template. Classes are used to create objects, and classes also define these objects' states and behaviours. The class we have created together, HelloWorld, is not the best example for what a class is, so here's another example class that better illustrates one:  Let's dissect this class. Pay careful attention to the braces, which have been color coded to demonstrate where each brace's counterpart is. (Notice how the indents give us a sense of hierarch...

Linked List

/* File Name DLLinked.java     Program to Implement Linked List Theory:-A linked list is a Linear Data Structure which is a collection of nodes that are linked one another. Linked list is dynamic 1.Insert 2.Delete      Learner:-ABDUL MUGEESH      RollNo:-12CO104 */ import java.io.BufferedReader; import java.util.*; class Node { int info; Node next,prev; Node() { prev=next=null; } void insert(int d) { Node p=new Node(); p.info=d; if(this.next==null) this.next=p; else { Node tmp; tmp=this.next; while(tmp.next!=null) tmp=tmp.next;         tmp.next=p; p.prev=tmp; } } void delete() { Node tmp,p=null; tmp=this.next; if(tmp==null) System.out.println("\n\t\t LINKED LIST IS EMPTY."); while(tmp.next!=null) { tmp=tmp.next; } if(tmp==this.next){ this.next=null; tmp=null; } el...

Binary Subtraction

/* File Name : substract.java Program to Implement Binary Subtraction      Learner:-ABDUL MUGEESH      RollNo:-12CO104 */ import java.io.*; class substract { public static void main(String args[])throws IOException { BufferedReader obj= new BufferedReader(new InputStreamReader(System.in)); int a,b,n,i,c; System.out.print("\nEnter the Number of Bits :: "); n=Integer.parseInt(obj.readLine()); System.out.println("\nEnter Two Number :: "); a=Integer.parseInt(obj.readLine()); System.out.println(); b=Integer.parseInt(obj.readLine()); int x[]=new int [n]; int y[]=new int[n]; for(i=0;i<=n-1;i++) { int temp1=a%2; a=a/2; x[i]=temp1; int temp2=b%2; b=b/2; y[i]=temp2; } System.out.print("\nThe Binary form of the Two Numbers is :: "); for(i=n-1;i>=0;i--) { System.out.print(x[i]); } System.out.println(); for(i=n-1;i>=0;i--) ...

Booths Algrithm

/*  File Name: bnew.java Program to Implement Bhoots Algrithms.      Learner:-ABDUL MUGEESH      RollNo:-12CO104 */ import java.io.*; import java.util.*; class booth { static void add(int a[],int b[],int ad) { int i,j,c=0; for(j=0;j<=ad-1;j++) { a[j]=a[j]+b[j]+c; c=0; if (a[j]==2) { a[j]=0; c=1; } else if (a[j]==3) { a[j]=1; c=1; } } } static void sub(int a[],int b[],int n) { int cb,borrow=0,bit,result; int c=n; int res[]=new int[c]; System.out.println(); for(bit=0;bit<=n-1;bit++) { result=a[bit]-b[bit]-borrow; if (result<0) { result=result*(-1); borrow=1; } else { borrow=0; } a[bit]=result%2; } } static void display(int a[],int b[],int q[],int q1,int n) { int i; for(i=n-1;i>=0;i--) { System.out.print(a[i]); } System.out.print("\t"); for(i...

DECIMAL NUMBER INTO BINARY CONVERSION

/* File Name : D2B.java */ // PROGRAM TO CONVERT DECIMAL NUMBER INTO BINARY import java.io.*; import java.lang.*; class D2B { public static void main(String []arg)throws IOException { int a[]=new int[9]; int b[]=new int[9]; int C[]=new int[9]; int D[]=new int[9]; int w[]=new int[9]; int x,y; int i=0; int j,q=0; int k=0; int l; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter the First Decimal Number"); x=Integer.parseInt(br.readLine()); //READING NUMBER for(i=0;i<9;i++) { a[i]=0; b[i]=0; C[i]=0; D[i]=0; w[i]=0; } i=0; while(x>0) { a[i]=x%2; x=x/2; i++; } System.out.print("Binary Conversion  for Firt Number::"); for(i=i-1,j=0;i>=0;i--,j++) { b[j]=a[i]; System.out.print(b[j]); } System.out.println(); System.out.println("Enter the  Second Decimal Number"); y...

Binary Addition

/* File Name : BinAdd.java */    import java.util.*; public class BinAdd    {  public static String addBit(String a, String b, String c)  { String r=""; if(a.equals("1") && b.equals("0") || a.equals("0") && b.equals("1")) { if( c.equals("0")) r="1"; else { r="0"; c="1"; } } else if( a.equals("0") && b.equals("0") ) { if(c.equals("0")) r="0"; else r="1"; } else if( a.equals("1") && b.equals("1") ) { if(c.equals("0")){ r="0"; c="1"; } else { r="1"; c="1"; } } return c+r; }   public static String add(String a, String b)   { String r=""; int len=a.length(); String carry="0"; for(int i=len-1;i...

Stack Operation

/* File Name : MyStack.java */ import java.util.Scanner; class Stack { int stk[]=new int[10]; int top,a; Stack() { top=-1; } void push (int item) { if (top==a) System.out.println("Stack overflow"); else stk[++top]=item; }/*end push*/ boolean isempty() { if (top<0) return true; else return false; }/*end isempty*/ int pop() { if (isempty()) { System.out.println("Stack underflow"); return 0; } else return (stk[top--]); }/*end pop*/ void stacktop() { if(isempty()) System.out.println("Stack underflow "); else System.out.println("Stack top is "+(stk[top])); }/*end stacktop*/ void display() { System.out.println("Stack-->"); for(int i=top;i>=0;i--) System.out.println("\t"+stk[i]); }/*end display*/ } class MyStack { public static void main(String args[]) { Scanner sc = new Scanner(System...